Skip to content

Commit

Permalink
Use a (jQuery) cookie to persist view state
Browse files Browse the repository at this point in the history
  • Loading branch information
mateu committed Apr 5, 2012
1 parent df7d05b commit 577b8ab
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
22 changes: 18 additions & 4 deletions files/javascript/render_page.js
Expand Up @@ -23,6 +23,10 @@ $(document).ready(function() {
// extraSpace : 60
// }).trigger('change');

// Recall edit view state
if ($.cookie('mojito.toggle_view') == 'off') {
toggle_view_off();
}
toggle_view();
prettyPrint();
sh_highlightDocument();
Expand Down Expand Up @@ -142,17 +146,27 @@ function toggle_view() {
$( "#toggle_view" ).button();
$( "#toggle_view" ).click(
function() {
$('.view_area_edit_mode').toggle();
var edit_width = $('#edit_area').css('width');
var edit_width_digits = edit_width.match(/^\d+/);
// Assumption: if #edit_area width is more than half the
// total window then we are toggling from width to narrow
if( edit_width_digits > (.50 * $(window).width()) ) {
$('#edit_area').css('width', '46%');
toggle_view_on();
}
else {
$('#edit_area').css('width', '100%');
toggle_view_off();
};
}
);
}
}

function toggle_view_on() {
$('.view_area_edit_mode').show();
$('#edit_area').css('width', '46%');
$.cookie('mojito.toggle_view', 'on', { expires: 7, path: '/' });
}
function toggle_view_off() {
$('.view_area_edit_mode').hide();
$('#edit_area').css('width', '100%');
$.cookie('mojito.toggle_view', 'off', { expires: 7, path: '/' });
}
47 changes: 47 additions & 0 deletions files/jquery/jquery.cookie.js
@@ -0,0 +1,47 @@
/*!
* jQuery Cookie Plugin
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function($) {
$.cookie = function(key, value, options) {

// key and at least value given, set cookie...
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
options = $.extend({}, options);

if (value === null || value === undefined) {
options.expires = -1;
}

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}

value = String(value);

return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}

// key and possibly options given, get cookie...
options = value || {};
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

var pairs = document.cookie.split('; ');
for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
}
return null;
};
})(jQuery);
1 change: 1 addition & 0 deletions lib/Mojito/Template/Role/Javascript.pm
Expand Up @@ -27,6 +27,7 @@ sub _build_javascripts {
'javascript/publish.js',
'syntax_highlight/prettify.js',
'jquery/jquery-ui.custom.min.js',
'jquery/jquery.cookie.js',
'SHJS/sh_main.min.js',
'SHJS/sh_perl.min.js',
'SHJS/sh_javascript.min.js',
Expand Down

0 comments on commit 577b8ab

Please sign in to comment.