Cookie to save the selected probes when refreshing the page.

Change-Id: I9b1d55d108af42ff1c03a46b4a7d77b7dd458cc8
This commit is contained in:
François Rossigneux 2014-02-13 14:05:19 +01:00
parent d2e9782030
commit 825f5d9cce
2 changed files with 37 additions and 5 deletions

View File

@ -0,0 +1,7 @@
/*!
* jQuery Cookie Plugin v1.4.0
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2013 Klaus Hartl
* Released under the MIT license
*/(function(a){typeof define=="function"&&define.amd?define(["jquery"],a):a(jQuery)})(function(a){function c(a){return h.raw?a:encodeURIComponent(a)}function d(a){return h.raw?a:decodeURIComponent(a)}function e(a){return c(h.json?JSON.stringify(a):String(a))}function f(a){a.indexOf('"')===0&&(a=a.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return a=decodeURIComponent(a.replace(b," ")),h.json?JSON.parse(a):a}catch(c){}}function g(b,c){var d=h.raw?b:f(b);return a.isFunction(c)?c(d):d}var b=/\+/g,h=a.cookie=function(b,f,i){if(f!==undefined&&!a.isFunction(f)){i=a.extend({},h.defaults,i);if(typeof i.expires=="number"){var j=i.expires,k=i.expires=new Date;k.setTime(+k+j*864e5)}return document.cookie=[c(b),"=",e(f),i.expires?"; expires="+i.expires.toUTCString():"",i.path?"; path="+i.path:"",i.domain?"; domain="+i.domain:"",i.secure?"; secure":""].join("")}var l=b?undefined:{},m=document.cookie?document.cookie.split("; "):[];for(var n=0,o=m.length;n<o;n++){var p=m[n].split("="),q=d(p.shift()),r=p.join("=");if(b&&b===q){l=g(r,f);break}!b&&(r=g(r))!==undefined&&(l[q]=r)}return l};h.defaults={},a.removeCookie=function(b,c){return a.cookie(b)===undefined?!1:(a.cookie(b,"",a.extend({},c,{expires:-1})),!a.cookie(b))}});

View File

@ -7,6 +7,7 @@
<link rel="stylesheet" href="{{ url_for('v1.static', filename='select2/select2.css') }}"/>
<link rel="icon" type="image/png" href="{{ url_for('v1.static', filename='favicon.png') }}" />
<script src="{{ url_for('v1.static', filename='jquery-1.9.1.min.js') }}"></script>
<script src="{{ url_for('v1.static', filename='jquery.cookie-1.4.0.min.js') }}"></script>
<script src="{{ url_for('v1.static', filename='select2/select2.min.js') }}"></script>
<script type="text/javascript">
// <![CDATA[
@ -54,13 +55,17 @@
$(document).ready(function () {
setInterval('reloadAllImages()', {{ refresh*1000 }});
// Apply select2 on HTML select
// Init select probe list
var cookie = $.cookie('probes');
if(!cookie) {
var probes = [];
} else {
var probes = JSON.parse(cookie);
}
$('select').select2({
placeholder: 'Select probes'
});
// Clear the page
deselectAll();
$('select').select2('val', probes);
// Event handler for adding a probe
$(document.body).on('select2-selecting', 'select', function(e) {
@ -68,7 +73,7 @@
$('#probes').append(probe);
reloadAllImages();
});
// Event handler for deleting a probe
$(document.body).on('select2-removing', 'select', function(e) {
$('#' + e.val).parent().remove();
@ -77,6 +82,26 @@
// Bind buttons to event handlers
$('#select-all').click(selectAll);
$('#deselect-all').click(deselectAll);
// Display the graph for each preselected probes
$.each(probes, function(index, value) {
$('select').trigger({
type: 'select2-selecting',
val: value
});
});
{% if view == 'scale' %}
// Set a cookie storing the probe list
$(window).unload(function() {
var value = $('select').val();
if(value == null) {
$.removeCookie('probes', {path: '/'});
} else {
$.cookie('probes', JSON.stringify(value), {path: '/'});
}
});
{% endif %}
});
// ]]>
</script>