summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/autosave.js
diff options
context:
space:
mode:
authorFatih Acet <acetfatih@gmail.com>2016-07-22 17:01:57 +0300
committerFatih Acet <acetfatih@gmail.com>2016-07-22 17:07:29 +0300
commitbc3f33f92a16d3afdb8a472f60940e7c46a31564 (patch)
treec6e568504b7888591ad15586bb1ace99db8941ca /app/assets/javascripts/autosave.js
parent033e5423a2594e08a7ebcd2379bd2331f4c39032 (diff)
downloadgitlab-ce-jsify.tar.gz
JSify all the things!jsify
Diffstat (limited to 'app/assets/javascripts/autosave.js')
-rw-r--r--app/assets/javascripts/autosave.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/app/assets/javascripts/autosave.js b/app/assets/javascripts/autosave.js
new file mode 100644
index 00000000000..cf000f1cef9
--- /dev/null
+++ b/app/assets/javascripts/autosave.js
@@ -0,0 +1,60 @@
+this.Autosave = (function() {
+ function Autosave(field, key) {
+ this.field = field;
+ if (key.join != null) {
+ key = key.join("/");
+ }
+ this.key = "autosave/" + key;
+ this.field.data("autosave", this);
+ this.restore();
+ this.field.on("input", (function(_this) {
+ return function() {
+ return _this.save();
+ };
+ })(this));
+ }
+
+ Autosave.prototype.restore = function() {
+ var e, error, text;
+ if (window.localStorage == null) {
+ return;
+ }
+ try {
+ text = window.localStorage.getItem(this.key);
+ } catch (error) {
+ e = error;
+ return;
+ }
+ if ((text != null ? text.length : void 0) > 0) {
+ this.field.val(text);
+ }
+ return this.field.trigger("input");
+ };
+
+ Autosave.prototype.save = function() {
+ var text;
+ if (window.localStorage == null) {
+ return;
+ }
+ text = this.field.val();
+ if ((text != null ? text.length : void 0) > 0) {
+ try {
+ return window.localStorage.setItem(this.key, text);
+ } catch (undefined) {}
+ } else {
+ return this.reset();
+ }
+ };
+
+ Autosave.prototype.reset = function() {
+ if (window.localStorage == null) {
+ return;
+ }
+ try {
+ return window.localStorage.removeItem(this.key);
+ } catch (undefined) {}
+ };
+
+ return Autosave;
+
+})();