diff options
author | Jochen Topf <jochen@topf.org> | 2014-05-16 20:08:00 +0200 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2014-05-16 20:08:00 +0200 |
commit | 19ec1d7e4325df4b802acf1c0b57f6e447601b92 (patch) | |
tree | 809dd6b12c85fec398f0e04785370c002675b73a | |
parent | b086da01dccf250ff0ae79a2c309d376c2ba81e5 (diff) | |
download | taginfo-19ec1d7e4325df4b802acf1c0b57f6e447601b92.tar taginfo-19ec1d7e4325df4b802acf1c0b57f6e447601b92.tar.gz |
Add cookie-base comparison list.
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | web/i18n/de.yml | 8 | ||||
-rw-r--r-- | web/i18n/en.yml | 8 | ||||
-rw-r--r-- | web/lib/javascript.rb | 1 | ||||
-rw-r--r-- | web/lib/ui/comparison.rb | 2 | ||||
-rw-r--r-- | web/lib/ui/keys.rb | 2 | ||||
-rw-r--r-- | web/lib/ui/tags.rb | 2 | ||||
-rw-r--r-- | web/public/css/taginfo.css | 11 | ||||
-rw-r--r-- | web/public/js/jquery.cookie-minified.js | 8 | ||||
-rw-r--r-- | web/public/js/jquery.cookie.js | 117 | ||||
-rw-r--r-- | web/public/js/taginfo.js | 66 | ||||
-rw-r--r-- | web/views/key.erb | 15 | ||||
-rw-r--r-- | web/views/tag.erb | 15 |
13 files changed, 252 insertions, 4 deletions
@@ -66,6 +66,7 @@ Taginfo uses the following Javascript libraries: * jQuery UI 1.9.2 * customSelect (http://adam.co/lab/jquery/customselect/) * tipsy (http://onehackoranother.com/projects/jquery/tipsy/) +* jQuery Cookie (https://github.com/carhartl/jquery-cookie/) * Flexigrid (Originally from http://code.google.com/p/flexigrid/ http://www.flexigrid.info/ , the version used and included has some changes and bugfixes.) diff --git a/web/i18n/de.yml b/web/i18n/de.yml index b3ae308..574715b 100644 --- a/web/i18n/de.yml +++ b/web/i18n/de.yml @@ -317,6 +317,14 @@ flexigrid: nomsg: Keine Einträge errormsg: Fehler in der Verbindung +comparison: + list: Vergleichsliste + items: Einträge + add_key: Diesen Key hinzufügen + add_tag: Diesen Tag hinzufügen + clear: Vergleichsliste löschen + compare: Keys/Tags vergleichen + sources: master: name: Master diff --git a/web/i18n/en.yml b/web/i18n/en.yml index c8356a3..eba5c97 100644 --- a/web/i18n/en.yml +++ b/web/i18n/en.yml @@ -313,6 +313,14 @@ flexigrid: nomsg: No items errormsg: Connection Error +comparison: + list: Comparison list + items: items + add_key: Add this key to list + add_tag: Add this tag to list + clear: Clear comparison list + compare: Compare keys/tags + sources: master: name: Master diff --git a/web/lib/javascript.rb b/web/lib/javascript.rb index 5cf0a5a..cb73ff7 100644 --- a/web/lib/javascript.rb +++ b/web/lib/javascript.rb @@ -18,6 +18,7 @@ class Javascript @@js_files = { # :common => [ 'jquery-1.9.0.min', 'jquery-ui-1.9.2.custom.min', 'customSelect.jquery.min', 'jquery.tipsy-minified' ], :common => [ 'common' ], + :cookie => [ 'jquery.cookie-minified' ], :taginfo => [ 'taginfo' ], :flexigrid => [ 'jquery-migrate-1.0.0.min', 'flexigrid-minified' ], :d3 => [ 'd3/d3.v3.min' ], diff --git a/web/lib/ui/comparison.rb b/web/lib/ui/comparison.rb index 611d62e..4bc5d46 100644 --- a/web/lib/ui/comparison.rb +++ b/web/lib/ui/comparison.rb @@ -15,7 +15,7 @@ class Taginfo < Sinatra::Base params[:key].each_with_index do |key, index| @data << { :key => key, - :value => (params[:value].is_a?(Array) ? params[:value][index] : nil) + :value => (params[:value].is_a?(Array) ? (params[:value][index] == '' ? nil : params[:value][index]) : nil) } end end diff --git a/web/lib/ui/keys.rb b/web/lib/ui/keys.rb index 1ffc8eb..5b8af23 100644 --- a/web/lib/ui/keys.rb +++ b/web/lib/ui/keys.rb @@ -52,7 +52,7 @@ class Taginfo < Sinatra::Base @img_width = TaginfoConfig.get('geodistribution.width') * TaginfoConfig.get('geodistribution.scale_image') @img_height = TaginfoConfig.get('geodistribution.height') * TaginfoConfig.get('geodistribution.scale_image') - javascript_for(:flexigrid, :d3) + javascript_for(:flexigrid, :cookie, :d3) javascript "#{ r18n.locale.code }/key" erb :key end diff --git a/web/lib/ui/tags.rb b/web/lib/ui/tags.rb index 4250b33..4ba184c 100644 --- a/web/lib/ui/tags.rb +++ b/web/lib/ui/tags.rb @@ -60,7 +60,7 @@ class Taginfo < Sinatra::Base @img_width = TaginfoConfig.get('geodistribution.width') * TaginfoConfig.get('geodistribution.scale_image') @img_height = TaginfoConfig.get('geodistribution.height') * TaginfoConfig.get('geodistribution.scale_image') - javascript_for(:flexigrid) + javascript_for(:flexigrid, :cookie) javascript "#{ r18n.locale.code }/tag" erb :tag end diff --git a/web/public/css/taginfo.css b/web/public/css/taginfo.css index 7dd4e2f..84a1f14 100644 --- a/web/public/css/taginfo.css +++ b/web/public/css/taginfo.css @@ -338,6 +338,17 @@ span.char { border-radius: 4px; } +form#list-form { + margin: 0 20px 0 0; + float: right; +} + +select#list { + background-color: #ffffff; + color: #083e76; + width: 200px; +} + form#filter-form { margin-top: 0px; clear: both; diff --git a/web/public/js/jquery.cookie-minified.js b/web/public/js/jquery.cookie-minified.js new file mode 100644 index 0000000..b8b4c70 --- /dev/null +++ b/web/public/js/jquery.cookie-minified.js @@ -0,0 +1,8 @@ +/* + * jQuery Cookie Plugin v1.4.1 + * https://github.com/carhartl/jquery-cookie + * + * Copyright 2013 Klaus Hartl + * Released under the MIT license + */ +(function(a){if(typeof define==="function"&&define.amd){define(["jquery"],a)}else{if(typeof exports==="object"){a(require("jquery"))}else{a(jQuery)}}}(function(f){var a=/\+/g;function d(i){return b.raw?i:encodeURIComponent(i)}function g(i){return b.raw?i:decodeURIComponent(i)}function h(i){return d(b.json?JSON.stringify(i):String(i))}function c(i){if(i.indexOf('"')===0){i=i.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\")}try{i=decodeURIComponent(i.replace(a," "));return b.json?JSON.parse(i):i}catch(j){}}function e(j,i){var k=b.raw?j:c(j);return f.isFunction(i)?i(k):k}var b=f.cookie=function(q,p,v){if(p!==undefined&&!f.isFunction(p)){v=f.extend({},b.defaults,v);if(typeof v.expires==="number"){var r=v.expires,u=v.expires=new Date();u.setTime(+u+r*86400000)}return(document.cookie=[d(q),"=",h(p),v.expires?"; expires="+v.expires.toUTCString():"",v.path?"; path="+v.path:"",v.domain?"; domain="+v.domain:"",v.secure?"; secure":""].join(""))}var w=q?undefined:{};var s=document.cookie?document.cookie.split("; "):[];for(var o=0,m=s.length;o<m;o++){var n=s[o].split("=");var j=g(n.shift());var k=n.join("=");if(q&&q===j){w=e(k,p);break}if(!q&&(k=e(k))!==undefined){w[j]=k}}return w};b.defaults={};f.removeCookie=function(j,i){if(f.cookie(j)===undefined){return false}f.cookie(j,"",f.extend({},i,{expires:-1}));return !f.cookie(j)}}));
\ No newline at end of file diff --git a/web/public/js/jquery.cookie.js b/web/public/js/jquery.cookie.js new file mode 100644 index 0000000..c7f3a59 --- /dev/null +++ b/web/public/js/jquery.cookie.js @@ -0,0 +1,117 @@ +/*! + * jQuery Cookie Plugin v1.4.1 + * https://github.com/carhartl/jquery-cookie + * + * Copyright 2013 Klaus Hartl + * Released under the MIT license + */ +(function (factory) { + if (typeof define === 'function' && define.amd) { + // AMD + define(['jquery'], factory); + } else if (typeof exports === 'object') { + // CommonJS + factory(require('jquery')); + } else { + // Browser globals + factory(jQuery); + } +}(function ($) { + + var pluses = /\+/g; + + function encode(s) { + return config.raw ? s : encodeURIComponent(s); + } + + function decode(s) { + return config.raw ? s : decodeURIComponent(s); + } + + function stringifyCookieValue(value) { + return encode(config.json ? JSON.stringify(value) : String(value)); + } + + function parseCookieValue(s) { + if (s.indexOf('"') === 0) { + // This is a quoted cookie as according to RFC2068, unescape... + s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); + } + + try { + // Replace server-side written pluses with spaces. + // If we can't decode the cookie, ignore it, it's unusable. + // If we can't parse the cookie, ignore it, it's unusable. + s = decodeURIComponent(s.replace(pluses, ' ')); + return config.json ? JSON.parse(s) : s; + } catch(e) {} + } + + function read(s, converter) { + var value = config.raw ? s : parseCookieValue(s); + return $.isFunction(converter) ? converter(value) : value; + } + + var config = $.cookie = function (key, value, options) { + + // Write + + if (value !== undefined && !$.isFunction(value)) { + options = $.extend({}, config.defaults, options); + + if (typeof options.expires === 'number') { + var days = options.expires, t = options.expires = new Date(); + t.setTime(+t + days * 864e+5); + } + + return (document.cookie = [ + encode(key), '=', stringifyCookieValue(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('')); + } + + // Read + + var result = key ? undefined : {}; + + // To prevent the for loop in the first place assign an empty array + // in case there are no cookies at all. Also prevents odd result when + // calling $.cookie(). + var cookies = document.cookie ? document.cookie.split('; ') : []; + + for (var i = 0, l = cookies.length; i < l; i++) { + var parts = cookies[i].split('='); + var name = decode(parts.shift()); + var cookie = parts.join('='); + + if (key && key === name) { + // If second argument (value) is a function it's a converter... + result = read(cookie, value); + break; + } + + // Prevent storing a cookie that we couldn't decode. + if (!key && (cookie = read(cookie)) !== undefined) { + result[name] = cookie; + } + } + + return result; + }; + + config.defaults = {}; + + $.removeCookie = function (key, options) { + if ($.cookie(key) === undefined) { + return false; + } + + // Must not alter options, thus extending a fresh object... + $.cookie(key, '', $.extend({}, options, { expires: -1 })); + return !$.cookie(key); + }; + +})); diff --git a/web/public/js/taginfo.js b/web/public/js/taginfo.js index 47d949e..abb8589 100644 --- a/web/public/js/taginfo.js +++ b/web/public/js/taginfo.js @@ -443,7 +443,6 @@ function create_flexigrid(domid, options) { }); jQuery('div.bDiv:visible').bind('click', function(event) { var row = jQuery(event.target).parents('tr'); - console.log("click", row); jQuery('div.bDiv:visible tr').removeClass('trOver'); jQuery(row).addClass('trOver'); }); @@ -583,6 +582,71 @@ function open_help() { /* ============================ */ +function get_comparison_list() { + return jQuery.cookie('taginfo_comparison_list') || []; +} + +function set_comparison_list(list) { + jQuery.cookie('taginfo_comparison_list', list, { expires: 1, path: '/' }); +} + +function comparison_list_update() { + var cl = jQuery('#list option:first').html(); + cl = cl.replace(/([0-9]+)/, String(get_comparison_list().length)); + jQuery('#list option:first').html(cl); + jQuery('#list').val('title').change(); +} + +function comparison_list_item_clean(text) { + return text === null || text.match(/^[a-zA-Z:_]+$/) !== null; +} + +function comparison_list_url(list) { + var okay = true; + jQuery.each(list, function(index, item) { + if (!comparison_list_item_clean(item[0]) || + !comparison_list_item_clean(item[1])) { + okay = false; + } + }); + + if (okay) { + return '/comparison/' + jQuery.map(list, function(item, i) { + return item[0] + (item[1] === null ? '' : ('=' + item[1])); + }).join('/'); + } else { + var keys = []; + var values = []; + jQuery.each(list, function(index, item) { + keys.push(item[0]); + values.push(item[1] === null ? '' : item[1]); + }); + return '/comparison/?' + jQuery.param({ 'key': keys, 'value': values }); + } +} + +function comparison_list_change(key, value) { + var list = get_comparison_list(), + command = jQuery('#list').val(); + + if (command == 'title') { + return true; + } else if (command == 'add') { + list.push([key, value]); + set_comparison_list(list); + } else if (command == 'clear') { + set_comparison_list([]); + } else if (command == 'compare') { + window.location = comparison_list_url(list); + } + + comparison_list_update(); + return false; +} + + +/* ============================ */ + jQuery(document).ready(function() { jQuery('#javascriptmsg').remove(); diff --git a/web/views/key.erb b/web/views/key.erb index d168aa4..c2f0cf4 100644 --- a/web/views/key.erb +++ b/web/views/key.erb @@ -8,6 +8,14 @@ <option value="relations"<%= @sel['relations'] %>><%= h(t.pages.key.filter.relations) %></option> </select> </form> + <form id="list-form"> + <select id="list" name="list"> + <option value="title" selected="selected" style="color: #000000"><%= h(t.comparison.list) %> (0 <%= h(t.comparison.items) %>)</option> + <option value="add"><%= h(t.comparison.add_key) %></option> + <option value="clear"><%= h(t.comparison.clear) %></option> + <option value="compare"><%= h(t.comparison.compare) %></option> + </select> + </form> <div id="tools" class="no-print"> <% if @count_all_values <= TaginfoConfig.get('xapi.max_results', 1000) %> <%= xapi_link(@filter_xapi, @key) %> <%= josm_link(@filter_xapi, @key) %> @@ -113,6 +121,13 @@ function page_init2() { var filter = #{ @filter_type.to_json }, key = #{ @key.to_json }; jQuery('h1').html(fmt_key(key)); + + jQuery.cookie.json = true; + comparison_list_update(); + jQuery('#list').bind('change', function() { + comparison_list_change(key); + }); + jQuery('#level0_button').bind('click', function() { return level0_editor('#{ TaginfoConfig.get('level0.overpass_url_prefix') }', '#{ TaginfoConfig.get('level0.level0_url_prefix') }', filter, key, undefined); }); diff --git a/web/views/tag.erb b/web/views/tag.erb index b1758fa..9397d91 100644 --- a/web/views/tag.erb +++ b/web/views/tag.erb @@ -8,6 +8,14 @@ <option value="relations"<%= @sel['relations'] %>><%= h(t.pages.key.filter.relations) %></option> </select> </form> + <form id="list-form"> + <select id="list" name="list"> + <option value="title" selected="selected" style="color: #000000"><%= h(t.comparison.list) %> (0 <%= h(t.comparison.items) %>)</option> + <option value="add"><%= h(t.comparison.add_tag) %></option> + <option value="clear"><%= h(t.comparison.clear) %></option> + <option value="compare"><%= h(t.comparison.compare) %></option> + </select> + </form> <div id="tools"> <% if @count_all < TaginfoConfig.get('xapi.max_results', 1000) %> <%= xapi_link(@filter_xapi, @key, @value) %> <%= josm_link(@filter_xapi, @key, @value) %> @@ -112,6 +120,13 @@ function page_init2() { value = #{ @value.to_json }; up = function() { window.location = url_for_key(key); }; jQuery('h1').html(link_to_key(key) + '=' + fmt_value(value)); + + jQuery.cookie.json = true; + comparison_list_update(); + jQuery('#list').bind('change', function() { + comparison_list_change(key, value); + }); + jQuery('span#keylink').html(link_to_key(key)); jQuery('span#relationlink').html(link_to_rtype(value)); jQuery('#level0_button').bind('click', function() { |