stores.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.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(domain.id.val()).then(function(data) {
  15. domain.records.set(data.result);
  16. });
  17. }
  18. function loadSettings(name) {
  19. var domain = findDomain(name);
  20. if(domain.settings.count() > 0) {
  21. return;
  22. }
  23. cloudflare.settings(domain.id.val()).then(function(data) {
  24. data.result.forEach(function(setting) {
  25. if(setting.id == 'development_mode') {
  26. setting.timestamp = Date.now() / 1000 + setting.time_remaining;
  27. }
  28. });
  29. domain.settings.set(data.result);
  30. });
  31. }
  32. function recordAdd(name, record) {
  33. var domain = findDomain(name);
  34. return cloudflare.recordAdd(domain.id.val(), record).then(function(data) {
  35. if(data.success) {
  36. domain.records.push(data.result);
  37. }
  38. });
  39. }
  40. function recordEdit(name, record) {
  41. var domain = findDomain(name);
  42. return cloudflare.recordEdit({zoneId: domain.id.val(), recId: record.id}, record).then(function(data) {
  43. if(data.success) {
  44. var oldRecord = domain.records.find(function(r) {
  45. return r.id.val() === record.id;
  46. });
  47. oldRecord.set(data.result);
  48. }
  49. });
  50. }
  51. function recordDelete(name, id) {
  52. var domain = findDomain(name);
  53. return cloudflare.recordDelete({zoneId: domain.id.val(), recId: id}).then(function(data) {
  54. if(data.success) {
  55. var oldRecord = domain.records.find(function(r) {
  56. return r.id.val() === id;
  57. });
  58. oldRecord.remove();
  59. }
  60. });
  61. }
  62. function settingChange(name, setting, value) {
  63. var domain = findDomain(name);
  64. var params = {zoneId: domain.id.val(), setting: setting};
  65. return cloudflare.settingChange(params, {value: value}).then(function(data) {
  66. if(data.success) {
  67. if(data.result.id == 'development_mode') {
  68. data.result.timestamp = Date.now() / 1000 + data.result.time_remaining;
  69. }
  70. domain.settings.find(function(s) { return s.id.val() == setting }).set(data.result);
  71. }
  72. });
  73. }
  74. function purgeCache(name) {
  75. return cloudflare.purgeCache(findDomain(name).id.val(), {purge_everything: true});
  76. }
  77. cloudflare.domains().then(function(data) {
  78. data.result.forEach(function(domain) {
  79. domain.records = [];
  80. domain.settings = [];
  81. });
  82. DomainCortex.set(data.result);
  83. });
  84. module.exports = {
  85. Domains: {
  86. find: findDomain,
  87. recordAdd: recordAdd,
  88. recordEdit: recordEdit,
  89. recordDelete: recordDelete,
  90. settingChange: settingChange,
  91. purgeCache: purgeCache,
  92. loadRecords: loadRecords,
  93. loadSettings: loadSettings,
  94. cortex: DomainCortex
  95. }
  96. };
  97. window.Cortex = Cortex;