1 | /* |
---|
2 | Copyright (c) 2005 Tim Taylor Consulting <http://tool-man.org/> |
---|
3 | |
---|
4 | Permission is hereby granted, free of charge, to any person obtaining a |
---|
5 | copy of this software and associated documentation files (the "Software"), |
---|
6 | to deal in the Software without restriction, including without limitation |
---|
7 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | Software is furnished to do so, subject to the following conditions: |
---|
10 | |
---|
11 | The above copyright notice and this permission notice shall be included |
---|
12 | in all copies or substantial portions of the Software. |
---|
13 | |
---|
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
19 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
---|
20 | IN THE SOFTWARE. |
---|
21 | */ |
---|
22 | |
---|
23 | var ToolMan = { |
---|
24 | events : function() { |
---|
25 | if (!ToolMan._eventsFactory) throw "ToolMan Events module isn't loaded"; |
---|
26 | return ToolMan._eventsFactory |
---|
27 | }, |
---|
28 | |
---|
29 | css : function() { |
---|
30 | if (!ToolMan._cssFactory) throw "ToolMan CSS module isn't loaded"; |
---|
31 | return ToolMan._cssFactory |
---|
32 | }, |
---|
33 | |
---|
34 | coordinates : function() { |
---|
35 | if (!ToolMan._coordinatesFactory) throw "ToolMan Coordinates module isn't loaded"; |
---|
36 | return ToolMan._coordinatesFactory |
---|
37 | }, |
---|
38 | |
---|
39 | drag : function() { |
---|
40 | if (!ToolMan._dragFactory) throw "ToolMan Drag module isn't loaded"; |
---|
41 | return ToolMan._dragFactory |
---|
42 | }, |
---|
43 | |
---|
44 | dragsort : function() { |
---|
45 | if (!ToolMan._dragsortFactory) throw "ToolMan DragSort module isn't loaded"; |
---|
46 | return ToolMan._dragsortFactory |
---|
47 | }, |
---|
48 | |
---|
49 | helpers : function() { |
---|
50 | return ToolMan._helpers |
---|
51 | }, |
---|
52 | |
---|
53 | cookies : function() { |
---|
54 | if (!ToolMan._cookieOven) throw "ToolMan Cookie module isn't loaded"; |
---|
55 | return ToolMan._cookieOven |
---|
56 | }, |
---|
57 | |
---|
58 | junkdrawer : function() { |
---|
59 | return ToolMan._junkdrawer |
---|
60 | } |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | ToolMan._helpers = { |
---|
65 | map : function(array, func) { |
---|
66 | for (var i = 0, n = array.length; i < n; i++) func(array[i]) |
---|
67 | }, |
---|
68 | |
---|
69 | nextItem : function(item, nodeName) { |
---|
70 | if (item == null) return false; |
---|
71 | var next = item.nextSibling |
---|
72 | while (next != null) { |
---|
73 | if (next.nodeName == nodeName) return next |
---|
74 | next = next.nextSibling |
---|
75 | } |
---|
76 | return null |
---|
77 | }, |
---|
78 | |
---|
79 | previousItem : function(item, nodeName) { |
---|
80 | var previous = item.previousSibling |
---|
81 | while (previous != null) { |
---|
82 | if (previous.nodeName == nodeName) return previous |
---|
83 | previous = previous.previousSibling |
---|
84 | } |
---|
85 | return null |
---|
86 | }, |
---|
87 | |
---|
88 | moveBefore : function(item1, item2) { |
---|
89 | var parent = item1.parentNode |
---|
90 | parent.removeChild(item1) |
---|
91 | parent.insertBefore(item1, item2) |
---|
92 | }, |
---|
93 | |
---|
94 | moveAfter : function(item1, item2) { |
---|
95 | var parent = item1.parentNode |
---|
96 | parent.removeChild(item1) |
---|
97 | parent.insertBefore(item1, item2 ? item2.nextSibling : null) |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * scripts without a proper home |
---|
103 | * |
---|
104 | * stuff here is subject to change unapologetically and without warning |
---|
105 | */ |
---|
106 | ToolMan._junkdrawer = { |
---|
107 | serializeList : function(list) { |
---|
108 | var items = list.getElementsByTagName("li") |
---|
109 | var array = new Array() |
---|
110 | for (var i = 0, n = items.length; i < n; i++) { |
---|
111 | var item = items[i] |
---|
112 | |
---|
113 | array.push(ToolMan.junkdrawer()._identifier(item)) |
---|
114 | } |
---|
115 | return array.join('|') |
---|
116 | }, |
---|
117 | |
---|
118 | inspectListOrder : function(id) { |
---|
119 | alert(ToolMan.junkdrawer().serializeList(document.getElementById(id))) |
---|
120 | }, |
---|
121 | |
---|
122 | restoreListOrder : function(listID) { |
---|
123 | var list = document.getElementById(listID) |
---|
124 | if (list == null) return |
---|
125 | |
---|
126 | var cookie = ToolMan.cookies().get("list-" + listID) |
---|
127 | if (!cookie) return; |
---|
128 | |
---|
129 | var IDs = cookie.split('|') |
---|
130 | var items = ToolMan.junkdrawer()._itemsByID(list) |
---|
131 | |
---|
132 | for (var i = 0, n = IDs.length; i < n; i++) { |
---|
133 | var itemID = IDs[i] |
---|
134 | if (itemID in items) { |
---|
135 | var item = items[itemID] |
---|
136 | list.removeChild(item) |
---|
137 | list.insertBefore(item, null) |
---|
138 | } |
---|
139 | } |
---|
140 | }, |
---|
141 | |
---|
142 | _identifier : function(item) { |
---|
143 | var trim = ToolMan.junkdrawer().trim |
---|
144 | var identifier |
---|
145 | |
---|
146 | identifier = trim(item.getAttribute("id")) |
---|
147 | if (identifier != null && identifier.length > 0) return identifier; |
---|
148 | |
---|
149 | identifier = trim(item.getAttribute("itemID")) |
---|
150 | if (identifier != null && identifier.length > 0) return identifier; |
---|
151 | |
---|
152 | // FIXME: strip out special chars or make this an MD5 hash or something |
---|
153 | return trim(item.innerHTML) |
---|
154 | }, |
---|
155 | |
---|
156 | _itemsByID : function(list) { |
---|
157 | var array = new Array() |
---|
158 | var items = list.getElementsByTagName('li') |
---|
159 | for (var i = 0, n = items.length; i < n; i++) { |
---|
160 | var item = items[i] |
---|
161 | array[ToolMan.junkdrawer()._identifier(item)] = item |
---|
162 | } |
---|
163 | return array |
---|
164 | }, |
---|
165 | |
---|
166 | trim : function(text) { |
---|
167 | if (text == null) return null |
---|
168 | return text.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2') |
---|
169 | } |
---|
170 | } |
---|