From 0c0271ae34b4861ff5eac8bb3592534876caafb9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 18 Apr 2016 20:35:40 -0700 Subject: Tag sorting and sanitation --- ext/bg/js/templates.js | 19 ++++++------------- ext/bg/js/translator.js | 46 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 21 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index ae7c4a0d..d77f09f6 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -77,23 +77,16 @@ templates['term.html'] = template({"1":function(container,depth0,helpers,partial + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(11, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n"; },"11":function(container,depth0,helpers,partials,data) { - var stack1, helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; + var helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; - return " " - + alias4(((helper = (helper = helpers.key || (data && data.key)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"key","hash":{},"data":data}) : helper))) + + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) + "\n"; -},"12":function(container,depth0,helpers,partials,data) { - var helper; - - return "tag-" - + container.escapeExpression(((helper = (helper = helpers["class"] || (depth0 != null ? depth0["class"] : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"class","hash":{},"data":data}) : helper))); -},"14":function(container,depth0,helpers,partials,data) { - return "tag-default"; -},"16":function(container,depth0,helpers,partials,data) { +},"13":function(container,depth0,helpers,partials,data) { return "
  • " + container.escapeExpression(container.lambda(depth0, depth0)) + "
  • \n"; @@ -106,7 +99,7 @@ templates['term.html'] = template({"1":function(container,depth0,helpers,partial + "\n" + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(10, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n
    \n
      \n" - + ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(16, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(13, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n
    \n"; },"useData":true}); templates['term-list.html'] = template({"1":function(container,depth0,helpers,partials,data) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index e2d15e46..eeeea540 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -103,8 +103,8 @@ class Translator { return 1; } - const p1 = v1.tags.hasOwnProperty('P'); - const p2 = v2.tags.hasOwnProperty('P'); + const p1 = v1.popular; + const p2 = v2.popular; if (p1 && !p2) { return -1; } else if (!p1 && p2) { @@ -158,22 +158,52 @@ class Translator { } } - let tagInfo = {}; + let popular = false; + let tagItems = []; for (const tag of entry.tags) { - const info = this.tags[tag]; - if (info) { - tagInfo[tag] = info; + const tagItem = this.tags[tag]; + if (tagItem) { + tagItems.push({ + class: tagItem.class || 'default', + order: tagItem.order || Number.MAX_SAFE_INTEGER, + name: tag + }); + } + if (tag === 'P') { + popular = true; } } + tagItems = tagItems.sort((v1, v2) => { + const order1 = v1.order; + const order2 = v2.order; + if (order1 < order2) { + return -1; + } else if (order1 > order2) { + return 1; + } + + const name1 = v1.name; + const name2 = v2.name; + if (name1 < name2) { + return -1; + } else if (name1 > name2) { + return 1; + } + + return 0; + }); + + if (matched) { groups[entry.id] = { expression: entry.expression, reading: entry.reading, glossary: entry.glossary, - tags: tagInfo, + tags: tagItems, source: source, - rules: rules + rules: rules, + popular: popular }; } } -- cgit v1.2.3