/* =========================================================== * PageState.js v0.0.1 * =========================================================== * Copyright 2015 Prevu * ========================================================== */ (function ($) { "use strict"; // jshint ;_; /* PageState CLASS DEFINITION * ====================== */ var PageState = function (element, options) { this.init(element, options); }; PageState.prototype = { constructor: PageState, init: function(element, options) { var that = this; this.options = options; this.$container = $(element); this.$storage = $.sessionStorage; this.$dataset = {}; if (!that.$storage) return false; if (!that.options.pageKey) { throw "Required page key not set. Init page state cannot be performed."; } var pageState = that.$storage.get(that.options.pageKey); if (!pageState) return false; this.$dataset = pageState; // gather all controls marked with PageState attribute data-ps-key from the specified controller $.each(this.$container.find("[data-ps-key]"), function(index, obj) { // mark control as handled to avoid state restoration after user changed anything. Restoration applicable only once. var objType = $(obj).data('ps-type'); if (!objType) { switch ($(obj).tagName) { default: $(obj).on("change", function() { plugin.saveData($(obj)); }); break; } } else { switch (objType) { case 'date': break; default: $(obj).on("change", function() { plugin.saveData($(obj)); }); break; } } }); if (typeof this.options.initCallback === 'function') { this.options.initCallback(this.$dataset); } return true; }, setData: function(key, value) { if (!this.$dataset) { throw "Dataset not set. Save page state cannot be performed."; } this.$dataset[key] = value; }, saveData: function(control) { if (!this.$storage) { throw "Storage not set. Save page state cannot be performed."; } if (!this.options.pageKey) { throw "Required page key not set. Save page state cannot be performed."; } if (!this.$dataset) { throw "Dataset not set. Save page state cannot be performed."; } if (!!control) { var objType = $(control).data('ps-type'); if (!objType) { switch ($(control).tagName) { default: this.$dataset[$(control).data('ps-key')] = $(control).val(); break; } } else { switch (objType) { case 'date': break; default: this.$dataset[$(control).data('ps-key')] = $(control).val(); break; } } } this.$storage.set(this.options.pageKey, JSON.stringify(this.$dataset)); }, restoreData: function(container) { var plugin = this; if (!this.$storage) throw "Required storage provider is not set. Restore page state cannot be performed."; if (!plugin.options.pageKey) throw "Required page key not set. Restore page state cannot be performed."; var parent = $(container) || this.$container; // gather all controls marked with PageState attribute data-ps-key from the specified controller $.each(parent.find("[data-ps-key]:not([data-ps-restored])"), function (index, obj) { //plugin.$restored.push($(this)); // mark control as handled to avoid state restoration after user changed anything. Restoration applicable only once. $(obj).attr('data-ps-restored', true); var propValue = plugin.$dataset[$(obj).data('ps-key')]; var objType = $(obj).data('ps-type'); if (!objType) { switch ($(obj).tagName) { default: if (!propValue) return; $(obj).val(propValue); break; } } else { switch (objType) { case 'date': break; default: if (!propValue) return; $(obj).val(propValue); break; } } }); return true; }, destroy: function() { var e = $.Event('destroy'); this.$container.trigger(e); if (e.isDefaultPrevented()) return; this.$container .off('.pageState') .removeData('pageState'); //this.$container.trigger('destroyed'); } }; /* PAGESTATE PLUGIN DEFINITION * ======================= */ $.fn.pageState = function (option, args) { return this.each(function() { var $this = $(this), data = $this.data('pageState'), options = $.extend({}, $.fn.pageState.defaults, $this.data(), typeof option == 'object' && option); if (!data) $this.data('pageState', (data = new PageState(this, options))); if (typeof option == 'string') data[option].apply(data, [].concat(args)); }); }; $.fn.pageState.defaults = { pageKey: null }; $.fn.pageState.Constructor = PageState; }(jQuery));