123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- ;(function ($) {
- if (!$.layout) return;
- $.ui.cookie = {
-
- acceptsCookies: !!navigator.cookieEnabled
- , read: function (name) {
- var
- c = document.cookie
- , cs = c ? c.split(';') : []
- , pair
- ;
- for (var i=0, n=cs.length; i < n; i++) {
- pair = $.trim(cs[i]).split('=');
- if (pair[0] == name)
- return decodeURIComponent(pair[1]);
- }
- return "";
- }
- , write: function (name, val, cookieOpts) {
- var
- params = ''
- , date = ''
- , clear = false
- , o = cookieOpts || {}
- ;
- if (!o.expires) ;
- else if (o.expires.toUTCString)
- date = o.expires;
- else if (typeof o.expires == 'number') {
- date = new Date();
- if (o.expires > 0)
- date.setDate(date.getDate() + o.expires);
- else {
- date.setYear(1970);
- clear = true;
- }
- }
- if (date) params += ';expires='+ date.toUTCString();
- if (o.path) params += ';path='+ o.path;
- if (o.domain) params += ';domain='+ o.domain;
- if (o.secure) params += ';secure';
- document.cookie = name +'='+ (clear ? "" : encodeURIComponent( val )) + params;
- }
- , clear: function (name) {
- $.ui.cookie.write(name, '', {expires: -1});
- }
- };
- $.layout.plugins.stateManagement = true;
- $.layout.defaults.stateManagement = {
- enabled: false
- , autoSave: true
- , autoLoad: true
-
- , stateKeys: "north.size,south.size,east.size,west.size,"+
- "north.isClosed,south.isClosed,east.isClosed,west.isClosed,"+
- "north.isHidden,south.isHidden,east.isHidden,west.isHidden"
- , cookie: {
- name: ""
- , domain: ""
- , path: ""
- , expires: ""
- , secure: false
- }
- };
- $.layout.optionsMap.layout.push("stateManagement");
- $.layout.state = {
-
- config: {
- allPanes: "north,south,west,east,center"
- }
-
- , saveCookie: function (inst, keys, cookieOpts) {
- var o = inst.options
- , oS = o.stateManagement
- , oC = $.extend( {}, oS.cookie, cookieOpts || {} )
- , data = inst.state.stateData = inst.readState( keys || oS.stateKeys )
- ;
- $.ui.cookie.write( oC.name || o.name || "Layout", $.layout.state.encodeJSON(data), oC );
- return $.extend( {}, data );
- }
-
- , deleteCookie: function (inst) {
- var o = inst.options;
- $.ui.cookie.clear( o.stateManagement.cookie.name || o.name || "Layout" );
- }
-
- , readCookie: function (inst) {
- var o = inst.options;
- var c = $.ui.cookie.read( o.stateManagement.cookie.name || o.name || "Layout" );
-
- return c ? $.layout.state.decodeJSON(c) : {};
- }
-
- , loadCookie: function (inst) {
- var c = $.layout.state.readCookie(inst);
- if (c && !$.isEmptyObject( c )) {
- inst.state.stateData = $.extend({}, c);
- inst.loadState(c);
- }
- return c;
- }
-
-
- , loadState: function (inst, stateData, animate) {
- stateData = $.layout.transformData( stateData );
- $.extend( true, inst.options, stateData );
-
- if (inst.state.initialized) {
- var pane, o, s, h, c
- , noAnimate = (animate===false);
- $.each($.layout.state.config.allPanes.split(","), function (idx, pane) {
- o = stateData[ pane ];
- if (typeof o != 'object') return;
- s = o.size;
- c = o.initClosed;
- h = o.initHidden;
- if (s > 0 || s=="auto") inst.sizePane(pane, s, false, null, noAnimate);
- if (h === true) inst.hide(pane, a);
- else if (c === false) inst.open (pane, false, noAnimate);
- else if (c === true) inst.close(pane, false, noAnimate);
- else if (h === false) inst.show (pane, false, noAnimate);
- });
- }
- }
-
- , readState: function (inst, keys) {
- var
- data = {}
- , alt = { isClosed: 'initClosed', isHidden: 'initHidden' }
- , state = inst.state
- , pair, pane, key, val
- ;
- if (!keys) keys = inst.options.stateManagement.stateKeys;
- if ($.isArray(keys)) keys = keys.join(",");
-
- keys = keys.replace(/__/g, ".").split(',');
-
- for (var i=0, n=keys.length; i < n; i++) {
- pair = keys[i].split(".");
- pane = pair[0];
- key = pair[1];
- if ($.layout.state.config.allPanes.indexOf(pane) < 0) continue;
- val = state[ pane ][ key ];
- if (val == undefined) continue;
- if (key=="isClosed" && state[pane]["isSliding"])
- val = true;
- ( data[pane] || (data[pane]={}) )[ alt[key] ? alt[key] : key ] = val;
- }
- return data;
- }
-
- , encodeJSON: function (JSON) {
- var native = window.JSON || {};
- return (native.stringify || stringify)(JSON);
- function stringify (h) {
- var D=[], i=0, k, v, t;
- for (k in h) {
- v = h[k];
- t = typeof v;
- if (t == 'string')
- v = '"'+ v +'"';
- else if (t == 'object')
- v = parse(v);
- D[i++] = '"'+ k +'":'+ v;
- }
- return '{'+ D.join(',') +'}';
- };
- }
-
- , decodeJSON: function (str) {
- try { return $.parseJSON ? $.parseJSON(str) : window["eval"]("("+ str +")") || {}; }
- catch (e) { return {}; }
- }
- , _create: function (inst) {
-
- $.extend( inst, {
-
- readCookie: function () { return $.layout.state.readCookie(inst); }
-
- , deleteCookie: function () { $.layout.state.deleteCookie(inst); }
-
- , saveCookie: function (keys, cookieOpts) { return $.layout.state.saveCookie(inst, keys, cookieOpts); }
-
- , loadCookie: function () { return $.layout.state.loadCookie(inst); }
-
- , loadState: function (stateData, animate) { $.layout.state.loadState(inst, stateData, animate); }
-
- , readState: function (keys) { return $.layout.state.readState(inst, keys); }
-
- , encodeJSON: $.layout.state.encodeJSON
- , decodeJSON: $.layout.state.decodeJSON
- });
-
- inst.state.stateData = {};
-
- var oS = inst.options.stateManagement;
- if (oS.enabled) {
- if (oS.autoLoad)
- inst.loadCookie();
- else
- inst.state.stateData = inst.readCookie();
- }
- }
- , _unload: function (inst) {
- var oS = inst.options.stateManagement;
- if (oS.enabled) {
- if (oS.autoSave)
- inst.saveCookie();
- else
- inst.state.stateData = inst.readState();
- }
- }
- };
- $.layout.onCreate.push( $.layout.state._create );
- $.layout.onUnload.push( $.layout.state._unload );
- })( jQuery );
|