Mercurial > projects > booket
annotate booket.js @ 6:e9ad4c625b7a
Add bookmarklet for bookmarking
Add a bookmarklet which gathers the URL and title of the current page which
can then be copied and pasted into Booket
author | Guido Berhoerster <guido+booket@berhoerster.name> |
---|---|
date | Wed, 10 Sep 2014 19:45:23 +0200 |
parents | 39c95b9826d2 |
children | a1a934adff8d |
rev | line source |
---|---|
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1 /* |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
2 * Copyright (C) 2014 Guido Berhoerster <guido+booket@berhoerster.name> |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
3 * |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
4 * Permission is hereby granted, free of charge, to any person obtaining |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
5 * a copy of this software and associated documentation files (the |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
6 * "Software"), to deal in the Software without restriction, including |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
7 * without limitation the rights to use, copy, modify, merge, publish, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
8 * distribute, sublicense, and/or sell copies of the Software, and to |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
9 * permit persons to whom the Software is furnished to do so, subject to |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
10 * the following conditions: |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
11 * |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
12 * The above copyright notice and this permission notice shall be included |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
13 * in all copies or substantial portions of the Software. |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
14 * |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
22 */ |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
23 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
24 (function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
25 'use strict'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
26 |
6
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
27 var BOOKMARKLET_URI = |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
28 'javascript:(function () {' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
29 '\'use strict\';' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
30 '' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
31 'window.alert(\'Copy the following data and paste it into \' +' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
32 '\'Booket:\\n\\n\' + JSON.stringify({' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
33 '\'url\': document.URL,' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
34 '\'title\': document.title' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
35 '}));' + |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
36 '}) ();'; |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
37 |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
38 |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
39 /* |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
40 * utility stuff |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
41 */ |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
42 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
43 function isNumber(number) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
44 return (Object.prototype.toString.call(number) === '[object Number]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
45 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
46 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
47 function isString(number) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
48 return (Object.prototype.toString.call(number) === '[object String]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
49 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
50 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
51 function arrayEqual(array1, array2) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
52 if (!Array.isArray(array1)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
53 throw new TypeError(typeof array1 + ' is not an array'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
54 } else if (!Array.isArray(array2)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
55 throw new TypeError(typeof array2 + ' is not an array'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
56 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
57 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
58 if (array1.length !== array2.length) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
59 return false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
60 } else if (array1.length === 0 && array2.length === 0) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
61 return true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
62 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
63 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
64 return array1.slice().sort().every(function (value, i) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
65 return value === array2[i]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
66 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
67 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
68 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
69 function parseHash(url) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
70 var hashData; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
71 var pos; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
72 var hash; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
73 var hashParts; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
74 var key; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
75 var value; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
76 var i; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
77 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
78 hashData = new StringMap(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
79 pos = url.indexOf('#'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
80 hash = (pos > -1) ? url.substr(pos + 1) : ''; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
81 // hash parts are seperated by a ';' |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
82 hashParts = hash.split(';'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
83 for (i = 0; i < hashParts.length; i++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
84 // key and value pairs are seperated by a '=', an empty value will |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
85 // cause the key to be ignored |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
86 pos = hashParts[i].indexOf('='); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
87 if (pos > -1) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
88 key = decodeURIComponent(hashParts[i].substr(0, pos)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
89 value = decodeURIComponent(hashParts[i].substr(pos + 1)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
90 hashData.set(key, value); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
91 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
92 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
93 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
94 return hashData; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
95 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
96 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
97 function serializeHash(url, hashData) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
98 var hashParts = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
99 var pos; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
100 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
101 pos = url.indexOf('#'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
102 if (pos > -1) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
103 url = url.substr(0, pos); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
104 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
105 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
106 hashData.forEach(function (value, key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
107 if (value !== '') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
108 hashParts.push(encodeURIComponent(key) + '=' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
109 encodeURIComponent(value)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
110 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
111 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
112 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
113 // only append a '#' if there are any hash parts |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
114 return url + (hashParts.length > 0 ? '#' + hashParts.join(';') : ''); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
115 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
116 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
117 function getAncestorElementDatasetItem(node, item) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
118 while ((node = node.parentNode) !== null) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
119 if (node.dataset && node.dataset[item] !== undefined) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
120 return node.dataset[item]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
121 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
122 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
123 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
124 return undefined; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
125 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
126 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
127 // for use with Node.querySelector() and Node.querySelectorAll() |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
128 function createDatasetSelector(name, value) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
129 return '[data-' + name + '="' + value.replace(/["\\]/g, '\\$&') + '"]'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
130 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
131 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
132 function extend(targetObject, sourceObject) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
133 var propertyName; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
134 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
135 for (propertyName in sourceObject.prototype) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
136 if (!Object.prototype.hasOwnProperty.call(targetObject.prototype, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
137 propertyName)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
138 targetObject.prototype[propertyName] = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
139 sourceObject.prototype[propertyName]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
140 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
141 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
142 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
143 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
144 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
145 var ObservableMixin = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
146 this._eventsObservers = {}; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
147 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
148 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
149 ObservableMixin.prototype.addObserver = function (eventName, observer) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
150 var i; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
151 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
152 if (!Object.prototype.hasOwnProperty.call(this._eventsObservers, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
153 eventName)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
154 this._eventsObservers[eventName] = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
155 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
156 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
157 // prevent observers for an event from being called more than once |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
158 for (i = 0; i < this._eventsObservers[eventName].length; i++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
159 if (this._eventsObservers[eventName][i] === observer) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
160 return; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
161 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
162 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
163 this._eventsObservers[eventName].push(observer); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
164 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
165 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
166 ObservableMixin.prototype.deleteObserver = function (eventName, observer) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
167 var i = 0; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
168 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
169 if (!Object.prototype.hasOwnProperty.call(this._eventsObservers, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
170 eventName)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
171 return; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
172 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
173 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
174 while (i < this._eventsObservers[eventName].length) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
175 if (this._eventsObservers[eventName][i] === observer) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
176 this._eventsObservers[eventName].splice(i, 1); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
177 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
178 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
179 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
180 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
181 ObservableMixin.prototype.notify = function (eventName) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
182 var origArguments; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
183 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
184 if (!Object.prototype.hasOwnProperty.call(this._eventsObservers, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
185 eventName)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
186 return; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
187 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
188 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
189 origArguments = Array.prototype.slice.call(arguments, 1); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
190 this._eventsObservers[eventName].forEach(function (observer, i) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
191 // call the observer function and pass on any additional arguments |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
192 observer.apply(undefined, origArguments); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
193 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
194 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
195 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
196 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
197 var StringMap = function (iter) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
198 this._stringMap = Object.create(null); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
199 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
200 if (iter !== undefined) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
201 if (Array.isArray(iter)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
202 iter.forEach(function (pair) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
203 if (Array.isArray(pair)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
204 this.set(pair[0], pair[1]); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
205 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
206 throw new TypeError(typeof pair + ' is not an array'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
207 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
208 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
209 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
210 throw new TypeError(typeof iter + ' is not iterable'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
211 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
212 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
213 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
214 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
215 Object.defineProperty(StringMap.prototype, 'size', { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
216 get: function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
217 var size = 0; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
218 var key; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
219 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
220 for (key in this._stringMap) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
221 if (key.charAt(0) === '@') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
222 size++; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
223 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
224 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
225 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
226 return size; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
227 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
228 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
229 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
230 StringMap.prototype.set = function (key, value) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
231 this._stringMap['@' + key] = value; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
232 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
233 return this; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
234 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
235 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
236 StringMap.prototype.get = function (key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
237 return this._stringMap['@' + key]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
238 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
239 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
240 StringMap.prototype.has = function (key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
241 return (('@' + key) in this._stringMap); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
242 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
243 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
244 StringMap.prototype.delete = function (key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
245 if (this.has(key)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
246 delete this._stringMap['@' + key]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
247 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
248 return true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
249 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
250 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
251 return false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
252 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
253 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
254 StringMap.prototype.forEach = function (callbackFn, thisArg) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
255 Object.keys(this._stringMap).forEach(function (key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
256 if (key.charAt(0) === '@') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
257 key = key.substr(1); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
258 callbackFn.call(thisArg, this.get(key), key, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
259 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
260 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
261 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
262 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
263 StringMap.prototype.keys = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
264 return Object.keys(this._stringMap).map(function (key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
265 return key.substr(1); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
266 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
267 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
268 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
269 StringMap.prototype.toJSON = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
270 return this._stringMap; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
271 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
272 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
273 StringMap.prototype.toString = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
274 return Object.prototype.toString.call(this._stringMap); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
275 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
276 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
277 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
278 var StringSet = function (iter) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
279 this._stringArray = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
280 this._stringMap = new StringMap(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
281 if (iter !== undefined) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
282 if (Array.isArray(iter) || iter instanceof StringSet) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
283 iter.forEach(function (string) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
284 this.add(string); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
285 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
286 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
287 throw new TypeError(typeof iter + ' is not iterable'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
288 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
289 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
290 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
291 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
292 Object.defineProperty(StringSet.prototype, 'size', { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
293 get: function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
294 return this._stringArray.length; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
295 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
296 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
297 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
298 StringSet.prototype.has = function (string) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
299 return this._stringMap.has(string); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
300 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
301 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
302 StringSet.prototype.add = function (string) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
303 if (!this.has(string)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
304 this._stringMap.set(string, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
305 this._stringArray.push(string); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
306 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
307 return this; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
308 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
309 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
310 StringSet.prototype.delete = function (string) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
311 if (this.has(string)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
312 this._stringMap.delete(string); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
313 this._stringArray.splice(this._stringArray.indexOf(string), 1); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
314 return true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
315 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
316 return false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
317 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
318 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
319 StringSet.prototype.forEach = function (callbackFn, thisArg) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
320 this._stringArray.forEach(function (key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
321 callbackFn.call(thisArg, key, key, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
322 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
323 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
324 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
325 StringSet.prototype.keys = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
326 return this._stringArray.slice(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
327 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
328 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
329 StringSet.prototype.values = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
330 return this._stringArray.slice(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
331 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
332 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
333 StringSet.prototype.clear = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
334 this._stringMap = new StringMap(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
335 this._stringArray = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
336 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
337 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
338 StringSet.prototype.toJSON = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
339 return this._stringArray; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
340 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
341 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
342 StringSet.prototype.toString = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
343 return this._stringArray.toString(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
344 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
345 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
346 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
347 /* |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
348 * model |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
349 */ |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
350 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
351 var Bookmark = function (url, title, tags, ctime, mtime) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
352 var parsedTime; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
353 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
354 if (!isString(url)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
355 throw new TypeError(typeof url + ' is not a string'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
356 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
357 this.url = url; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
358 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
359 this.title = (isString(title) && title !== '') ? title : url; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
360 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
361 if (Array.isArray(tags)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
362 // remove duplicates, non-string or empty tags and tags containing |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
363 // commas |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
364 this.tags = new StringSet(tags.filter(function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
365 return (isString(tag) && tag !== '' && tag.indexOf(',') === -1); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
366 }).sort()); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
367 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
368 this.tags = new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
369 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
370 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
371 if (isNumber(ctime) || isString(ctime)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
372 parsedTime = new Date(ctime); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
373 this.ctime = !isNaN(parsedTime.getTime()) ? parsedTime : new Date(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
374 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
375 this.ctime = new Date(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
376 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
377 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
378 if (isNumber(mtime) || isString(mtime)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
379 parsedTime = new Date(mtime); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
380 // modification time must be greater than creation time |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
381 this.mtime = (!isNaN(parsedTime.getTime()) || |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
382 parsedTime >= this.ctime) ? parsedTime : new Date(this.ctime); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
383 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
384 this.mtime = new Date(this.ctime); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
385 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
386 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
387 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
388 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
389 var BookmarkModel = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
390 ObservableMixin.call(this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
391 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
392 this.unsavedChanges = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
393 this._bookmarks = new StringMap(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
394 this._tagCount = new StringMap(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
395 this._filterTags = new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
396 this._searchTerm = ''; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
397 this._filteredBookmarks = new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
398 this._searchedBookmarks = new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
399 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
400 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
401 extend(BookmarkModel, ObservableMixin); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
402 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
403 BookmarkModel.prototype.add = function (bookmarks) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
404 var addedBookmarkUrls = new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
405 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
406 // argument can be a single bookmark or a list of bookmarks |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
407 if (!Array.isArray(bookmarks)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
408 bookmarks = [bookmarks]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
409 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
410 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
411 bookmarks.forEach(function (bookmark) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
412 // delete any existing bookmark for the given URL before adding the new |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
413 // one in order to update views |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
414 this.delete(bookmark.url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
415 this._bookmarks.set(bookmark.url, bookmark); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
416 addedBookmarkUrls.add(bookmark.url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
417 this.unsavedChanges = true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
418 this.notify('bookmark-added', bookmark); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
419 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
420 // update tag count |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
421 bookmark.tags.forEach(function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
422 var tagCount; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
423 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
424 if (this._tagCount.has(tag)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
425 tagCount = this._tagCount.get(tag) + 1; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
426 this._tagCount.set(tag, tagCount); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
427 this.notify('tag-count-changed', tag, tagCount); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
428 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
429 this._tagCount.set(tag, 1); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
430 this.notify('tag-added', tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
431 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
432 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
433 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
434 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
435 // apply tag filter and search added bookmarks |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
436 this.updateFilteredSearchedBookmarks(addedBookmarkUrls); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
437 this.notify('filter-tags-search-changed', this._searchedBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
438 this._filterTags, this._searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
439 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
440 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
441 BookmarkModel.prototype.has = function (url) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
442 return this._bookmarks.has(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
443 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
444 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
445 BookmarkModel.prototype.get = function (url) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
446 return this._bookmarks.get(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
447 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
448 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
449 BookmarkModel.prototype.delete = function (urls) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
450 var needUpdateFilterTags = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
451 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
452 // argument can be a single bookmark or a list of bookmarks |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
453 if (!Array.isArray(urls)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
454 urls = [urls]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
455 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
456 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
457 urls.forEach(function (url) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
458 var bookmark; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
459 var tagCount; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
460 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
461 if (this._bookmarks.has(url)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
462 bookmark = this._bookmarks.get(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
463 this._bookmarks.delete(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
464 this.unsavedChanges = true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
465 this.notify('bookmark-deleted', bookmark.url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
466 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
467 // update tag count |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
468 bookmark.tags.forEach(function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
469 if (this._tagCount.has(tag)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
470 tagCount = this._tagCount.get(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
471 if (tagCount > 1) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
472 tagCount--; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
473 this._tagCount.set(tag, tagCount); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
474 this.notify('tag-count-changed', tag, tagCount); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
475 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
476 this._tagCount.delete(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
477 this.notify('tag-deleted', tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
478 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
479 if (this._filterTags.has(tag)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
480 this._filterTags.delete(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
481 needUpdateFilterTags = true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
482 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
483 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
484 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
485 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
486 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
487 // update filtered and searched bookmarks |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
488 if (this._filteredBookmarks.has(url)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
489 this._filteredBookmarks.delete(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
490 if (this._searchedBookmarks.has(url)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
491 this._searchedBookmarks.delete(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
492 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
493 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
494 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
495 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
496 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
497 if (needUpdateFilterTags) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
498 this.updateFilteredSearchedBookmarks(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
499 this.notify('filter-tags-search-changed', this._searchedBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
500 this._filterTags, this._searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
501 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
502 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
503 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
504 BookmarkModel.prototype.forEach = function (callbackFn, thisArg) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
505 this._bookmarks.keys().forEach(function (key) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
506 callbackFn.call(thisArg, this._bookmarks.get(key), key, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
507 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
508 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
509 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
510 BookmarkModel.prototype.hasTag = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
511 return this._tagCount.has(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
512 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
513 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
514 BookmarkModel.prototype.getTagCount = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
515 return (this._tagCount.has(tag)) ? this._tagCount.get(tag) : undefined; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
516 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
517 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
518 BookmarkModel.prototype.updateSearchedBookmarks = function (urlsSubset) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
519 var searchUrls; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
520 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
521 // additive search if urlsSubset is given |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
522 if (urlsSubset !== undefined) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
523 searchUrls = urlsSubset; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
524 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
525 this._searchedBookmarks = new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
526 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
527 searchUrls = this._filteredBookmarks.values(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
528 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
529 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
530 // search for the search term in title and URL |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
531 searchUrls.forEach(function (url) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
532 var bookmark; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
533 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
534 bookmark = this.get(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
535 if (this._searchTerm === '' || |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
536 bookmark.title.indexOf(this._searchTerm) !== -1 || |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
537 bookmark.url.indexOf(this._searchTerm) !== -1) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
538 this._searchedBookmarks.add(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
539 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
540 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
541 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
542 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
543 BookmarkModel.prototype.updateFilteredSearchedBookmarks = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
544 function (urlsSubset) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
545 var filterUrls; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
546 var searchUrls; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
547 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
548 // additive filtering if urlsSubset is given |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
549 if (urlsSubset !== undefined) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
550 filterUrls = urlsSubset; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
551 searchUrls = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
552 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
553 this._filteredBookmarks = new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
554 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
555 filterUrls = this._bookmarks.keys(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
556 searchUrls = undefined; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
557 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
558 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
559 // apply tag filter |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
560 filterUrls.forEach(function (url) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
561 var bookmark; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
562 var matchingTagCount = 0; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
563 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
564 bookmark = this.get(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
565 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
566 bookmark.tags.forEach(function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
567 if (this._filterTags.has(tag)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
568 matchingTagCount++; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
569 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
570 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
571 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
572 if (matchingTagCount === this._filterTags.size) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
573 this._filteredBookmarks.add(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
574 if (urlsSubset !== undefined) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
575 searchUrls.push(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
576 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
577 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
578 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
579 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
580 // search the filter results |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
581 this.updateSearchedBookmarks(searchUrls); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
582 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
583 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
584 BookmarkModel.prototype.toggleFilterTag = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
585 if (this._filterTags.has(tag)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
586 this._filterTags.delete(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
587 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
588 this._filterTags.add(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
589 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
590 this.updateFilteredSearchedBookmarks(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
591 this.notify('filter-tags-search-changed', this._searchedBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
592 this._filterTags, this._searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
593 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
594 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
595 BookmarkModel.prototype.setFilterTags = function (filterTags) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
596 if (!arrayEqual(filterTags.values(), this._filterTags.values())) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
597 this._filterTags = new StringSet(filterTags); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
598 this.updateFilteredSearchedBookmarks(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
599 this.notify('filter-tags-search-changed', this._searchedBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
600 this._filterTags, this._searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
601 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
602 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
603 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
604 BookmarkModel.prototype.setSearchTerm = function (searchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
605 if (searchTerm !== this._searchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
606 this._searchTerm = searchTerm; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
607 this.updateSearchedBookmarks(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
608 this.notify('filter-tags-search-changed', this._searchedBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
609 this._filterTags, this._searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
610 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
611 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
612 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
613 BookmarkModel.prototype.setFilterTagsSearchTerm = function (filterTags, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
614 searchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
615 if (!arrayEqual(filterTags.values(), this._filterTags.values())) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
616 this._filterTags = new StringSet(filterTags); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
617 this._searchTerm = searchTerm; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
618 this.updateFilteredSearchedBookmarks(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
619 this.notify('filter-tags-search-changed', this._searchedBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
620 this._filterTags, this._searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
621 } else if (searchTerm !== this._searchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
622 this._searchTerm = searchTerm; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
623 this.updateSearchedBookmarks(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
624 this.notify('filter-tags-search-changed', this._searchedBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
625 this._filterTags, this._searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
626 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
627 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
628 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
629 BookmarkModel.prototype.parseLoadedBookmarks = function (data) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
630 var parsedData; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
631 var bookmarks = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
632 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
633 try { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
634 parsedData = JSON.parse(data); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
635 } catch (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
636 this.notify('load-file-error', e.message); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
637 return; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
638 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
639 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
640 if (!Array.isArray(parsedData.bookmarks)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
641 this.notify('parse-file-error', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
642 'This file does not contain bookmarks.'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
643 return; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
644 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
645 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
646 // create a temporary list of valid bookmarks |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
647 parsedData.bookmarks.forEach(function (bookmark) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
648 if (isString(bookmark.url) && bookmark.url !== '') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
649 bookmarks.push(new Bookmark(bookmark.url, bookmark.title, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
650 bookmark.tags, bookmark.ctime, bookmark.mtime)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
651 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
652 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
653 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
654 // add each bookmark to the model ordered by the last modification time |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
655 this.add(bookmarks.sort(function (bookmark1, bookmark2) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
656 return bookmark1.ctime - bookmark2.ctime; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
657 })); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
658 this.unsavedChanges = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
659 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
660 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
661 BookmarkModel.prototype.loadFile = function (bookmarkFile) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
662 var bookmarkFileReader; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
663 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
664 // delete all existing bookmarks first |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
665 this.delete(this._bookmarks.keys()); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
666 this.unsavedChanges = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
667 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
668 bookmarkFileReader = new FileReader(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
669 bookmarkFileReader.addEventListener('error', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
670 bookmarkFileReader.addEventListener('load', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
671 bookmarkFileReader.readAsText(bookmarkFile); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
672 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
673 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
674 BookmarkModel.prototype.saveFile = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
675 var jsonBlob; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
676 var bookmarkData = { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
677 'bookmarks': [] |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
678 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
679 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
680 this._bookmarks.forEach(function (bookmark) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
681 bookmarkData.bookmarks.push(bookmark); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
682 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
683 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
684 jsonBlob = new Blob([JSON.stringify(bookmarkData)], {type: |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
685 'application/json'}); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
686 this.notify('save-file', jsonBlob); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
687 this.unsavedChanges = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
688 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
689 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
690 BookmarkModel.prototype.handleEvent = function (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
691 if (e.type === 'load') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
692 this.parseLoadedBookmarks(e.target.result); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
693 } else if (e.type === 'error') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
694 this.notify('load-file-error', e.target.error.message); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
695 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
696 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
697 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
698 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
699 /* |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
700 * view |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
701 */ |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
702 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
703 var TagView = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
704 ObservableMixin.call(this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
705 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
706 this.tagListElement = document.querySelector('#tags ul.tag-list'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
707 this.tagListElement.addEventListener('click', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
708 |
5
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
709 this.tagDatalistElement = document.querySelector('#tag-datalist'); |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
710 |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
711 this.tagTemplate = document.querySelector('#tag-template'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
712 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
713 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
714 extend(TagView, ObservableMixin); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
715 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
716 TagView.prototype.onTagAdded = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
717 var newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
718 var tagElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
719 var setTagButton; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
720 var toggleTagButton; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
721 var tagElements; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
722 var i; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
723 var referenceTag = ''; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
724 var referenceNode; |
5
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
725 var tagOptionElement; |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
726 var i; |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
727 var isInDatalist = false; |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
728 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
729 // create new tag element from template |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
730 newNode = document.importNode(this.tagTemplate.content, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
731 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
732 tagElement = newNode.querySelector('li'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
733 tagElement.dataset.tag = tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
734 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
735 setTagButton = tagElement.querySelector('button[name="set-tag"]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
736 setTagButton.textContent = tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
737 setTagButton.title = 'Set filter to "' + tag + '"'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
738 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
739 toggleTagButton = tagElement.querySelector('button[name="toggle-tag"]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
740 toggleTagButton.textContent = '+'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
741 toggleTagButton.title = 'Add "' + tag + '" to filter'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
742 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
743 // maintain alphabetical order when inserting the tag element |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
744 tagElements = this.tagListElement.querySelectorAll('li'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
745 for (i = 0; i < tagElements.length; i ++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
746 if (tagElements[i].dataset.tag > referenceTag && |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
747 tagElements[i].dataset.tag < tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
748 referenceTag = tagElements[i].dataset.tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
749 referenceNode = tagElements[i]; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
750 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
751 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
752 this.tagListElement.insertBefore(newNode, (referenceNode !== undefined) ? |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
753 referenceNode.nextSibling : this.tagListElement.firstChild); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
754 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
755 // initialize tag count |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
756 this.onTagCountChanged(tag, 1); |
5
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
757 |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
758 // add to datalist |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
759 for (i = 0; i < this.tagDatalistElement.options.length; i++) { |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
760 if (this.tagDatalistElement.options[i].value == tag) { |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
761 isInDatalist = true; |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
762 break; |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
763 } |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
764 } |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
765 if (!isInDatalist) { |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
766 tagOptionElement = document.createElement('option'); |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
767 tagOptionElement.value = tag; |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
768 this.tagDatalistElement.appendChild(tagOptionElement); |
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
769 } |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
770 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
771 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
772 TagView.prototype.onTagCountChanged = function (tag, tagCount) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
773 this.tagListElement.querySelector('li' + createDatasetSelector('tag', tag) + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
774 ' .tag-count').textContent = '(' + tagCount + ')'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
775 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
776 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
777 TagView.prototype.onTagDeleted = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
778 var tagElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
779 |
5
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
780 // remove from tag list |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
781 tagElement = this.tagListElement.querySelector('li' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
782 createDatasetSelector('tag', tag)); |
5
39c95b9826d2
Add autocompletion for tags
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
3
diff
changeset
|
783 tagElement.parentNode.removeChild(tagElement); |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
784 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
785 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
786 TagView.prototype.onFilterTagsSearchChanged = function (filteredBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
787 newFilterTags, newSearchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
788 var tagElements; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
789 var i; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
790 var tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
791 var toggleTagButton; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
792 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
793 tagElements = this.tagListElement.querySelectorAll('li'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
794 for (i = 0; i < tagElements.length; i++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
795 tag = tagElements[i].dataset.tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
796 toggleTagButton = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
797 tagElements[i].querySelector('button[name="toggle-tag"]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
798 if (newFilterTags.has(tag)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
799 tagElements[i].classList.add('active-filter-tag'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
800 toggleTagButton.textContent = '\u2212'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
801 toggleTagButton.title = 'Remove "' + tag + '" from filter'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
802 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
803 tagElements[i].classList.remove('active-filter-tag'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
804 toggleTagButton.textContent = '+'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
805 toggleTagButton.title = 'Add "' + tag + '" to filter'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
806 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
807 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
808 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
809 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
810 TagView.prototype.handleEvent = function (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
811 if (e.type === 'click' && (e.target.name === 'set-tag' || |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
812 e.target.name === 'toggle-tag')) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
813 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
814 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
815 this.notify(e.target.name, getAncestorElementDatasetItem(e.target, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
816 'tag')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
817 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
818 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
819 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
820 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
821 var ActionsView = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
822 var saveFormElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
823 var loadFormElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
824 var newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
825 var editorFormElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
826 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
827 ObservableMixin.call(this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
828 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
829 this.tagInputTemplate = document.querySelector('#tag-input-template'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
830 saveFormElement = document.querySelector('form#save-form'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
831 saveFormElement.addEventListener('submit', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
832 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
833 this.saveLinkElement = saveFormElement.querySelector('a#save-link'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
834 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
835 loadFormElement = document.querySelector('form#load-form'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
836 loadFormElement.addEventListener('submit', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
837 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
838 // create new editor form from template |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
839 newNode = document.importNode( |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
840 document.querySelector('#bookmark-editor-template').content, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
841 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
842 editorFormElement = newNode.querySelector('form.bookmark-editor-form'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
843 editorFormElement.querySelector('legend').textContent = 'Add Bookmark'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
844 editorFormElement.querySelector('input:not([type="hidden"])').accessKey = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
845 'a'; |
6
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
846 editorFormElement.addEventListener('input', this); |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
847 editorFormElement.addEventListener('click', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
848 editorFormElement.addEventListener('submit', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
849 editorFormElement.addEventListener('reset', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
850 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
851 this.editTagListElement = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
852 editorFormElement.querySelector('ul.tag-input-list'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
853 this.editTagListElement.appendChild(this.createTagInputElement('')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
854 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
855 saveFormElement.parentNode.insertBefore(newNode, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
856 saveFormElement.nextSibling); |
6
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
857 |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
858 document.querySelector('a#bookmarklet-link').href = BOOKMARKLET_URI; |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
859 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
860 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
861 extend(ActionsView, ObservableMixin); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
862 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
863 ActionsView.prototype.createTagInputElement = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
864 var newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
865 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
866 newNode = document.importNode(this.tagInputTemplate.content, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
867 newNode.querySelector('input[name="tag"]').value = tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
868 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
869 return newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
870 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
871 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
872 ActionsView.prototype.handleEvent = function (e) { |
6
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
873 var bookmarkletData; |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
874 var parsedData; |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
875 var tags = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
876 var i; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
877 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
878 switch (e.type) { |
6
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
879 case 'input': |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
880 if (e.target.name === 'bookmarklet-import') { |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
881 // get rid of any preceding text |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
882 bookmarkletData = e.target.value.replace(/^[^{]*/, ''); |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
883 |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
884 try { |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
885 parsedData = JSON.parse(bookmarkletData); |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
886 } catch (exception) { |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
887 return; |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
888 } |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
889 |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
890 if (isString(parsedData.url) && parsedData.url !== '') { |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
891 e.target.form.elements.url.value = parsedData.url; |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
892 } |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
893 if (isString(parsedData.title) && parsedData.title !== '') { |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
894 e.target.form.elements.title.value = parsedData.title; |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
895 } |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
896 } |
e9ad4c625b7a
Add bookmarklet for bookmarking
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
5
diff
changeset
|
897 break; |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
898 case 'click': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
899 if (e.target.name === 'more-tags') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
900 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
901 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
902 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
903 this.editTagListElement.appendChild(this.createTagInputElement('')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
904 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
905 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
906 case 'submit': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
907 if (e.target.id === 'save-form') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
908 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
909 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
910 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
911 this.notify('save-file'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
912 } else if (e.target.id === 'load-form') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
913 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
914 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
915 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
916 this.notify('load-file', e.target.file.files[0]); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
917 e.target.reset(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
918 } else if (e.target.classList.contains('bookmark-editor-form')) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
919 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
920 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
921 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
922 if (e.target.tag.length) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
923 for (i = 0; i < e.target.tag.length; i++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
924 tags.push(e.target.tag[i].value.trim()); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
925 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
926 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
927 tags.push(e.target.tag.value.trim()); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
928 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
929 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
930 this.notify('save-bookmark', e.target.url.value, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
931 e.target.title.value, tags); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
932 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
933 e.target.reset(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
934 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
935 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
936 case 'reset': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
937 if (e.target.classList.contains('bookmark-editor-form')) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
938 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
939 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
940 // remove all but one tag input element |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
941 while (this.editTagListElement.firstChild !== null) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
942 this.editTagListElement.removeChild( |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
943 this.editTagListElement.firstChild); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
944 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
945 this.editTagListElement.appendChild(this.createTagInputElement('')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
946 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
947 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
948 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
949 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
950 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
951 ActionsView.prototype.onSaveFile = function (jsonBlob) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
952 this.saveLinkElement.href = URL.createObjectURL(jsonBlob); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
953 this.saveLinkElement.click(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
954 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
955 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
956 ActionsView.prototype.confirmLoadFile = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
957 return window.confirm('There are unsaved changes to your bookmarks.\n' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
958 'Proceed loading the bookmark file?'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
959 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
960 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
961 ActionsView.prototype.onLoadFileError = function (message) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
962 window.alert('Failed to load bookmark file:\n' + message); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
963 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
964 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
965 ActionsView.prototype.onParseFileError = function (message) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
966 window.alert('Failed to parse bookmark file:\n' + message); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
967 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
968 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
969 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
970 var BookmarkView = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
971 var searchFormElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
972 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
973 ObservableMixin.call(this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
974 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
975 this.bookmarkTemplate = document.querySelector('#bookmark-template'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
976 this.bookmarkTagTemplate = document.querySelector('#bookmark-tag-template'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
977 this.bookmarkEditorTemplate = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
978 document.querySelector('#bookmark-editor-template'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
979 this.tagInputTemplate = document.querySelector('#tag-input-template'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
980 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
981 this.bookmarkListElement = document.querySelector('ul#bookmark-list'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
982 this.bookmarkListElement.addEventListener('click', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
983 this.bookmarkListElement.addEventListener('submit', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
984 this.bookmarkListElement.addEventListener('reset', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
985 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
986 searchFormElement = document.querySelector('#search-form'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
987 searchFormElement.addEventListener('submit', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
988 searchFormElement.addEventListener('reset', this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
989 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
990 this.searchTermInputElement = searchFormElement['search-term']; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
991 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
992 this.bookmarkMessageElement = document.querySelector('#bookmark-message'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
993 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
994 this.updateBookmarkMessage(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
995 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
996 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
997 extend(BookmarkView, ObservableMixin); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
998 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
999 BookmarkView.prototype.handleEvent = function (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1000 var i; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1001 var tags = []; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1002 var node; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1003 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1004 switch (e.type) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1005 case 'click': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1006 switch (e.target.name) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1007 case 'edit-bookmark': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1008 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1009 // fallthrough |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1010 case 'delete-bookmark': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1011 this.notify(e.target.name, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1012 getAncestorElementDatasetItem(e.target, 'bookmarkUrl')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1013 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1014 case 'more-tags': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1015 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1016 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1017 e.target.form.querySelector('ul.tag-input-list').appendChild( |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1018 this.createTagInputElement('')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1019 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1020 case 'set-tag': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1021 case 'toggle-tag': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1022 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1023 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1024 this.notify(e.target.name, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1025 getAncestorElementDatasetItem(e.target, 'tag')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1026 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1027 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1028 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1029 case 'submit': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1030 if (e.target.classList.contains('bookmark-editor-form')) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1031 // save bookmark-editor-form form contents |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1032 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1033 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1034 if (e.target.tag.length) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1035 for (i = 0; i < e.target.tag.length; i++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1036 tags.push(e.target.tag[i].value.trim()); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1037 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1038 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1039 tags.push(e.target.tag.value.trim()); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1040 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1041 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1042 this.notify('save-bookmark', e.target.url.value, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1043 e.target.title.value, tags, e.target['original-url'].value); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1044 } else if (e.target.id === 'search-form') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1045 // search |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1046 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1047 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1048 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1049 this.notify('search', e.target['search-term'].value); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1050 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1051 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1052 case 'reset': |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1053 if (e.target.classList.contains('bookmark-editor-form')) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1054 // cancel bookmark-editor-form form |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1055 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1056 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1057 // re-enable edit button |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1058 this.bookmarkListElement.querySelector('li' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1059 createDatasetSelector('bookmark-url', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1060 e.target['original-url'].value) + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1061 ' button[name="edit-bookmark"]').disabled = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1062 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1063 e.target.parentNode.removeChild(e.target); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1064 } else if (e.target.id === 'search-form') { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1065 // clear search |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1066 e.preventDefault(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1067 e.target.blur(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1068 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1069 this.notify('search', ''); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1070 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1071 break; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1072 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1073 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1074 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1075 BookmarkView.prototype.updateBookmarkMessage = function () { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1076 this.bookmarkMessageElement.textContent = 'Showing ' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1077 this.bookmarkListElement.querySelectorAll('ul#bookmark-list > ' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1078 'li:not([hidden])').length + ' of ' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1079 this.bookmarkListElement.querySelectorAll('ul#bookmark-list > ' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1080 'li').length + ' bookmarks.'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1081 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1082 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1083 BookmarkView.prototype.onBookmarkAdded = function (bookmark) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1084 var newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1085 var bookmarkElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1086 var linkElement; |
3
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1087 var hostnameElement; |
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1088 var urlElement; |
2
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1089 var ctimeElement; |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1090 var mtimeElement; |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1091 var tagListElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1092 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1093 newNode = document.importNode(this.bookmarkTemplate.content, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1094 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1095 bookmarkElement = newNode.querySelector('li'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1096 bookmarkElement.dataset.bookmarkUrl = bookmark.url; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1097 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1098 linkElement = bookmarkElement.querySelector('a.bookmark-link'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1099 linkElement.textContent = linkElement.title = bookmark.title; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1100 linkElement.href = bookmark.url; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1101 |
3
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1102 hostnameElement = bookmarkElement.querySelector('.bookmark-hostname'); |
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1103 hostnameElement.textContent = (linkElement.hostname !== '') ? |
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1104 '[' + linkElement.hostname + ']' : ''; |
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1105 |
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1106 urlElement = bookmarkElement.querySelector('.bookmark-url'); |
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1107 urlElement.textContent = bookmark.url; |
4786df0aec03
Display URL and hostname
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
2
diff
changeset
|
1108 |
2
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1109 ctimeElement = bookmarkElement.querySelector('.ctime'); |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1110 ctimeElement.dateTime = bookmark.ctime.toISOString(); |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1111 ctimeElement.textContent = bookmark.ctime.toString(); |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1112 |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1113 mtimeElement = bookmarkElement.querySelector('.mtime'); |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1114 mtimeElement.dateTime = bookmark.mtime.toISOString(); |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1115 mtimeElement.textContent = bookmark.mtime.toString(); |
82c50265c8dc
Display creation and modification time
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
0
diff
changeset
|
1116 |
0
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1117 tagListElement = bookmarkElement.querySelector('ul.tag-list'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1118 bookmark.tags.forEach(function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1119 var newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1120 var tagElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1121 var setTagButton; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1122 var toggleTagButton; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1123 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1124 newNode = document.importNode(this.bookmarkTagTemplate.content, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1125 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1126 tagElement = newNode.querySelector('li'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1127 tagElement.dataset.tag = tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1128 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1129 setTagButton = newNode.querySelector('button[name="set-tag"]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1130 setTagButton.textContent = tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1131 setTagButton.title = 'Set filter to "' + tag + '"'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1132 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1133 toggleTagButton = newNode.querySelector('button[name="toggle-tag"]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1134 toggleTagButton.textContent = '+'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1135 toggleTagButton.title = 'Add "' + tag + '" to filter'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1136 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1137 tagListElement.appendChild(newNode); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1138 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1139 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1140 // insert new or last modified bookmark on top of the list |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1141 this.bookmarkListElement.insertBefore(newNode, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1142 this.bookmarkListElement.firstChild); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1143 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1144 this.updateBookmarkMessage(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1145 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1146 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1147 BookmarkView.prototype.onBookmarkDeleted = function (bookmarkUrl) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1148 var bookmarkElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1149 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1150 bookmarkElement = this.bookmarkListElement.querySelector('li' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1151 createDatasetSelector('bookmark-url', bookmarkUrl)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1152 if (bookmarkElement !== null) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1153 this.bookmarkListElement.removeChild(bookmarkElement); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1154 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1155 this.updateBookmarkMessage(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1156 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1157 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1158 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1159 BookmarkView.prototype.onFilterTagsSearchChanged = function (filteredBookmarks, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1160 newFilterTags, newSearchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1161 var bookmarkElements; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1162 var i; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1163 var tagElements; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1164 var toggleTagButton; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1165 var j; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1166 var tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1167 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1168 this.searchTermInputElement.value = newSearchTerm; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1169 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1170 bookmarkElements = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1171 this.bookmarkListElement.querySelectorAll('ul#bookmark-list > li'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1172 for (i = 0; i < bookmarkElements.length; i++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1173 // update visibility of bookmarks |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1174 if (filteredBookmarks.has(bookmarkElements[i].dataset.bookmarkUrl)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1175 // update tag elements of visible bookmarks |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1176 tagElements = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1177 bookmarkElements[i].querySelectorAll('ul.tag-list > li'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1178 for (j = 0; j < tagElements.length; j++) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1179 tag = tagElements[j].dataset.tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1180 toggleTagButton = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1181 tagElements[j].querySelector('button[name="toggle-tag"]'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1182 if (newFilterTags.has(tag)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1183 tagElements[j].classList.add('active-filter-tag'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1184 toggleTagButton.textContent = '\u2212'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1185 toggleTagButton.title = 'Remove "' + tag + '" from filter'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1186 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1187 tagElements[j].classList.remove('active-filter-tag'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1188 toggleTagButton.textContent = '+'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1189 toggleTagButton.title = 'Add "' + tag + '" to filter'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1190 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1191 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1192 bookmarkElements[i].hidden = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1193 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1194 bookmarkElements[i].hidden = true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1195 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1196 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1197 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1198 this.updateBookmarkMessage(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1199 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1200 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1201 BookmarkView.prototype.createTagInputElement = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1202 var newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1203 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1204 newNode = document.importNode(this.tagInputTemplate.content, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1205 newNode.querySelector('input[name="tag"]').value = tag; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1206 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1207 return newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1208 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1209 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1210 BookmarkView.prototype.displayBookmarkEditor = function (bookmark) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1211 var bookmarkElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1212 var newNode; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1213 var formElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1214 var editTagListElement; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1215 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1216 bookmarkElement = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1217 this.bookmarkListElement.querySelector('ul#bookmark-list > li' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1218 createDatasetSelector('bookmark-url', bookmark.url)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1219 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1220 // disable edit button while editing |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1221 bookmarkElement.querySelector('button[name="edit-bookmark"]').disabled = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1222 true; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1223 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1224 // create new editor form from template |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1225 newNode = document.importNode(this.bookmarkEditorTemplate.content, true); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1226 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1227 // fill with data of given bookmark |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1228 formElement = newNode.querySelector('form.bookmark-editor-form'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1229 formElement.querySelector('legend').textContent = 'Edit Bookmark'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1230 formElement['original-url'].value = bookmark.url; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1231 formElement.url.value = bookmark.url; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1232 formElement.title.value = bookmark.title; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1233 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1234 editTagListElement = formElement.querySelector('ul.tag-input-list'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1235 bookmark.tags.forEach(function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1236 editTagListElement.appendChild(this.createTagInputElement(tag)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1237 }, this); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1238 editTagListElement.appendChild(this.createTagInputElement('')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1239 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1240 // insert editor form into bookmark item |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1241 bookmarkElement.appendChild(newNode); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1242 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1243 // focus first input element |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1244 formElement.querySelector('input').focus(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1245 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1246 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1247 BookmarkView.prototype.confirmReplaceBookmark = function (bookmark) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1248 return window.confirm('Replace bookmark "' + bookmark.title + '"\n[' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1249 bookmark.url + ']?'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1250 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1251 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1252 BookmarkView.prototype.confirmDeleteBookmark = function (bookmark) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1253 return window.confirm('Delete bookmark "' + bookmark.title + '"\n[' + |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1254 bookmark.url + ']?'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1255 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1256 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1257 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1258 /* |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1259 * controller |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1260 */ |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1261 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1262 var BooketController = function(bookmarkModel, actionsView, tagView, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1263 bookmarkView) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1264 this.bookmarkModel = bookmarkModel; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1265 this.actionsView = actionsView; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1266 this.tagView = tagView; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1267 this.bookmarkView = bookmarkView; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1268 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1269 /* connect the views to the model */ |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1270 this.bookmarkModel.addObserver('bookmark-added', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1271 this.bookmarkView.onBookmarkAdded.bind(this.bookmarkView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1272 this.bookmarkModel.addObserver('bookmark-deleted', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1273 this.bookmarkView.onBookmarkDeleted.bind(this.bookmarkView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1274 this.bookmarkModel.addObserver('filter-tags-search-changed', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1275 this.bookmarkView.onFilterTagsSearchChanged.bind(this.bookmarkView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1276 this.bookmarkModel.addObserver('load-file-error', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1277 this.actionsView.onLoadFileError.bind(this.actionsView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1278 this.bookmarkModel.addObserver('parse-file-error', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1279 this.actionsView.onParseFileError.bind(this.actionsView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1280 this.bookmarkModel.addObserver('save-file', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1281 this.actionsView.onSaveFile.bind(this.actionsView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1282 this.bookmarkModel.addObserver('tag-added', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1283 this.tagView.onTagAdded.bind(this.tagView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1284 this.bookmarkModel.addObserver('tag-count-changed', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1285 this.tagView.onTagCountChanged.bind(this.tagView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1286 this.bookmarkModel.addObserver('tag-deleted', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1287 this.tagView.onTagDeleted.bind(this.tagView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1288 this.bookmarkModel.addObserver('filter-tags-search-changed', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1289 this.tagView.onFilterTagsSearchChanged.bind(this.tagView)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1290 this.bookmarkModel.addObserver('filter-tags-search-changed', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1291 this.onFilterTagsSearchChanged.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1292 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1293 /* handle input */ |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1294 window.addEventListener('hashchange', this.onHashChange.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1295 window.addEventListener('beforeunload', this.onBeforeUnload.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1296 this.actionsView.addObserver('save-file', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1297 this.bookmarkModel.saveFile.bind(this.bookmarkModel)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1298 this.actionsView.addObserver('load-file', this.onLoadFile.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1299 this.actionsView.addObserver('save-bookmark', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1300 this.onSaveBookmark.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1301 this.bookmarkView.addObserver('edit-bookmark', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1302 this.onEditBookmark.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1303 this.bookmarkView.addObserver('save-bookmark', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1304 this.onSaveBookmark.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1305 this.bookmarkView.addObserver('delete-bookmark', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1306 this.onDeleteBookmark.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1307 this.bookmarkView.addObserver('toggle-tag', |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1308 this.onToggleFilterTag.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1309 this.bookmarkView.addObserver('set-tag', this.onSetTagFilter.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1310 this.bookmarkView.addObserver('search', this.onSearch.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1311 this.tagView.addObserver('toggle-tag', this.onToggleFilterTag.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1312 this.tagView.addObserver('set-tag', this.onSetTagFilter.bind(this)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1313 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1314 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1315 BooketController.prototype.parseTagsParameter = function (tagsString) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1316 var tags; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1317 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1318 tags = tagsString.split(',').filter(function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1319 return (tag !== '') && this.bookmarkModel.hasTag(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1320 }, this).sort(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1321 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1322 return new StringSet(tags); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1323 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1324 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1325 BooketController.prototype.onHashChange = function (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1326 var hashData; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1327 var filterTags; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1328 var searchTerm; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1329 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1330 hashData = parseHash(window.location.href); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1331 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1332 filterTags = hashData.has('tags') ? |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1333 this.parseTagsParameter(hashData.get('tags')) : new StringSet(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1334 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1335 searchTerm = hashData.has('search') ? hashData.get('search') : ''; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1336 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1337 this.bookmarkModel.setFilterTagsSearchTerm(filterTags, searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1338 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1339 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1340 BooketController.prototype.onBeforeUnload = function (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1341 var confirmationMessage = 'There are unsaved changes to your bookmarks.'; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1342 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1343 if (this.bookmarkModel.unsavedChanges) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1344 if (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1345 e.returnValue = confirmationMessage; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1346 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1347 if (window.event) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1348 window.event.returnValue = confirmationMessage; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1349 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1350 return confirmationMessage; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1351 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1352 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1353 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1354 BooketController.prototype.onFilterTagsSearchChanged = |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1355 function (filteredBookmarks, newFilterTags, newSearchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1356 var url = window.location.href; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1357 var hashData; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1358 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1359 // serialize tag filter and search term and update window.location |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1360 hashData = parseHash(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1361 hashData.set('tags', newFilterTags.values().join(',')); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1362 hashData.set('search', newSearchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1363 history.pushState(null, null, serializeHash(url, hashData)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1364 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1365 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1366 BooketController.prototype.onLoadFile = function (bookmarkFile) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1367 if (this.bookmarkModel.unsavedChanges) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1368 if (!this.actionsView.confirmLoadFile()) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1369 return; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1370 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1371 this.bookmarkModel.unsavedChanges = false; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1372 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1373 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1374 this.bookmarkModel.loadFile(bookmarkFile); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1375 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1376 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1377 BooketController.prototype.onEditBookmark = function (bookmarkUrl) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1378 this.bookmarkView.displayBookmarkEditor( |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1379 this.bookmarkModel.get(bookmarkUrl)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1380 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1381 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1382 BooketController.prototype.onSaveBookmark = function (url, title, tags, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1383 originalUrl) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1384 var ctime; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1385 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1386 if (originalUrl === undefined) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1387 // saving new bookmark, get confirmation before replacing existing one |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1388 if (this.bookmarkModel.has(url)) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1389 if (this.bookmarkView.confirmReplaceBookmark( |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1390 this.bookmarkModel.get(url))) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1391 this.bookmarkModel.delete(url); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1392 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1393 return; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1394 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1395 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1396 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1397 ctime = new Date(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1398 } else { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1399 // saving edited bookmark, preserve creation time of any replaced |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1400 // bookmark |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1401 ctime = (this.bookmarkModel.has(url)) ? |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1402 this.bookmarkModel.get(url).ctime : new Date(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1403 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1404 this.bookmarkModel.delete(originalUrl); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1405 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1406 this.bookmarkModel.add(new Bookmark(url, title, tags, ctime)); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1407 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1408 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1409 BooketController.prototype.onDeleteBookmark = function (bookmarkUrl) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1410 if (this.bookmarkView.confirmDeleteBookmark( |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1411 this.bookmarkModel.get(bookmarkUrl))) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1412 this.bookmarkModel.delete(bookmarkUrl); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1413 } |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1414 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1415 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1416 BooketController.prototype.onToggleFilterTag = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1417 this.bookmarkModel.toggleFilterTag(tag); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1418 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1419 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1420 BooketController.prototype.onSetTagFilter = function (tag) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1421 this.bookmarkModel.setFilterTags(new StringSet([tag])); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1422 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1423 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1424 BooketController.prototype.onSearch = function (searchTerm) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1425 this.bookmarkModel.setSearchTerm(searchTerm); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1426 }; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1427 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1428 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1429 document.addEventListener('DOMContentLoaded', function (e) { |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1430 var controller; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1431 var bookmarkModel; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1432 var actionsView; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1433 var tagView; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1434 var bookmarkView; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1435 var hashChangeEvent; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1436 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1437 bookmarkModel = new BookmarkModel(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1438 tagView = new TagView(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1439 actionsView = new ActionsView(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1440 bookmarkView = new BookmarkView(); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1441 controller = new BooketController(bookmarkModel, actionsView, |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1442 tagView, bookmarkView); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1443 |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1444 // initialize state from the current URL |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1445 hashChangeEvent = new Event('hashchange'); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1446 hashChangeEvent.oldURL = window.location.href; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1447 hashChangeEvent.newURL = window.location.href; |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1448 window.dispatchEvent(hashChangeEvent); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1449 }); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1450 }()); |
c2248f662a2c
Initial revision
Guido Berhoerster <guido+booket@berhoerster.name>
parents:
diff
changeset
|
1451 |