Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /*** |
michael@0 | 2 | Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) |
michael@0 | 3 | Mochi-ized By Thomas Herve (_firstname_@nimail.org) |
michael@0 | 4 | |
michael@0 | 5 | See scriptaculous.js for full license. |
michael@0 | 6 | |
michael@0 | 7 | ***/ |
michael@0 | 8 | |
michael@0 | 9 | MochiKit.Base._deps('Sortable', ['Base', 'Iter', 'DOM', 'Position', 'DragAndDrop']); |
michael@0 | 10 | |
michael@0 | 11 | MochiKit.Sortable.NAME = 'MochiKit.Sortable'; |
michael@0 | 12 | MochiKit.Sortable.VERSION = '1.4.2'; |
michael@0 | 13 | |
michael@0 | 14 | MochiKit.Sortable.__repr__ = function () { |
michael@0 | 15 | return '[' + this.NAME + ' ' + this.VERSION + ']'; |
michael@0 | 16 | }; |
michael@0 | 17 | |
michael@0 | 18 | MochiKit.Sortable.toString = function () { |
michael@0 | 19 | return this.__repr__(); |
michael@0 | 20 | }; |
michael@0 | 21 | |
michael@0 | 22 | MochiKit.Sortable.EXPORT = [ |
michael@0 | 23 | ]; |
michael@0 | 24 | |
michael@0 | 25 | MochiKit.Sortable.EXPORT_OK = [ |
michael@0 | 26 | ]; |
michael@0 | 27 | |
michael@0 | 28 | MochiKit.Base.update(MochiKit.Sortable, { |
michael@0 | 29 | /*** |
michael@0 | 30 | |
michael@0 | 31 | Manage sortables. Mainly use the create function to add a sortable. |
michael@0 | 32 | |
michael@0 | 33 | ***/ |
michael@0 | 34 | sortables: {}, |
michael@0 | 35 | |
michael@0 | 36 | _findRootElement: function (element) { |
michael@0 | 37 | while (element.tagName.toUpperCase() != "BODY") { |
michael@0 | 38 | if (element.id && MochiKit.Sortable.sortables[element.id]) { |
michael@0 | 39 | return element; |
michael@0 | 40 | } |
michael@0 | 41 | element = element.parentNode; |
michael@0 | 42 | } |
michael@0 | 43 | }, |
michael@0 | 44 | |
michael@0 | 45 | _createElementId: function(element) { |
michael@0 | 46 | if (element.id == null || element.id == "") { |
michael@0 | 47 | var d = MochiKit.DOM; |
michael@0 | 48 | var id; |
michael@0 | 49 | var count = 1; |
michael@0 | 50 | while (d.getElement(id = "sortable" + count) != null) { |
michael@0 | 51 | count += 1; |
michael@0 | 52 | } |
michael@0 | 53 | d.setNodeAttribute(element, "id", id); |
michael@0 | 54 | } |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | /** @id MochiKit.Sortable.options */ |
michael@0 | 58 | options: function (element) { |
michael@0 | 59 | element = MochiKit.Sortable._findRootElement(MochiKit.DOM.getElement(element)); |
michael@0 | 60 | if (!element) { |
michael@0 | 61 | return; |
michael@0 | 62 | } |
michael@0 | 63 | return MochiKit.Sortable.sortables[element.id]; |
michael@0 | 64 | }, |
michael@0 | 65 | |
michael@0 | 66 | /** @id MochiKit.Sortable.destroy */ |
michael@0 | 67 | destroy: function (element){ |
michael@0 | 68 | var s = MochiKit.Sortable.options(element); |
michael@0 | 69 | var b = MochiKit.Base; |
michael@0 | 70 | var d = MochiKit.DragAndDrop; |
michael@0 | 71 | |
michael@0 | 72 | if (s) { |
michael@0 | 73 | MochiKit.Signal.disconnect(s.startHandle); |
michael@0 | 74 | MochiKit.Signal.disconnect(s.endHandle); |
michael@0 | 75 | b.map(function (dr) { |
michael@0 | 76 | d.Droppables.remove(dr); |
michael@0 | 77 | }, s.droppables); |
michael@0 | 78 | b.map(function (dr) { |
michael@0 | 79 | dr.destroy(); |
michael@0 | 80 | }, s.draggables); |
michael@0 | 81 | |
michael@0 | 82 | delete MochiKit.Sortable.sortables[s.element.id]; |
michael@0 | 83 | } |
michael@0 | 84 | }, |
michael@0 | 85 | |
michael@0 | 86 | /** @id MochiKit.Sortable.create */ |
michael@0 | 87 | create: function (element, options) { |
michael@0 | 88 | element = MochiKit.DOM.getElement(element); |
michael@0 | 89 | var self = MochiKit.Sortable; |
michael@0 | 90 | self._createElementId(element); |
michael@0 | 91 | |
michael@0 | 92 | /** @id MochiKit.Sortable.options */ |
michael@0 | 93 | options = MochiKit.Base.update({ |
michael@0 | 94 | |
michael@0 | 95 | /** @id MochiKit.Sortable.element */ |
michael@0 | 96 | element: element, |
michael@0 | 97 | |
michael@0 | 98 | /** @id MochiKit.Sortable.tag */ |
michael@0 | 99 | tag: 'li', // assumes li children, override with tag: 'tagname' |
michael@0 | 100 | |
michael@0 | 101 | /** @id MochiKit.Sortable.dropOnEmpty */ |
michael@0 | 102 | dropOnEmpty: false, |
michael@0 | 103 | |
michael@0 | 104 | /** @id MochiKit.Sortable.tree */ |
michael@0 | 105 | tree: false, |
michael@0 | 106 | |
michael@0 | 107 | /** @id MochiKit.Sortable.treeTag */ |
michael@0 | 108 | treeTag: 'ul', |
michael@0 | 109 | |
michael@0 | 110 | /** @id MochiKit.Sortable.overlap */ |
michael@0 | 111 | overlap: 'vertical', // one of 'vertical', 'horizontal' |
michael@0 | 112 | |
michael@0 | 113 | /** @id MochiKit.Sortable.constraint */ |
michael@0 | 114 | constraint: 'vertical', // one of 'vertical', 'horizontal', false |
michael@0 | 115 | // also takes array of elements (or ids); or false |
michael@0 | 116 | |
michael@0 | 117 | /** @id MochiKit.Sortable.containment */ |
michael@0 | 118 | containment: [element], |
michael@0 | 119 | |
michael@0 | 120 | /** @id MochiKit.Sortable.handle */ |
michael@0 | 121 | handle: false, // or a CSS class |
michael@0 | 122 | |
michael@0 | 123 | /** @id MochiKit.Sortable.only */ |
michael@0 | 124 | only: false, |
michael@0 | 125 | |
michael@0 | 126 | /** @id MochiKit.Sortable.hoverclass */ |
michael@0 | 127 | hoverclass: null, |
michael@0 | 128 | |
michael@0 | 129 | /** @id MochiKit.Sortable.ghosting */ |
michael@0 | 130 | ghosting: false, |
michael@0 | 131 | |
michael@0 | 132 | /** @id MochiKit.Sortable.scroll */ |
michael@0 | 133 | scroll: false, |
michael@0 | 134 | |
michael@0 | 135 | /** @id MochiKit.Sortable.scrollSensitivity */ |
michael@0 | 136 | scrollSensitivity: 20, |
michael@0 | 137 | |
michael@0 | 138 | /** @id MochiKit.Sortable.scrollSpeed */ |
michael@0 | 139 | scrollSpeed: 15, |
michael@0 | 140 | |
michael@0 | 141 | /** @id MochiKit.Sortable.format */ |
michael@0 | 142 | format: /^[^_]*_(.*)$/, |
michael@0 | 143 | |
michael@0 | 144 | /** @id MochiKit.Sortable.onChange */ |
michael@0 | 145 | onChange: MochiKit.Base.noop, |
michael@0 | 146 | |
michael@0 | 147 | /** @id MochiKit.Sortable.onUpdate */ |
michael@0 | 148 | onUpdate: MochiKit.Base.noop, |
michael@0 | 149 | |
michael@0 | 150 | /** @id MochiKit.Sortable.accept */ |
michael@0 | 151 | accept: null |
michael@0 | 152 | }, options); |
michael@0 | 153 | |
michael@0 | 154 | // clear any old sortable with same element |
michael@0 | 155 | self.destroy(element); |
michael@0 | 156 | |
michael@0 | 157 | // build options for the draggables |
michael@0 | 158 | var options_for_draggable = { |
michael@0 | 159 | revert: true, |
michael@0 | 160 | ghosting: options.ghosting, |
michael@0 | 161 | scroll: options.scroll, |
michael@0 | 162 | scrollSensitivity: options.scrollSensitivity, |
michael@0 | 163 | scrollSpeed: options.scrollSpeed, |
michael@0 | 164 | constraint: options.constraint, |
michael@0 | 165 | handle: options.handle |
michael@0 | 166 | }; |
michael@0 | 167 | |
michael@0 | 168 | if (options.starteffect) { |
michael@0 | 169 | options_for_draggable.starteffect = options.starteffect; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | if (options.reverteffect) { |
michael@0 | 173 | options_for_draggable.reverteffect = options.reverteffect; |
michael@0 | 174 | } else if (options.ghosting) { |
michael@0 | 175 | options_for_draggable.reverteffect = function (innerelement) { |
michael@0 | 176 | innerelement.style.top = 0; |
michael@0 | 177 | innerelement.style.left = 0; |
michael@0 | 178 | }; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | if (options.endeffect) { |
michael@0 | 182 | options_for_draggable.endeffect = options.endeffect; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | if (options.zindex) { |
michael@0 | 186 | options_for_draggable.zindex = options.zindex; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | // build options for the droppables |
michael@0 | 190 | var options_for_droppable = { |
michael@0 | 191 | overlap: options.overlap, |
michael@0 | 192 | containment: options.containment, |
michael@0 | 193 | hoverclass: options.hoverclass, |
michael@0 | 194 | onhover: self.onHover, |
michael@0 | 195 | tree: options.tree, |
michael@0 | 196 | accept: options.accept |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | var options_for_tree = { |
michael@0 | 200 | onhover: self.onEmptyHover, |
michael@0 | 201 | overlap: options.overlap, |
michael@0 | 202 | containment: options.containment, |
michael@0 | 203 | hoverclass: options.hoverclass, |
michael@0 | 204 | accept: options.accept |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | // fix for gecko engine |
michael@0 | 208 | MochiKit.DOM.removeEmptyTextNodes(element); |
michael@0 | 209 | |
michael@0 | 210 | options.draggables = []; |
michael@0 | 211 | options.droppables = []; |
michael@0 | 212 | |
michael@0 | 213 | // drop on empty handling |
michael@0 | 214 | if (options.dropOnEmpty || options.tree) { |
michael@0 | 215 | new MochiKit.DragAndDrop.Droppable(element, options_for_tree); |
michael@0 | 216 | options.droppables.push(element); |
michael@0 | 217 | } |
michael@0 | 218 | MochiKit.Base.map(function (e) { |
michael@0 | 219 | // handles are per-draggable |
michael@0 | 220 | var handle = options.handle ? |
michael@0 | 221 | MochiKit.DOM.getFirstElementByTagAndClassName(null, |
michael@0 | 222 | options.handle, e) : e; |
michael@0 | 223 | options.draggables.push( |
michael@0 | 224 | new MochiKit.DragAndDrop.Draggable(e, |
michael@0 | 225 | MochiKit.Base.update(options_for_draggable, |
michael@0 | 226 | {handle: handle}))); |
michael@0 | 227 | new MochiKit.DragAndDrop.Droppable(e, options_for_droppable); |
michael@0 | 228 | if (options.tree) { |
michael@0 | 229 | e.treeNode = element; |
michael@0 | 230 | } |
michael@0 | 231 | options.droppables.push(e); |
michael@0 | 232 | }, (self.findElements(element, options) || [])); |
michael@0 | 233 | |
michael@0 | 234 | if (options.tree) { |
michael@0 | 235 | MochiKit.Base.map(function (e) { |
michael@0 | 236 | new MochiKit.DragAndDrop.Droppable(e, options_for_tree); |
michael@0 | 237 | e.treeNode = element; |
michael@0 | 238 | options.droppables.push(e); |
michael@0 | 239 | }, (self.findTreeElements(element, options) || [])); |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | // keep reference |
michael@0 | 243 | self.sortables[element.id] = options; |
michael@0 | 244 | |
michael@0 | 245 | options.lastValue = self.serialize(element); |
michael@0 | 246 | options.startHandle = MochiKit.Signal.connect(MochiKit.DragAndDrop.Draggables, 'start', |
michael@0 | 247 | MochiKit.Base.partial(self.onStart, element)); |
michael@0 | 248 | options.endHandle = MochiKit.Signal.connect(MochiKit.DragAndDrop.Draggables, 'end', |
michael@0 | 249 | MochiKit.Base.partial(self.onEnd, element)); |
michael@0 | 250 | }, |
michael@0 | 251 | |
michael@0 | 252 | /** @id MochiKit.Sortable.onStart */ |
michael@0 | 253 | onStart: function (element, draggable) { |
michael@0 | 254 | var self = MochiKit.Sortable; |
michael@0 | 255 | var options = self.options(element); |
michael@0 | 256 | options.lastValue = self.serialize(options.element); |
michael@0 | 257 | }, |
michael@0 | 258 | |
michael@0 | 259 | /** @id MochiKit.Sortable.onEnd */ |
michael@0 | 260 | onEnd: function (element, draggable) { |
michael@0 | 261 | var self = MochiKit.Sortable; |
michael@0 | 262 | self.unmark(); |
michael@0 | 263 | var options = self.options(element); |
michael@0 | 264 | if (options.lastValue != self.serialize(options.element)) { |
michael@0 | 265 | options.onUpdate(options.element); |
michael@0 | 266 | } |
michael@0 | 267 | }, |
michael@0 | 268 | |
michael@0 | 269 | // return all suitable-for-sortable elements in a guaranteed order |
michael@0 | 270 | |
michael@0 | 271 | /** @id MochiKit.Sortable.findElements */ |
michael@0 | 272 | findElements: function (element, options) { |
michael@0 | 273 | return MochiKit.Sortable.findChildren(element, options.only, options.tree, options.tag); |
michael@0 | 274 | }, |
michael@0 | 275 | |
michael@0 | 276 | /** @id MochiKit.Sortable.findTreeElements */ |
michael@0 | 277 | findTreeElements: function (element, options) { |
michael@0 | 278 | return MochiKit.Sortable.findChildren( |
michael@0 | 279 | element, options.only, options.tree ? true : false, options.treeTag); |
michael@0 | 280 | }, |
michael@0 | 281 | |
michael@0 | 282 | /** @id MochiKit.Sortable.findChildren */ |
michael@0 | 283 | findChildren: function (element, only, recursive, tagName) { |
michael@0 | 284 | if (!element.hasChildNodes()) { |
michael@0 | 285 | return null; |
michael@0 | 286 | } |
michael@0 | 287 | tagName = tagName.toUpperCase(); |
michael@0 | 288 | if (only) { |
michael@0 | 289 | only = MochiKit.Base.flattenArray([only]); |
michael@0 | 290 | } |
michael@0 | 291 | var elements = []; |
michael@0 | 292 | MochiKit.Base.map(function (e) { |
michael@0 | 293 | if (e.tagName && |
michael@0 | 294 | e.tagName.toUpperCase() == tagName && |
michael@0 | 295 | (!only || |
michael@0 | 296 | MochiKit.Iter.some(only, function (c) { |
michael@0 | 297 | return MochiKit.DOM.hasElementClass(e, c); |
michael@0 | 298 | }))) { |
michael@0 | 299 | elements.push(e); |
michael@0 | 300 | } |
michael@0 | 301 | if (recursive) { |
michael@0 | 302 | var grandchildren = MochiKit.Sortable.findChildren(e, only, recursive, tagName); |
michael@0 | 303 | if (grandchildren && grandchildren.length > 0) { |
michael@0 | 304 | elements = elements.concat(grandchildren); |
michael@0 | 305 | } |
michael@0 | 306 | } |
michael@0 | 307 | }, element.childNodes); |
michael@0 | 308 | return elements; |
michael@0 | 309 | }, |
michael@0 | 310 | |
michael@0 | 311 | /** @id MochiKit.Sortable.onHover */ |
michael@0 | 312 | onHover: function (element, dropon, overlap) { |
michael@0 | 313 | if (MochiKit.DOM.isChildNode(dropon, element)) { |
michael@0 | 314 | return; |
michael@0 | 315 | } |
michael@0 | 316 | var self = MochiKit.Sortable; |
michael@0 | 317 | |
michael@0 | 318 | if (overlap > .33 && overlap < .66 && self.options(dropon).tree) { |
michael@0 | 319 | return; |
michael@0 | 320 | } else if (overlap > 0.5) { |
michael@0 | 321 | self.mark(dropon, 'before'); |
michael@0 | 322 | if (dropon.previousSibling != element) { |
michael@0 | 323 | var oldParentNode = element.parentNode; |
michael@0 | 324 | element.style.visibility = 'hidden'; // fix gecko rendering |
michael@0 | 325 | dropon.parentNode.insertBefore(element, dropon); |
michael@0 | 326 | if (dropon.parentNode != oldParentNode) { |
michael@0 | 327 | self.options(oldParentNode).onChange(element); |
michael@0 | 328 | } |
michael@0 | 329 | self.options(dropon.parentNode).onChange(element); |
michael@0 | 330 | } |
michael@0 | 331 | } else { |
michael@0 | 332 | self.mark(dropon, 'after'); |
michael@0 | 333 | var nextElement = dropon.nextSibling || null; |
michael@0 | 334 | if (nextElement != element) { |
michael@0 | 335 | var oldParentNode = element.parentNode; |
michael@0 | 336 | element.style.visibility = 'hidden'; // fix gecko rendering |
michael@0 | 337 | dropon.parentNode.insertBefore(element, nextElement); |
michael@0 | 338 | if (dropon.parentNode != oldParentNode) { |
michael@0 | 339 | self.options(oldParentNode).onChange(element); |
michael@0 | 340 | } |
michael@0 | 341 | self.options(dropon.parentNode).onChange(element); |
michael@0 | 342 | } |
michael@0 | 343 | } |
michael@0 | 344 | }, |
michael@0 | 345 | |
michael@0 | 346 | _offsetSize: function (element, type) { |
michael@0 | 347 | if (type == 'vertical' || type == 'height') { |
michael@0 | 348 | return element.offsetHeight; |
michael@0 | 349 | } else { |
michael@0 | 350 | return element.offsetWidth; |
michael@0 | 351 | } |
michael@0 | 352 | }, |
michael@0 | 353 | |
michael@0 | 354 | /** @id MochiKit.Sortable.onEmptyHover */ |
michael@0 | 355 | onEmptyHover: function (element, dropon, overlap) { |
michael@0 | 356 | var oldParentNode = element.parentNode; |
michael@0 | 357 | var self = MochiKit.Sortable; |
michael@0 | 358 | var droponOptions = self.options(dropon); |
michael@0 | 359 | |
michael@0 | 360 | if (!MochiKit.DOM.isChildNode(dropon, element)) { |
michael@0 | 361 | var index; |
michael@0 | 362 | |
michael@0 | 363 | var children = self.findElements(dropon, {tag: droponOptions.tag, |
michael@0 | 364 | only: droponOptions.only}); |
michael@0 | 365 | var child = null; |
michael@0 | 366 | |
michael@0 | 367 | if (children) { |
michael@0 | 368 | var offset = self._offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); |
michael@0 | 369 | |
michael@0 | 370 | for (index = 0; index < children.length; index += 1) { |
michael@0 | 371 | if (offset - self._offsetSize(children[index], droponOptions.overlap) >= 0) { |
michael@0 | 372 | offset -= self._offsetSize(children[index], droponOptions.overlap); |
michael@0 | 373 | } else if (offset - (self._offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { |
michael@0 | 374 | child = index + 1 < children.length ? children[index + 1] : null; |
michael@0 | 375 | break; |
michael@0 | 376 | } else { |
michael@0 | 377 | child = children[index]; |
michael@0 | 378 | break; |
michael@0 | 379 | } |
michael@0 | 380 | } |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | dropon.insertBefore(element, child); |
michael@0 | 384 | |
michael@0 | 385 | self.options(oldParentNode).onChange(element); |
michael@0 | 386 | droponOptions.onChange(element); |
michael@0 | 387 | } |
michael@0 | 388 | }, |
michael@0 | 389 | |
michael@0 | 390 | /** @id MochiKit.Sortable.unmark */ |
michael@0 | 391 | unmark: function () { |
michael@0 | 392 | var m = MochiKit.Sortable._marker; |
michael@0 | 393 | if (m) { |
michael@0 | 394 | MochiKit.Style.hideElement(m); |
michael@0 | 395 | } |
michael@0 | 396 | }, |
michael@0 | 397 | |
michael@0 | 398 | /** @id MochiKit.Sortable.mark */ |
michael@0 | 399 | mark: function (dropon, position) { |
michael@0 | 400 | // mark on ghosting only |
michael@0 | 401 | var d = MochiKit.DOM; |
michael@0 | 402 | var self = MochiKit.Sortable; |
michael@0 | 403 | var sortable = self.options(dropon.parentNode); |
michael@0 | 404 | if (sortable && !sortable.ghosting) { |
michael@0 | 405 | return; |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | if (!self._marker) { |
michael@0 | 409 | self._marker = d.getElement('dropmarker') || |
michael@0 | 410 | document.createElement('DIV'); |
michael@0 | 411 | MochiKit.Style.hideElement(self._marker); |
michael@0 | 412 | d.addElementClass(self._marker, 'dropmarker'); |
michael@0 | 413 | self._marker.style.position = 'absolute'; |
michael@0 | 414 | document.getElementsByTagName('body').item(0).appendChild(self._marker); |
michael@0 | 415 | } |
michael@0 | 416 | var offsets = MochiKit.Position.cumulativeOffset(dropon); |
michael@0 | 417 | self._marker.style.left = offsets.x + 'px'; |
michael@0 | 418 | self._marker.style.top = offsets.y + 'px'; |
michael@0 | 419 | |
michael@0 | 420 | if (position == 'after') { |
michael@0 | 421 | if (sortable.overlap == 'horizontal') { |
michael@0 | 422 | self._marker.style.left = (offsets.x + dropon.clientWidth) + 'px'; |
michael@0 | 423 | } else { |
michael@0 | 424 | self._marker.style.top = (offsets.y + dropon.clientHeight) + 'px'; |
michael@0 | 425 | } |
michael@0 | 426 | } |
michael@0 | 427 | MochiKit.Style.showElement(self._marker); |
michael@0 | 428 | }, |
michael@0 | 429 | |
michael@0 | 430 | _tree: function (element, options, parent) { |
michael@0 | 431 | var self = MochiKit.Sortable; |
michael@0 | 432 | var children = self.findElements(element, options) || []; |
michael@0 | 433 | |
michael@0 | 434 | for (var i = 0; i < children.length; ++i) { |
michael@0 | 435 | var match = children[i].id.match(options.format); |
michael@0 | 436 | |
michael@0 | 437 | if (!match) { |
michael@0 | 438 | continue; |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | var child = { |
michael@0 | 442 | id: encodeURIComponent(match ? match[1] : null), |
michael@0 | 443 | element: element, |
michael@0 | 444 | parent: parent, |
michael@0 | 445 | children: [], |
michael@0 | 446 | position: parent.children.length, |
michael@0 | 447 | container: self._findChildrenElement(children[i], options.treeTag.toUpperCase()) |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | /* Get the element containing the children and recurse over it */ |
michael@0 | 451 | if (child.container) { |
michael@0 | 452 | self._tree(child.container, options, child) |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | parent.children.push (child); |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | return parent; |
michael@0 | 459 | }, |
michael@0 | 460 | |
michael@0 | 461 | /* Finds the first element of the given tag type within a parent element. |
michael@0 | 462 | Used for finding the first LI[ST] within a L[IST]I[TEM].*/ |
michael@0 | 463 | _findChildrenElement: function (element, containerTag) { |
michael@0 | 464 | if (element && element.hasChildNodes) { |
michael@0 | 465 | containerTag = containerTag.toUpperCase(); |
michael@0 | 466 | for (var i = 0; i < element.childNodes.length; ++i) { |
michael@0 | 467 | if (element.childNodes[i].tagName.toUpperCase() == containerTag) { |
michael@0 | 468 | return element.childNodes[i]; |
michael@0 | 469 | } |
michael@0 | 470 | } |
michael@0 | 471 | } |
michael@0 | 472 | return null; |
michael@0 | 473 | }, |
michael@0 | 474 | |
michael@0 | 475 | /** @id MochiKit.Sortable.tree */ |
michael@0 | 476 | tree: function (element, options) { |
michael@0 | 477 | element = MochiKit.DOM.getElement(element); |
michael@0 | 478 | var sortableOptions = MochiKit.Sortable.options(element); |
michael@0 | 479 | options = MochiKit.Base.update({ |
michael@0 | 480 | tag: sortableOptions.tag, |
michael@0 | 481 | treeTag: sortableOptions.treeTag, |
michael@0 | 482 | only: sortableOptions.only, |
michael@0 | 483 | name: element.id, |
michael@0 | 484 | format: sortableOptions.format |
michael@0 | 485 | }, options || {}); |
michael@0 | 486 | |
michael@0 | 487 | var root = { |
michael@0 | 488 | id: null, |
michael@0 | 489 | parent: null, |
michael@0 | 490 | children: new Array, |
michael@0 | 491 | container: element, |
michael@0 | 492 | position: 0 |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | return MochiKit.Sortable._tree(element, options, root); |
michael@0 | 496 | }, |
michael@0 | 497 | |
michael@0 | 498 | /** |
michael@0 | 499 | * Specifies the sequence for the Sortable. |
michael@0 | 500 | * @param {Node} element Element to use as the Sortable. |
michael@0 | 501 | * @param {Object} newSequence New sequence to use. |
michael@0 | 502 | * @param {Object} options Options to use fro the Sortable. |
michael@0 | 503 | */ |
michael@0 | 504 | setSequence: function (element, newSequence, options) { |
michael@0 | 505 | var self = MochiKit.Sortable; |
michael@0 | 506 | var b = MochiKit.Base; |
michael@0 | 507 | element = MochiKit.DOM.getElement(element); |
michael@0 | 508 | options = b.update(self.options(element), options || {}); |
michael@0 | 509 | |
michael@0 | 510 | var nodeMap = {}; |
michael@0 | 511 | b.map(function (n) { |
michael@0 | 512 | var m = n.id.match(options.format); |
michael@0 | 513 | if (m) { |
michael@0 | 514 | nodeMap[m[1]] = [n, n.parentNode]; |
michael@0 | 515 | } |
michael@0 | 516 | n.parentNode.removeChild(n); |
michael@0 | 517 | }, self.findElements(element, options)); |
michael@0 | 518 | |
michael@0 | 519 | b.map(function (ident) { |
michael@0 | 520 | var n = nodeMap[ident]; |
michael@0 | 521 | if (n) { |
michael@0 | 522 | n[1].appendChild(n[0]); |
michael@0 | 523 | delete nodeMap[ident]; |
michael@0 | 524 | } |
michael@0 | 525 | }, newSequence); |
michael@0 | 526 | }, |
michael@0 | 527 | |
michael@0 | 528 | /* Construct a [i] index for a particular node */ |
michael@0 | 529 | _constructIndex: function (node) { |
michael@0 | 530 | var index = ''; |
michael@0 | 531 | do { |
michael@0 | 532 | if (node.id) { |
michael@0 | 533 | index = '[' + node.position + ']' + index; |
michael@0 | 534 | } |
michael@0 | 535 | } while ((node = node.parent) != null); |
michael@0 | 536 | return index; |
michael@0 | 537 | }, |
michael@0 | 538 | |
michael@0 | 539 | /** @id MochiKit.Sortable.sequence */ |
michael@0 | 540 | sequence: function (element, options) { |
michael@0 | 541 | element = MochiKit.DOM.getElement(element); |
michael@0 | 542 | var self = MochiKit.Sortable; |
michael@0 | 543 | var options = MochiKit.Base.update(self.options(element), options || {}); |
michael@0 | 544 | |
michael@0 | 545 | return MochiKit.Base.map(function (item) { |
michael@0 | 546 | return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; |
michael@0 | 547 | }, MochiKit.DOM.getElement(self.findElements(element, options) || [])); |
michael@0 | 548 | }, |
michael@0 | 549 | |
michael@0 | 550 | /** |
michael@0 | 551 | * Serializes the content of a Sortable. Useful to send this content through a XMLHTTPRequest. |
michael@0 | 552 | * These options override the Sortable options for the serialization only. |
michael@0 | 553 | * @param {Node} element Element to serialize. |
michael@0 | 554 | * @param {Object} options Serialization options. |
michael@0 | 555 | */ |
michael@0 | 556 | serialize: function (element, options) { |
michael@0 | 557 | element = MochiKit.DOM.getElement(element); |
michael@0 | 558 | var self = MochiKit.Sortable; |
michael@0 | 559 | options = MochiKit.Base.update(self.options(element), options || {}); |
michael@0 | 560 | var name = encodeURIComponent(options.name || element.id); |
michael@0 | 561 | |
michael@0 | 562 | if (options.tree) { |
michael@0 | 563 | return MochiKit.Base.flattenArray(MochiKit.Base.map(function (item) { |
michael@0 | 564 | return [name + self._constructIndex(item) + "[id]=" + |
michael@0 | 565 | encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); |
michael@0 | 566 | }, self.tree(element, options).children)).join('&'); |
michael@0 | 567 | } else { |
michael@0 | 568 | return MochiKit.Base.map(function (item) { |
michael@0 | 569 | return name + "[]=" + encodeURIComponent(item); |
michael@0 | 570 | }, self.sequence(element, options)).join('&'); |
michael@0 | 571 | } |
michael@0 | 572 | } |
michael@0 | 573 | }); |
michael@0 | 574 | |
michael@0 | 575 | // trunk compatibility |
michael@0 | 576 | MochiKit.Sortable.Sortable = MochiKit.Sortable; |
michael@0 | 577 | |
michael@0 | 578 | MochiKit.Sortable.__new__ = function () { |
michael@0 | 579 | MochiKit.Base.nameFunctions(this); |
michael@0 | 580 | |
michael@0 | 581 | this.EXPORT_TAGS = { |
michael@0 | 582 | ":common": this.EXPORT, |
michael@0 | 583 | ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK) |
michael@0 | 584 | }; |
michael@0 | 585 | }; |
michael@0 | 586 | |
michael@0 | 587 | MochiKit.Sortable.__new__(); |
michael@0 | 588 | |
michael@0 | 589 | MochiKit.Base._exportSymbols(this, MochiKit.Sortable); |