Mercurial > code > home > repos > href
comparison static/links.js @ 24:ab9a6132529a
redo siteRoot handling. fix some pathing bugs.
Ignore-this: 30fa1b320f62623e9b6a683f81d842a5
author | drewp@bigasterisk.com |
---|---|
date | Mon, 23 May 2016 23:39:24 -0700 |
parents | fa55f4439977 |
children |
comparison
equal
deleted
inserted
replaced
23:d6a09e8efa56 | 24:ab9a6132529a |
---|---|
2 | 2 |
3 var model = { | 3 var model = { |
4 filterTags: ko.observableArray(tagsFromWindowLocation()) | 4 filterTags: ko.observableArray(tagsFromWindowLocation()) |
5 }; | 5 }; |
6 | 6 |
7 function componentsAfterRoot() { | |
8 var p = window.location.href; | |
9 return comps = p.substr(toRoot.length + 1).split("/"); | |
10 } | |
11 | |
7 function tagsFromWindowLocation() { | 12 function tagsFromWindowLocation() { |
8 var p = window.location.pathname; | 13 var comps = componentsAfterRoot(); |
9 var comps = p.split("/"); | 14 comps.shift(); |
10 if (toRoot == ".") { | 15 if (!comps.length) { |
11 return []; | 16 return []; |
12 } else { | |
13 return (comps[comps.length-1] || "").replace("%20", "+").split("+"); | |
14 } | 17 } |
18 var tags = comps[0].replace("%20", "+").split("+"); | |
19 return tags.filter(function(t) { return t != ""; }); | |
15 } | 20 } |
16 | 21 |
17 function toggleTag(tag) { | 22 function toggleTag(tag) { |
18 var selected = model.filterTags(); | 23 var selected = model.filterTags(); |
19 | 24 |
38 }); | 43 }); |
39 } | 44 } |
40 | 45 |
41 var linklist = null; | 46 var linklist = null; |
42 // unsure how to use toRoot- can it change? | 47 // unsure how to use toRoot- can it change? |
43 $.getJSON("/href/templates", function (result) { | 48 $.getJSON(toRoot + "/templates", function (result) { |
44 linklist = result.linklist; | 49 linklist = result.linklist; |
45 }); | 50 }); |
51 | |
52 function pathFromUserAndTags(tags) { | |
53 var comps = componentsAfterRoot(); | |
54 var newPath = comps[0]; | |
55 if (tags.length) { | |
56 newPath += '/' + tags.join('+'); | |
57 } | |
58 return newPath; | |
59 } | |
46 | 60 |
47 function initUrlSync(model) { | 61 function initUrlSync(model) { |
48 // tag changes push url history; and url edits freshen the page | 62 // tag changes push url history; and url edits freshen the page |
49 | 63 |
50 ko.computed(function () { | 64 ko.computed(function () { |
51 var tags = model.filterTags(); | 65 var tags = model.filterTags(); |
52 var newPath = window.location.pathname; | 66 var newPath = pathFromUserAndTags(tags); |
53 if (toRoot == ".") { | |
54 newPath += "/"; | |
55 toRoot = ".."; | |
56 } else { | |
57 newPath = newPath.replace( | |
58 /(.*\/)[^\/]*$/, "$1") | |
59 } | |
60 if (tags.length) { | |
61 newPath += tags.join("+") | |
62 } else { | |
63 newPath = newPath.substr(0, newPath.length - 1); | |
64 toRoot = "."; | |
65 } | |
66 | |
67 changePage(newPath); | 67 changePage(newPath); |
68 }); | 68 }); |
69 | 69 |
70 function changePage(newPath) { | 70 function changePage(newPath) { |
71 if (window.location.pathname != newPath) { | 71 if (componentsAfterRoot().join('/') != newPath) { |
72 window.history.pushState({}, "", newPath); | 72 window.history.pushState({}, "", toRoot + '/' + newPath); |
73 | 73 |
74 function updateLinklist(fullPath) { | 74 function updateLinklist(fullPath) { |
75 var t0 = +new Date(); | 75 var t0 = +new Date(); |
76 if (linklist === null) { | 76 if (linklist === null) { |
77 console.log("too soon- templates aren't loaded"); | 77 console.log("too soon- templates aren't loaded"); |
78 return; | 78 return; |
79 } | 79 } |
80 $(".linklist").text("Loading..."); | 80 $(".linklist").text("Loading..."); |
81 $.getJSON(fullPath + ".json", function (result) { | 81 $.getJSON(toRoot + '/' + fullPath + ".json", function (result) { |
82 var t1 = +new Date(); | 82 var t1 = +new Date(); |
83 var rendered = Mustache.render(linklist, result) | 83 var rendered = Mustache.render(linklist, result) |
84 var t2 = +new Date(); | 84 var t2 = +new Date(); |
85 $(".linklist").html(rendered); | 85 $(".linklist").html(rendered); |
86 var t3 = +new Date(); | 86 var t3 = +new Date(); |
99 function initFilterTag(elem, model) { | 99 function initFilterTag(elem, model) { |
100 // sync the entry box and the model | 100 // sync the entry box and the model |
101 | 101 |
102 elem.change(function () { | 102 elem.change(function () { |
103 var tags = $(this).val().split(","); | 103 var tags = $(this).val().split(","); |
104 model.filterTags(tags); | 104 model.filterTags(tags.filter(function(t) { return t != ""; })); |
105 return false; | 105 return false; |
106 }); | 106 }); |
107 | 107 |
108 var filterCompleteWords = ""; | 108 var filterCompleteWords = ""; |
109 elem.select2({ | 109 elem.select2({ |