annotate static/links.js @ 10:e054949143e9

reworking addlink and shareWith support Ignore-this: b1665b776f3964f7fde219acadc51f32
author drewp@bigasterisk.com
date Fri, 15 Mar 2013 00:29:53 -0700
parents
children a8887fb93676
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
1
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
2 $("#filterTag").focus();
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
3
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
4 var model = {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
5 filterTags: ko.observableArray(currentFilter())
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
6 };
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
7
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
8 function currentFilter() {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
9 var p = window.location.pathname;
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
10 var comps = p.split("/");
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
11 if (toRoot == ".") {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
12 return [];
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
13 } else {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
14 return (comps[comps.length-1] || "").split("+");
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
15 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
16 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
17
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
18 function toggleTag(tag) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
19 var selected = currentFilter();
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
20
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
21 if (selected.indexOf(tag) == -1) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
22 selected.push(tag);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
23 } else {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
24 selected.splice(selected.indexOf(tag), 1);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
25 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
26 setPageTags(selected);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
27 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
28
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
29 function setPageTags(tags) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
30
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
31 var newPath = window.location.pathname;
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
32 if (toRoot == ".") {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
33 newPath += "/";
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
34 } else {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
35 newPath = newPath.replace(
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
36 /(.*\/)[^\/]*$/, "$1")
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
37 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
38 console.log("user root", newPath);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
39 if (tags.length) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
40 newPath += tags.join("+")
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
41 } else {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
42 newPath = newPath.substr(0, newPath.length - 1);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
43 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
44 console.log("done", newPath);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
45
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
46 window.location.pathname = newPath;
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
47 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
48
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
49 function backspaceLastTag() {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
50 var p = window.location.pathname;
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
51 var comps = p.split("/");
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
52 var selected = (comps[comps.length-1] || "").split("+");
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
53 if (selected.length == 0) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
54 return;
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
55 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
56 toggleTag(selected[selected.length-1]);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
57 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
58
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
59 $("a.tag").click(function () {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
60 var tag = $(this).text();
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
61 toggleTag(tag);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
62 return false;
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
63 });
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
64
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
65 $("#filterTag").change(function () {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
66 var tags = $(this).val().split(",");
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
67 setPageTags(tags);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
68 return false;
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
69 });
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
70
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
71 var filterCompleteWords = "";
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
72 $("#filterTag").select2({
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
73 allowClear: true,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
74 multiple: true,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
75 tokenSeparators: [' ', ','],
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
76 query: function (opts) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
77 $.ajax({
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
78 url: toRoot + "/tags",
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
79 data: {user: user, have: opts.element.val()},
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
80 success: function (data) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
81 opts.callback({results: data.tags});
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
82 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
83 });
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
84 },
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
85 change: function (ev) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
86 console.log("ch", ev.val);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
87 },
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
88 initSelection: function (element, callback) {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
89 var data = [];
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
90 $(element.val().split(",")).each(function () {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
91 data.push({id: this, text: this});
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
92 });
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
93 callback(data);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
94 }
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
95 });
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
96 $("#filterTag").select2("val", model.filterTags());
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
97
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
diff changeset
98 ko.applyBindings(model);