From b22470bd98b5ff7384c4f70017dfee1b99904548 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Mon, 25 Mar 2013 09:05:59 +0100 Subject: [PATCH] rename init-all-plugin.js into plain plugin.js that has additional features for toggling expose both jquery.js and jquery.min.js and let the templates explicitly decide --- Makefile | 4 +- .../jquery.html5storage.js | 108 ++++++++++++++++++ .../jquery.html5storage.min.js | 4 + .../jquery.html5storage.url | 1 + third-party/jquery/js/jquery.js | 2 +- third-party/jquery/js/jquery.min.js | 1 + unfold/js/init-all-plugins.js | 18 --- unfold/js/plugin.js | 65 +++++++++++ views/templates/layout-unfold1.html | 5 +- views/templates/layout-unfold2.html | 5 +- 10 files changed, 189 insertions(+), 24 deletions(-) create mode 100644 third-party/jquery-html5storage/jquery.html5storage.js create mode 100644 third-party/jquery-html5storage/jquery.html5storage.min.js create mode 100644 third-party/jquery-html5storage/jquery.html5storage.url create mode 120000 third-party/jquery/js/jquery.min.js delete mode 100644 unfold/js/init-all-plugins.js create mode 100644 unfold/js/plugin.js diff --git a/Makefile b/Makefile index 38fde28e..9f56f869 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,9 @@ THIRD-PARTY-RESOURCES += $(shell ls third-party/bootstrap/*/*) # just the single js as identified with a symlink THIRD-PARTY-RESOURCES += $(shell ls third-party/datatables/js/dataTables.js) # likewise -THIRD-PARTY-RESOURCES += $(shell ls third-party/jquery/js/jquery.js) +THIRD-PARTY-RESOURCES += $(shell ls third-party/jquery/js/jquery{,.min}.js) +# for storing the visible status of plugins +THIRD-PARTY-RESOURCES += $(shell ls third-party/jquery-html5storage/jquery.html5storage{,.min}.js) # used in hazelnut THIRD-PARTY-RESOURCES += $(shell ls third-party/jquery-ui/themes/base/jquery-ui.css) # spin comes in plain or min, + the jquery plugin, and our own settings diff --git a/third-party/jquery-html5storage/jquery.html5storage.js b/third-party/jquery-html5storage/jquery.html5storage.js new file mode 100644 index 00000000..e1139f48 --- /dev/null +++ b/third-party/jquery-html5storage/jquery.html5storage.js @@ -0,0 +1,108 @@ +/*! +* jQuery Plugin to use Local Storage or Session Storage without worrying +* about HTML5 support. It uses Cookies for backward compatibility. +* +* @author Alberto Varela Sánchez (http://www.berriart.com) +* @version 1.0 (17th January 2013) +* +* Released under the MIT License (http://opensource.org/licenses/MIT) +* +* Copyright (c) 2013 Alberto Varela Sánchez (alberto@berriart.com) +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +;(function(window, $ ) { + "use strict"; + + var types = ['localStorage','sessionStorage'], + support = []; + + $.each(types, function( i, type ) { + try { + support[type] = type in window && window[type] !== null; + } catch (e) { + support[type] = false; + } + + $[type] = { + settings : { + cookiePrefix : 'html5fallback:' + type + ':', + cookieOptions : { + path : '/', + domain : document.domain, + expires : ('localStorage' === type) ? { expires: 365 } : undefined + } + }, + + getItem : function( key ) { + var response; + if(support[type]) { + response = window[type].getItem(key); + } + else { + response = $.cookie(this.settings.cookiePrefix + key); + } + + return response; + }, + + setItem : function( key, value ) { + if(support[type]) { + return window[type].setItem(key, value); + } + else { + return $.cookie(this.settings.cookiePrefix + key, value, this.settings.cookieOptions); + } + }, + + removeItem : function( key ) { + if(support[type]) { + return window[type].removeItem(key); + } + else { + var options = $.extend(this.settings.cookieOptions, { + expires: -1 + }); + return $.cookie(this.settings.cookiePrefix + key, null, options); + } + }, + + clear : function() { + if(support[type]) { + return window[type].clear(); + } + else { + var reg = new RegExp('^' + this.settings.cookiePrefix, ''), + options = $.extend(this.settings.cookieOptions, { + expires: -1 + }); + + if(document.cookie && document.cookie !== ''){ + $.each(document.cookie.split(';'), function( i, cookie ){ + if(reg.test(cookie = $.trim(cookie))) { + $.cookie( cookie.substr(0,cookie.indexOf('=')), null, options); + } + }); + } + } + } + }; + }); +})(window, jQuery); \ No newline at end of file diff --git a/third-party/jquery-html5storage/jquery.html5storage.min.js b/third-party/jquery-html5storage/jquery.html5storage.min.js new file mode 100644 index 00000000..9f1cc35a --- /dev/null +++ b/third-party/jquery-html5storage/jquery.html5storage.min.js @@ -0,0 +1,4 @@ +/*! Html5 Storage jQuery Plugin - v1.0 - 2013-01-19 +* https://github.com/artberri/jquery-html5storage +* Copyright (c) 2013 Alberto Varela; Licensed MIT */ +(function(e,t){"use strict";var n=["localStorage","sessionStorage"],r=[];t.each(n,function(n,i){try{r[i]=i in e&&e[i]!==null}catch(s){r[i]=!1}t[i]={settings:{cookiePrefix:"html5fallback:"+i+":",cookieOptions:{path:"/",domain:document.domain,expires:"localStorage"===i?{expires:365}:undefined}},getItem:function(n){var s;return r[i]?s=e[i].getItem(n):s=t.cookie(this.settings.cookiePrefix+n),s},setItem:function(n,s){return r[i]?e[i].setItem(n,s):t.cookie(this.settings.cookiePrefix+n,s,this.settings.cookieOptions)},removeItem:function(n){if(r[i])return e[i].removeItem(n);var s=t.extend(this.settings.cookieOptions,{expires:-1});return t.cookie(this.settings.cookiePrefix+n,null,s)},clear:function(){if(r[i])return e[i].clear();var n=new RegExp("^"+this.settings.cookiePrefix,""),s=t.extend(this.settings.cookieOptions,{expires:-1});document.cookie&&document.cookie!==""&&t.each(document.cookie.split(";"),function(e,r){n.test(r=t.trim(r))&&t.cookie(r.substr(0,r.indexOf("=")),null,s)})}}})})(window,jQuery); \ No newline at end of file diff --git a/third-party/jquery-html5storage/jquery.html5storage.url b/third-party/jquery-html5storage/jquery.html5storage.url new file mode 100644 index 00000000..d9156e66 --- /dev/null +++ b/third-party/jquery-html5storage/jquery.html5storage.url @@ -0,0 +1 @@ +https://github.com/artberri/jquery-html5storage.git@8188a60f5207db8aee4320c5fbc05db910d9ba4b diff --git a/third-party/jquery/js/jquery.js b/third-party/jquery/js/jquery.js index d8aad832..4aee0552 120000 --- a/third-party/jquery/js/jquery.js +++ b/third-party/jquery/js/jquery.js @@ -1 +1 @@ -jquery-1.9.1.min.js \ No newline at end of file +jquery-1.9.1.js \ No newline at end of file diff --git a/third-party/jquery/js/jquery.min.js b/third-party/jquery/js/jquery.min.js new file mode 120000 index 00000000..d8aad832 --- /dev/null +++ b/third-party/jquery/js/jquery.min.js @@ -0,0 +1 @@ +jquery-1.9.1.min.js \ No newline at end of file diff --git a/unfold/js/init-all-plugins.js b/unfold/js/init-all-plugins.js deleted file mode 100644 index 735abba9..00000000 --- a/unfold/js/init-all-plugins.js +++ /dev/null @@ -1,18 +0,0 @@ -/* upon document completion, we locate all the hide and show areas, - * and configure their behaviour - */ -$(document).ready(function() { - $('.plugin-hide').each(function() { - $(this).click(function () { - var plugin='#'+$(this).attr('id').replace('hide-',''); - var show='#'+$(this).attr('id').replace('hide-','show-'); - $(plugin).slideUp(); $(show).show(); $(this).hide();}); - }); - $('.plugin-show').each(function() { - $(this).click(function () { - var plugin='#'+$(this).attr('id').replace('show-',''); - var hide='#'+$(this).attr('id').replace('show-','hide-'); - $(plugin).slideDown(); $(hide).show(); $(this).hide();}); - }); - $('.plugin-tooltip').each(function(){ $(this).tooltip({'selector':'','placement':'right'}); }); -}) diff --git a/unfold/js/plugin.js b/unfold/js/plugin.js new file mode 100644 index 00000000..b03b7375 --- /dev/null +++ b/unfold/js/plugin.js @@ -0,0 +1,65 @@ +var plugin = { + + debug:true, + + ////////// use local storage to remember open/closed toggles + store_status : function (domid) { + var plugin=$('#'+domid); + key='toggle.'+domid; + if (debug) console.log('storing toggle status for '+domid); + $.localStorage.setItem(key,plugin.visible()); + }, + // restore last status + restore_last_status : function (domid) { + key='toggle.'+domid; + // don't do anything if nothing stored + var retrieved=$.localStorage.getItem(key,undefined); + if (retrieved===null) return; + if (debug) console.log ("Applying retrieved status " + retrieved + " to " + domid); + set_visible(domid,retrieved); + }, + // triggered upon $(document).ready + init_all_plugins: function() { + $('.plugin-hide').each(function() { + $(this).click(function () { + var plugin='#'+$(this).attr('id').replace('hide-',''); + var show='#'+$(this).attr('id').replace('hide-','show-'); + $(plugin).slideUp(); $(show).show(); $(this).hide();}); + }); + $('.plugin-show').each(function() { + $(this).click(function () { + var plugin='#'+$(this).attr('id').replace('show-',''); + var hide='#'+$(this).attr('id').replace('show-','hide-'); + $(plugin).slideDown(); $(hide).show(); $(this).hide();}); + }); + $('.plugin-tooltip').each(function(){ $(this).tooltip({'selector':'','placement':'right'}); }); + }, + toggle : function (domid) { + var plugin=$('#'+domid); + plugin.toggle(); + var showbtn=$('#show-'+domid); + var hidebtn=$('#hide-'+domid); + if (plugin.visible()) { + hidebtn.show(); + showbtn.hide(); + } else { + hidebtn.hide(); + showbtn.show(); + } + plugin.store_status(domid); + }, + // 'target' is retrieved from storage so essentially a string 'true' or 'false' + set_visible : function (domid, target) { + var plugin=$('#'+domid); + if (plugin.visible()!=target) { + if (debug) console.log('set_visible: toggling ' + domid); + plugin.toggle (domid); + } + }, +} // global unfold + +/* upon document completion, we locate all the hide and show areas, + * and configure their behaviour + */ +$(document).ready(plugin.init_all_plugins) + diff --git a/views/templates/layout-unfold1.html b/views/templates/layout-unfold1.html index 8ce6cdc1..1afb575a 100644 --- a/views/templates/layout-unfold1.html +++ b/views/templates/layout-unfold1.html @@ -8,8 +8,9 @@ {{ header_prelude }} {# let's add these ones no matter what #} {# not yet needed {% insert_str prelude "css/layout-unfold1.css" %} #} -{% insert_str prelude "js/jquery.js" %} -{% insert_str prelude "js/init-all-plugins.js" %} +{% insert_str prelude "js/jquery.min.js" %} +{% insert_str prelude "js/jquery.html5storage.min.js" %} +{% insert_str prelude "js/plugin.js" %} {% block container %}
diff --git a/views/templates/layout-unfold2.html b/views/templates/layout-unfold2.html index 75c30876..ed7a4881 100644 --- a/views/templates/layout-unfold2.html +++ b/views/templates/layout-unfold2.html @@ -8,8 +8,9 @@ {{ header_prelude }} {# let's add these ones no matter what #} {% insert_str prelude "css/layout-unfold2.css" %} -{% insert_str prelude "js/jquery.js" %} -{% insert_str prelude "js/init-all-plugins.js" %} +{% insert_str prelude "js/jquery.min.js" %} +{% insert_str prelude "js/jquery.html5storage.min.js" %} +{% insert_str prelude "js/plugin.js" %} {% block container %}
-- 2.43.0