Skip to content

Commit

Permalink
Merge pull request kugaevsky#1 from kf99916/master
Browse files Browse the repository at this point in the history
Let jquery-phoenix support sessionStorage
  • Loading branch information
foamrider committed Mar 30, 2016
2 parents f2df54f + 7862ad1 commit f7baa16
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
37 changes: 28 additions & 9 deletions jquery.phoenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ FEATURES:
webStorage: "localStorage",
maxItems: 100,
saveInterval: 1000,
expireTime: false,
clearOnSubmit: false,
saveOnChange: false,
saveOnInput: false,
keyAttributes: ["tagName", "id", "name"]
};
saveTimers = [];
Expand All @@ -50,19 +52,22 @@ FEATURES:
this.options = $.extend({}, defaults, (typeof option === "object" ? option : void 0));
if (typeof option === "string") {
this.action = option;
} else if (this.options.action != null) {
this.action = this.options.action;
}
this.uri = window.location.host + window.location.pathname;
storageArray = [this.options.namespace, this.uri].concat((function() {
var _i, _len, _ref, _results;
_ref = this.options.keyAttributes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attr = _ref[_i];
_results.push(this.element[attr]);
var j, len, ref, results;
ref = this.options.keyAttributes;
results = [];
for (j = 0, len = ref.length; j < len; j++) {
attr = ref[j];
results.push(this.element[attr]);
}
return _results;
return results;
}).call(this));
this.storageKey = storageArray.join(".");
this.storageKeyDate = "savedDate." + storageArray.join(".");
this.storageIndexKey = [this.options.namespace, "index", window.location.host].join(".");
this.webStorage = window[this.options.webStorage];
this.init();
Expand All @@ -76,6 +81,7 @@ FEATURES:
var e, indexedItems;
this.stop();
this.webStorage.removeItem(this.storageKey);
this.webStorage.removeItem(this.storageKeyDate);
e = $.Event("phnx.removed");
this.$element.trigger(e);
indexedItems = this.indexedItems();
Expand All @@ -97,7 +103,11 @@ FEATURES:
};

Phoenix.prototype.load = function() {
var e, savedValue;
var e, savedDate, savedValue;
savedDate = this.webStorage[this.storageKeyDate];
if (this.options.expireTime && parseInt(savedDate) + parseInt(this.options.expireTime) < (new Date).getTime()) {
this.remove();
}
savedValue = this.webStorage[this.storageKey];
if (savedValue != null) {
if (this.$element.is(":checkbox, :radio")) {
Expand All @@ -119,6 +129,7 @@ FEATURES:

Phoenix.prototype.save = function() {
var e, selectedValues;
this.webStorage[this.storageKeyDate] = (new Date).getTime();
this.webStorage[this.storageKey] = this.$element.is(":checkbox, :radio") ? this.element.checked : this.element.tagName === "SELECT" ? (selectedValues = $.map(this.$element.find("option:selected"), function(el) {
return el.value;
}), JSON.stringify(selectedValues)) : this.element.value;
Expand Down Expand Up @@ -177,6 +188,13 @@ FEATURES:
};
})(this));
}
if (this.options.saveOnInput) {
$(this.element).on("input", (function(_this) {
return function() {
return _this.save();
};
})(this));
}
if (this.options.saveOnChange) {
return $(this.element).change((function(_this) {
return function() {
Expand All @@ -191,9 +209,10 @@ FEATURES:

})();
supportsHtml5Storage = function(webStorage) {
var error;
try {
return webStorage in window && window[webStorage] !== null;
} catch (_error) {
} catch (error) {
return false;
}
};
Expand Down
2 changes: 1 addition & 1 deletion jquery.phoenix.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
},
"homepage": "https://github.com/kugaevsky/jquery-phoenix",
"devDependencies": {
"eslint": "^0.6.2",
"gulp": "^3.6.2",
"gulp-coffee": "^1.4.3",
"gulp-eslint": "^0.1.7",
"gulp-load-plugins": "^0.5.1",
"gulp-rename": "^1.2.0",
"gulp-size": "^0.3.1",
"gulp-uglify": "^0.3.0",
"gulp-util": "^2.2.14"
"eslint": "^2.5.3",
"gulp": "^3.9.1",
"gulp-coffee": "^2.3.1",
"gulp-eslint": "^2.0.0",
"gulp-load-plugins": "^1.2.0",
"gulp-rename": "^1.2.2",
"gulp-size": "^2.1.0",
"gulp-uglify": "^1.5.3",
"gulp-util": "^3.0.7"
}
}
10 changes: 7 additions & 3 deletions src/jquery.phoenix.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ FEATURES:

@$element = $(@element)
@options = $.extend {}, defaults, (option if typeof option is "object")
@action = option if typeof option is "string"
if typeof option is "string"
@action = option
else if this.options.action?
@action = this.options.action
@uri = window.location.host + window.location.pathname
storageArray = [ @options.namespace, @uri ].concat (@element[attr] for attr in @options.keyAttributes)
@storageKey = storageArray.join "."
@storageKeyDate = 'savedDate.' + storageArray.join "."
@storageKeyDate = "savedDate." + storageArray.join "."
@storageIndexKey = [ @options.namespace, "index", window.location.host ].join(".")
@webStorage = window[@options.webStorage]

Expand All @@ -66,6 +69,7 @@ FEATURES:
remove: ->
@stop()
@webStorage.removeItem @storageKey
@webStorage.removeItem @storageKeyDate
e = $.Event("phnx.removed")
@$element.trigger(e)
indexedItems = @indexedItems()
Expand Down Expand Up @@ -136,7 +140,7 @@ FEATURES:
@load()
@start()
$(@options.clearOnSubmit).submit(=> @remove()) if @options.clearOnSubmit
$(@element).on('input',() => @save()) if @options.saveOnInput
$(@element).on("input",() => @save()) if @options.saveOnInput
$(@element).change(() => @save()) if @options.saveOnChange

supportsHtml5Storage = (webStorage) ->
Expand Down

0 comments on commit f7baa16

Please sign in to comment.