stores.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. domain.development_mode.set(data.response.expires_on || 0);
  53. }
  54. });
  55. }
  56. function purgeCache(name) {
  57. return cloudflare.purgeCache(findDomain(name).id.val(), {purge_everything: true});
  58. }
  59. cloudflare.domains().then(function(data) {
  60. data.result.forEach(function(domain) {
  61. domain.development_mode = Date.now() / 1000 + domain.development_mode;
  62. domain.records = [];
  63. });
  64. DomainCortex.set(data.result);
  65. });
  66. module.exports = {
  67. Domains: {
  68. find: findDomain,
  69. recordAdd: recordAdd,
  70. recordEdit: recordEdit,
  71. recordDelete: recordDelete,
  72. setDevelopmentMode: setDevelopmentMode,
  73. purgeCache: purgeCache,
  74. loadRecords: loadRecords,
  75. cortex: DomainCortex
  76. }
  77. };
  78. window.Cortex = Cortex;