stores.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 recordAdd(name, record) {
  19. var domain = findDomain(name);
  20. return cloudflare.recordAdd(domain.id.val(), record).then(function(data) {
  21. if(data.success) {
  22. domain.records.push(data.result);
  23. }
  24. });
  25. }
  26. function recordEdit(name, record) {
  27. var domain = findDomain(name);
  28. return cloudflare.recordEdit({zoneId: domain.id.val(), recId: record.id}, record).then(function(data) {
  29. if(data.success) {
  30. var oldRecord = domain.records.find(function(r) {
  31. return r.id.val() === record.id;
  32. });
  33. oldRecord.set(data.result);
  34. }
  35. });
  36. }
  37. function recordDelete(name, id) {
  38. var domain = findDomain(name);
  39. return cloudflare.recordDelete({zoneId: domain.id.val(), recId: id}).then(function(data) {
  40. if(data.success) {
  41. var oldRecord = domain.records.find(function(r) {
  42. return r.id.val() === id;
  43. });
  44. oldRecord.remove();
  45. }
  46. });
  47. }
  48. function setDevelopmentMode(name, value) {
  49. var domain = findDomain(name);
  50. return cloudflare.setDevelopmentMode(domain.id.val(), {value: value ? 'on' : 'off'}).then(function(data) {
  51. if(data.success) {
  52. var time = Date.now() / 1000 + (data.result.time_remaining || 0);
  53. domain.development_mode.set(time);
  54. }
  55. });
  56. }
  57. function purgeCache(name) {
  58. return cloudflare.purgeCache(findDomain(name).id.val(), {purge_everything: true});
  59. }
  60. cloudflare.domains().then(function(data) {
  61. data.result.forEach(function(domain) {
  62. domain.development_mode = Date.now() / 1000 + domain.development_mode;
  63. domain.records = [];
  64. });
  65. DomainCortex.set(data.result);
  66. });
  67. module.exports = {
  68. Domains: {
  69. find: findDomain,
  70. recordAdd: recordAdd,
  71. recordEdit: recordEdit,
  72. recordDelete: recordDelete,
  73. setDevelopmentMode: setDevelopmentMode,
  74. purgeCache: purgeCache,
  75. loadRecords: loadRecords,
  76. cortex: DomainCortex
  77. }
  78. };
  79. window.Cortex = Cortex;