simditor-autosave.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module unless amdModuleId is set
  4. define('simditor-autosave', ["jquery","simple-module","simditor"], function (a0,b1,c2) {
  5. return (root['SimditorAutosave'] = factory(a0,b1,c2));
  6. });
  7. } else if (typeof exports === 'object') {
  8. // Node. Does not work with strict CommonJS, but
  9. // only CommonJS-like environments that support module.exports,
  10. // like Node.
  11. module.exports = factory(require("jquery"),require("simple-module"),require("simditor"));
  12. } else {
  13. root['SimditorAutosave'] = factory(jQuery,SimpleModule,Simditor);
  14. }
  15. }(this, function ($, SimpleModule, Simditor) {
  16. var SimditorAutosave,
  17. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  18. hasProp = {}.hasOwnProperty;
  19. SimditorAutosave = (function(superClass) {
  20. extend(SimditorAutosave, superClass);
  21. function SimditorAutosave() {
  22. return SimditorAutosave.__super__.constructor.apply(this, arguments);
  23. }
  24. SimditorAutosave.pluginName = 'Autosave';
  25. SimditorAutosave.prototype.opts = {
  26. autosave: true,
  27. autosavePath: null
  28. };
  29. SimditorAutosave.prototype._init = function() {
  30. var currentVal, link, name, val;
  31. this.editor = this._module;
  32. if (!this.opts.autosave) {
  33. return;
  34. }
  35. this.name = typeof this.opts.autosave === 'string' ? this.opts.autosave : 'simditor';
  36. if (this.opts.autosavePath) {
  37. this.path = this.opts.autosavePath;
  38. } else {
  39. link = $("<a/>", {
  40. href: location.href
  41. });
  42. name = this.editor.textarea.data('autosave') || this.name;
  43. this.path = "/" + (link[0].pathname.replace(/\/$/g, "").replace(/^\//g, "")) + "/autosave/" + name + "/";
  44. }
  45. if (!this.path) {
  46. return;
  47. }
  48. this.editor.on("valuechanged", (function(_this) {
  49. return function() {
  50. return _this.storage.set(_this.path, _this.editor.getValue());
  51. };
  52. })(this));
  53. this.editor.el.closest('form').on('ajax:success.simditor-' + this.editor.id, (function(_this) {
  54. return function(e) {
  55. return _this.storage.remove(_this.path);
  56. };
  57. })(this));
  58. val = this.storage.get(this.path);
  59. if (!val) {
  60. return;
  61. }
  62. currentVal = this.editor.textarea.val();
  63. if (val === currentVal) {
  64. return;
  65. }
  66. if (this.editor.textarea.is('[data-autosave-confirm]')) {
  67. if (confirm(this.editor.textarea.data('autosave-confirm') || 'Are you sure to restore unsaved changes?')) {
  68. return this.editor.setValue(val);
  69. } else {
  70. return this.storage.remove(this.path);
  71. }
  72. } else {
  73. return this.editor.setValue(val);
  74. }
  75. };
  76. SimditorAutosave.prototype.storage = {
  77. supported: function() {
  78. var error;
  79. try {
  80. localStorage.setItem('_storageSupported', 'yes');
  81. localStorage.removeItem('_storageSupported');
  82. return true;
  83. } catch (_error) {
  84. error = _error;
  85. return false;
  86. }
  87. },
  88. set: function(key, val, session) {
  89. var storage;
  90. if (session == null) {
  91. session = false;
  92. }
  93. if (!this.supported()) {
  94. return;
  95. }
  96. storage = session ? sessionStorage : localStorage;
  97. return storage.setItem(key, val);
  98. },
  99. get: function(key, session) {
  100. var storage;
  101. if (session == null) {
  102. session = false;
  103. }
  104. if (!this.supported()) {
  105. return;
  106. }
  107. storage = session ? sessionStorage : localStorage;
  108. return storage[key];
  109. },
  110. remove: function(key, session) {
  111. var storage;
  112. if (session == null) {
  113. session = false;
  114. }
  115. if (!this.supported()) {
  116. return;
  117. }
  118. storage = session ? sessionStorage : localStorage;
  119. return storage.removeItem(key);
  120. }
  121. };
  122. return SimditorAutosave;
  123. })(SimpleModule);
  124. Simditor.connect(SimditorAutosave);
  125. return SimditorAutosave;
  126. }));