stores.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var cloudflare = require('./cloudflare');
  2. var Cortex = require('cortexjs');
  3. var DomainCortex = new Cortex([]);
  4. function findDomain(name) {
  5. return DomainCortex.find(function(d) {
  6. return d.zone_name.val() === name;
  7. });
  8. }
  9. function loadRecords(name) {
  10. var domain = findDomain(name);
  11. if(domain.records.count() > 0) {
  12. return;
  13. }
  14. cloudflare.records(name).then(function(data) {
  15. domain.records.set(data.response.recs.objs);
  16. });
  17. }
  18. function loadSettings(name) {
  19. var domain = findDomain(name);
  20. if(domain.settings.val()) {
  21. return;
  22. }
  23. cloudflare.settings(name).then(function(data) {
  24. domain.settings.set(data.response.result.objs[0]);
  25. });
  26. }
  27. function recordAdd(name, record) {
  28. return cloudflare.recordAdd(name, record).then(function(data) {
  29. if(data.result === 'success') {
  30. findDomain(name).records.push(data.response.rec.obj);
  31. }
  32. });
  33. }
  34. function recordEdit(name, record) {
  35. return cloudflare.recordEdit(name, record).then(function(data) {
  36. if(data.result === 'success') {
  37. var domain = findDomain(name);
  38. var oldRecord = domain.records.find(function(r) {
  39. return r.rec_id.val() === record.id;
  40. });
  41. oldRecord.set(data.response.rec.obj);
  42. }
  43. });
  44. }
  45. function recordDelete(name, id) {
  46. return cloudflare.recordDelete(name, id).then(function(data) {
  47. if(data.result === 'success') {
  48. var domain = findDomain(name);
  49. var oldRecord = domain.records.find(function(r) {
  50. return r.rec_id.val() === id;
  51. });
  52. oldRecord.remove();
  53. }
  54. });
  55. }
  56. function setDevelopmentMode(name, value) {
  57. return cloudflare.setDevelopmentMode(name, value).then(function(data) {
  58. if(data.result === 'success') {
  59. findDomain(name).settings.dev_mode.set(data.response.expires_on || 0);
  60. }
  61. });
  62. }
  63. function purgeCache(name) {
  64. return cloudflare.purgeCache(name);
  65. }
  66. cloudflare.domains().then(function(data) {
  67. DomainCortex.set(data.response.zones.objs);
  68. DomainCortex.forEach(function(element) {
  69. element.add('records', []);
  70. element.add('settings', false);
  71. });
  72. });
  73. module.exports = {
  74. Domains: {
  75. find: findDomain,
  76. recordAdd: recordAdd,
  77. recordEdit: recordEdit,
  78. recordDelete: recordDelete,
  79. setDevelopmentMode: setDevelopmentMode,
  80. purgeCache: purgeCache,
  81. loadRecords: loadRecords,
  82. loadSettings: loadSettings,
  83. cortex: DomainCortex
  84. }
  85. };
  86. window.Cortex = Cortex;