Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 206
- Log:
Initial import of Gullery, an open source photo gallery:
http://nubyonrails.com/pages/gullery
- Author:
- rool
- Date:
- Sun May 20 19:05:59 +0100 2007
- Size:
- 18709 Bytes
1 | // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) |
2 | // |
3 | // See scriptaculous.js for full license. |
4 | |
5 | /*--------------------------------------------------------------------------*/ |
6 | |
7 | var Droppables = { |
8 | drops: [], |
9 | |
10 | remove: function(element) { |
11 | this.drops = this.drops.reject(function(d) { return d.element==$(element) }); |
12 | }, |
13 | |
14 | add: function(element) { |
15 | element = $(element); |
16 | var options = Object.extend({ |
17 | greedy: true, |
18 | hoverclass: null |
19 | }, arguments[1] || {}); |
20 | |
21 | // cache containers |
22 | if(options.containment) { |
23 | options._containers = []; |
24 | var containment = options.containment; |
25 | if((typeof containment == 'object') && |
26 | (containment.constructor == Array)) { |
27 | containment.each( function(c) { options._containers.push($(c)) }); |
28 | } else { |
29 | options._containers.push($(containment)); |
30 | } |
31 | } |
32 | |
33 | if(options.accept) options.accept = [options.accept].flatten(); |
34 | |
35 | Element.makePositioned(element); // fix IE |
36 | options.element = element; |
37 | |
38 | this.drops.push(options); |
39 | }, |
40 | |
41 | isContained: function(element, drop) { |
42 | var parentNode = element.parentNode; |
43 | return drop._containers.detect(function(c) { return parentNode == c }); |
44 | }, |
45 | |
46 | isAffected: function(point, element, drop) { |
47 | return ( |
48 | (drop.element!=element) && |
49 | ((!drop._containers) || |
50 | this.isContained(element, drop)) && |
51 | ((!drop.accept) || |
52 | (Element.classNames(element).detect( |
53 | function(v) { return drop.accept.include(v) } ) )) && |
54 | Position.within(drop.element, point[0], point[1]) ); |
55 | }, |
56 | |
57 | deactivate: function(drop) { |
58 | if(drop.hoverclass) |
59 | Element.removeClassName(drop.element, drop.hoverclass); |
60 | this.last_active = null; |
61 | }, |
62 | |
63 | activate: function(drop) { |
64 | if(drop.hoverclass) |
65 | Element.addClassName(drop.element, drop.hoverclass); |
66 | this.last_active = drop; |
67 | }, |
68 | |
69 | show: function(point, element) { |
70 | if(!this.drops.length) return; |
71 | |
72 | if(this.last_active) this.deactivate(this.last_active); |
73 | this.drops.each( function(drop) { |
74 | if(Droppables.isAffected(point, element, drop)) { |
75 | if(drop.onHover) |
76 | drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); |
77 | if(drop.greedy) { |
78 | Droppables.activate(drop); |
79 | throw $break; |
80 | } |
81 | } |
82 | }); |
83 | }, |
84 | |
85 | fire: function(event, element) { |
86 | if(!this.last_active) return; |
87 | Position.prepare(); |
88 | |
89 | if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) |
90 | if (this.last_active.onDrop) |
91 | this.last_active.onDrop(element, this.last_active.element, event); |
92 | }, |
93 | |
94 | reset: function() { |
95 | if(this.last_active) |
96 | this.deactivate(this.last_active); |
97 | } |
98 | } |
99 | |
100 | var Draggables = { |
101 | drags: [], |
102 | observers: [], |
103 | |
104 | register: function(draggable) { |
105 | if(this.drags.length == 0) { |
106 | this.eventMouseUp = this.endDrag.bindAsEventListener(this); |
107 | this.eventMouseMove = this.updateDrag.bindAsEventListener(this); |
108 | this.eventKeypress = this.keyPress.bindAsEventListener(this); |
109 | |
110 | Event.observe(document, "mouseup", this.eventMouseUp); |
111 | Event.observe(document, "mousemove", this.eventMouseMove); |
112 | Event.observe(document, "keypress", this.eventKeypress); |
113 | } |
114 | this.drags.push(draggable); |
115 | }, |
116 | |
117 | unregister: function(draggable) { |
118 | this.drags = this.drags.reject(function(d) { return d==draggable }); |
119 | if(this.drags.length == 0) { |
120 | Event.stopObserving(document, "mouseup", this.eventMouseUp); |
121 | Event.stopObserving(document, "mousemove", this.eventMouseMove); |
122 | Event.stopObserving(document, "keypress", this.eventKeypress); |
123 | } |
124 | }, |
125 | |
126 | activate: function(draggable) { |
127 | window.focus(); // allows keypress events if window isn't currently focused, fails for Safari |
128 | this.activeDraggable = draggable; |
129 | }, |
130 | |
131 | deactivate: function(draggbale) { |
132 | this.activeDraggable = null; |
133 | }, |
134 | |
135 | updateDrag: function(event) { |
136 | if(!this.activeDraggable) return; |
137 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; |
138 | // Mozilla-based browsers fire successive mousemove events with |
139 | // the same coordinates, prevent needless redrawing (moz bug?) |
140 | if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; |
141 | this._lastPointer = pointer; |
142 | this.activeDraggable.updateDrag(event, pointer); |
143 | }, |
144 | |
145 | endDrag: function(event) { |
146 | if(!this.activeDraggable) return; |
147 | this._lastPointer = null; |
148 | this.activeDraggable.endDrag(event); |
149 | }, |
150 | |
151 | keyPress: function(event) { |
152 | if(this.activeDraggable) |
153 | this.activeDraggable.keyPress(event); |
154 | }, |
155 | |
156 | addObserver: function(observer) { |
157 | this.observers.push(observer); |
158 | this._cacheObserverCallbacks(); |
159 | }, |
160 | |
161 | removeObserver: function(element) { // element instead of observer fixes mem leaks |
162 | this.observers = this.observers.reject( function(o) { return o.element==element }); |
163 | this._cacheObserverCallbacks(); |
164 | }, |
165 | |
166 | notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' |
167 | if(this[eventName+'Count'] > 0) |
168 | this.observers.each( function(o) { |
169 | if(o[eventName]) o[eventName](eventName, draggable, event); |
170 | }); |
171 | }, |
172 | |
173 | _cacheObserverCallbacks: function() { |
174 | ['onStart','onEnd','onDrag'].each( function(eventName) { |
175 | Draggables[eventName+'Count'] = Draggables.observers.select( |
176 | function(o) { return o[eventName]; } |
177 | ).length; |
178 | }); |
179 | } |
180 | } |
181 | |
182 | /*--------------------------------------------------------------------------*/ |
183 | |
184 | var Draggable = Class.create(); |
185 | Draggable.prototype = { |
186 | initialize: function(element) { |
187 | var options = Object.extend({ |
188 | handle: false, |
189 | starteffect: function(element) { |
190 | new Effect.Opacity(element, {duration:0.2, from:1.0, to:0.7}); |
191 | }, |
192 | reverteffect: function(element, top_offset, left_offset) { |
193 | var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; |
194 | element._revert = new Effect.MoveBy(element, -top_offset, -left_offset, {duration:dur}); |
195 | }, |
196 | endeffect: function(element) { |
197 | new Effect.Opacity(element, {duration:0.2, from:0.7, to:1.0}); |
198 | }, |
199 | zindex: 1000, |
200 | revert: false, |
201 | snap: false // false, or xy or [x,y] or function(x,y){ return [x,y] } |
202 | }, arguments[1] || {}); |
203 | |
204 | this.element = $(element); |
205 | |
206 | if(options.handle && (typeof options.handle == 'string')) |
207 | this.handle = Element.childrenWithClassName(this.element, options.handle)[0]; |
208 | if(!this.handle) this.handle = $(options.handle); |
209 | if(!this.handle) this.handle = this.element; |
210 | |
211 | Element.makePositioned(this.element); // fix IE |
212 | |
213 | this.delta = this.currentDelta(); |
214 | this.options = options; |
215 | this.dragging = false; |
216 | |
217 | this.eventMouseDown = this.initDrag.bindAsEventListener(this); |
218 | Event.observe(this.handle, "mousedown", this.eventMouseDown); |
219 | |
220 | Draggables.register(this); |
221 | }, |
222 | |
223 | destroy: function() { |
224 | Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); |
225 | Draggables.unregister(this); |
226 | }, |
227 | |
228 | currentDelta: function() { |
229 | return([ |
230 | parseInt(this.element.style.left || '0'), |
231 | parseInt(this.element.style.top || '0')]); |
232 | }, |
233 | |
234 | initDrag: function(event) { |
235 | if(Event.isLeftClick(event)) { |
236 | // abort on form elements, fixes a Firefox issue |
237 | var src = Event.element(event); |
238 | if(src.tagName && ( |
239 | src.tagName=='INPUT' || |
240 | src.tagName=='SELECT' || |
241 | src.tagName=='BUTTON' || |
242 | src.tagName=='TEXTAREA')) return; |
243 | |
244 | if(this.element._revert) { |
245 | this.element._revert.cancel(); |
246 | this.element._revert = null; |
247 | } |
248 | |
249 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; |
250 | var pos = Position.cumulativeOffset(this.element); |
251 | this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); |
252 | |
253 | Draggables.activate(this); |
254 | Event.stop(event); |
255 | } |
256 | }, |
257 | |
258 | startDrag: function(event) { |
259 | this.dragging = true; |
260 | |
261 | if(this.options.zindex) { |
262 | this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); |
263 | this.element.style.zIndex = this.options.zindex; |
264 | } |
265 | |
266 | if(this.options.ghosting) { |
267 | this._clone = this.element.cloneNode(true); |
268 | Position.absolutize(this.element); |
269 | this.element.parentNode.insertBefore(this._clone, this.element); |
270 | } |
271 | |
272 | Draggables.notify('onStart', this, event); |
273 | if(this.options.starteffect) this.options.starteffect(this.element); |
274 | }, |
275 | |
276 | updateDrag: function(event, pointer) { |
277 | if(!this.dragging) this.startDrag(event); |
278 | Position.prepare(); |
279 | Droppables.show(pointer, this.element); |
280 | Draggables.notify('onDrag', this, event); |
281 | this.draw(pointer); |
282 | if(this.options.change) this.options.change(this); |
283 | |
284 | // fix AppleWebKit rendering |
285 | if(navigator.appVersion.indexOf('AppleWebKit')>0) window.scrollBy(0,0); |
286 | Event.stop(event); |
287 | }, |
288 | |
289 | finishDrag: function(event, success) { |
290 | this.dragging = false; |
291 | |
292 | if(this.options.ghosting) { |
293 | Position.relativize(this.element); |
294 | Element.remove(this._clone); |
295 | this._clone = null; |
296 | } |
297 | |
298 | if(success) Droppables.fire(event, this.element); |
299 | Draggables.notify('onEnd', this, event); |
300 | |
301 | var revert = this.options.revert; |
302 | if(revert && typeof revert == 'function') revert = revert(this.element); |
303 | |
304 | var d = this.currentDelta(); |
305 | if(revert && this.options.reverteffect) { |
306 | this.options.reverteffect(this.element, |
307 | d[1]-this.delta[1], d[0]-this.delta[0]); |
308 | } else { |
309 | this.delta = d; |
310 | } |
311 | |
312 | if(this.options.zindex) |
313 | this.element.style.zIndex = this.originalZ; |
314 | |
315 | if(this.options.endeffect) |
316 | this.options.endeffect(this.element); |
317 | |
318 | Draggables.deactivate(this); |
319 | Droppables.reset(); |
320 | }, |
321 | |
322 | keyPress: function(event) { |
323 | if(!event.keyCode==Event.KEY_ESC) return; |
324 | this.finishDrag(event, false); |
325 | Event.stop(event); |
326 | }, |
327 | |
328 | endDrag: function(event) { |
329 | if(!this.dragging) return; |
330 | this.finishDrag(event, true); |
331 | Event.stop(event); |
332 | }, |
333 | |
334 | draw: function(point) { |
335 | var pos = Position.cumulativeOffset(this.element); |
336 | var d = this.currentDelta(); |
337 | pos[0] -= d[0]; pos[1] -= d[1]; |
338 | |
339 | var p = [0,1].map(function(i){ return (point[i]-pos[i]-this.offset[i]) }.bind(this)); |
340 | |
341 | if(this.options.snap) { |
342 | if(typeof this.options.snap == 'function') { |
343 | p = this.options.snap(p[0],p[1]); |
344 | } else { |
345 | if(this.options.snap instanceof Array) { |
346 | p = p.map( function(v, i) { |
347 | return Math.round(v/this.options.snap[i])*this.options.snap[i] }.bind(this)) |
348 | } else { |
349 | p = p.map( function(v) { |
350 | return Math.round(v/this.options.snap)*this.options.snap }.bind(this)) |
351 | } |
352 | }} |
353 | |
354 | var style = this.element.style; |
355 | if((!this.options.constraint) || (this.options.constraint=='horizontal')) |
356 | style.left = p[0] + "px"; |
357 | if((!this.options.constraint) || (this.options.constraint=='vertical')) |
358 | style.top = p[1] + "px"; |
359 | if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering |
360 | } |
361 | } |
362 | |
363 | /*--------------------------------------------------------------------------*/ |
364 | |
365 | var SortableObserver = Class.create(); |
366 | SortableObserver.prototype = { |
367 | initialize: function(element, observer) { |
368 | this.element = $(element); |
369 | this.observer = observer; |
370 | this.lastValue = Sortable.serialize(this.element); |
371 | }, |
372 | |
373 | onStart: function() { |
374 | this.lastValue = Sortable.serialize(this.element); |
375 | }, |
376 | |
377 | onEnd: function() { |
378 | Sortable.unmark(); |
379 | if(this.lastValue != Sortable.serialize(this.element)) |
380 | this.observer(this.element) |
381 | } |
382 | } |
383 | |
384 | var Sortable = { |
385 | sortables: new Array(), |
386 | |
387 | options: function(element){ |
388 | element = $(element); |
389 | return this.sortables.detect(function(s) { return s.element == element }); |
390 | }, |
391 | |
392 | destroy: function(element){ |
393 | element = $(element); |
394 | this.sortables.findAll(function(s) { return s.element == element }).each(function(s){ |
395 | Draggables.removeObserver(s.element); |
396 | s.droppables.each(function(d){ Droppables.remove(d) }); |
397 | s.draggables.invoke('destroy'); |
398 | }); |
399 | this.sortables = this.sortables.reject(function(s) { return s.element == element }); |
400 | }, |
401 | |
402 | create: function(element) { |
403 | element = $(element); |
404 | var options = Object.extend({ |
405 | element: element, |
406 | tag: 'li', // assumes li children, override with tag: 'tagname' |
407 | dropOnEmpty: false, |
408 | tree: false, // fixme: unimplemented |
409 | overlap: 'vertical', // one of 'vertical', 'horizontal' |
410 | constraint: 'vertical', // one of 'vertical', 'horizontal', false |
411 | containment: element, // also takes array of elements (or id's); or false |
412 | handle: false, // or a CSS class |
413 | only: false, |
414 | hoverclass: null, |
415 | ghosting: false, |
416 | format: null, |
417 | onChange: Prototype.emptyFunction, |
418 | onUpdate: Prototype.emptyFunction |
419 | }, arguments[1] || {}); |
420 | |
421 | // clear any old sortable with same element |
422 | this.destroy(element); |
423 | |
424 | // build options for the draggables |
425 | var options_for_draggable = { |
426 | revert: true, |
427 | ghosting: options.ghosting, |
428 | constraint: options.constraint, |
429 | handle: options.handle }; |
430 | |
431 | if(options.starteffect) |
432 | options_for_draggable.starteffect = options.starteffect; |
433 | |
434 | if(options.reverteffect) |
435 | options_for_draggable.reverteffect = options.reverteffect; |
436 | else |
437 | if(options.ghosting) options_for_draggable.reverteffect = function(element) { |
438 | element.style.top = 0; |
439 | element.style.left = 0; |
440 | }; |
441 | |
442 | if(options.endeffect) |
443 | options_for_draggable.endeffect = options.endeffect; |
444 | |
445 | if(options.zindex) |
446 | options_for_draggable.zindex = options.zindex; |
447 | |
448 | // build options for the droppables |
449 | var options_for_droppable = { |
450 | overlap: options.overlap, |
451 | containment: options.containment, |
452 | hoverclass: options.hoverclass, |
453 | onHover: Sortable.onHover, |
454 | greedy: !options.dropOnEmpty |
455 | } |
456 | |
457 | // fix for gecko engine |
458 | Element.cleanWhitespace(element); |
459 | |
460 | options.draggables = []; |
461 | options.droppables = []; |
462 | |
463 | // make it so |
464 | |
465 | // drop on empty handling |
466 | if(options.dropOnEmpty) { |
467 | Droppables.add(element, |
468 | {containment: options.containment, onHover: Sortable.onEmptyHover, greedy: false}); |
469 | options.droppables.push(element); |
470 | } |
471 | |
472 | (this.findElements(element, options) || []).each( function(e) { |
473 | // handles are per-draggable |
474 | var handle = options.handle ? |
475 | Element.childrenWithClassName(e, options.handle)[0] : e; |
476 | options.draggables.push( |
477 | new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); |
478 | Droppables.add(e, options_for_droppable); |
479 | options.droppables.push(e); |
480 | }); |
481 | |
482 | // keep reference |
483 | this.sortables.push(options); |
484 | |
485 | // for onupdate |
486 | Draggables.addObserver(new SortableObserver(element, options.onUpdate)); |
487 | |
488 | }, |
489 | |
490 | // return all suitable-for-sortable elements in a guaranteed order |
491 | findElements: function(element, options) { |
492 | if(!element.hasChildNodes()) return null; |
493 | var elements = []; |
494 | $A(element.childNodes).each( function(e) { |
495 | if(e.tagName && e.tagName.toUpperCase()==options.tag.toUpperCase() && |
496 | (!options.only || (Element.hasClassName(e, options.only)))) |
497 | elements.push(e); |
498 | if(options.tree) { |
499 | var grandchildren = this.findElements(e, options); |
500 | if(grandchildren) elements.push(grandchildren); |
501 | } |
502 | }); |
503 | |
504 | return (elements.length>0 ? elements.flatten() : null); |
505 | }, |
506 | |
507 | onHover: function(element, dropon, overlap) { |
508 | if(overlap>0.5) { |
509 | Sortable.mark(dropon, 'before'); |
510 | if(dropon.previousSibling != element) { |
511 | var oldParentNode = element.parentNode; |
512 | element.style.visibility = "hidden"; // fix gecko rendering |
513 | dropon.parentNode.insertBefore(element, dropon); |
514 | if(dropon.parentNode!=oldParentNode) |
515 | Sortable.options(oldParentNode).onChange(element); |
516 | Sortable.options(dropon.parentNode).onChange(element); |
517 | } |
518 | } else { |
519 | Sortable.mark(dropon, 'after'); |
520 | var nextElement = dropon.nextSibling || null; |
521 | if(nextElement != element) { |
522 | var oldParentNode = element.parentNode; |
523 | element.style.visibility = "hidden"; // fix gecko rendering |
524 | dropon.parentNode.insertBefore(element, nextElement); |
525 | if(dropon.parentNode!=oldParentNode) |
526 | Sortable.options(oldParentNode).onChange(element); |
527 | Sortable.options(dropon.parentNode).onChange(element); |
528 | } |
529 | } |
530 | }, |
531 | |
532 | onEmptyHover: function(element, dropon) { |
533 | if(element.parentNode!=dropon) { |
534 | var oldParentNode = element.parentNode; |
535 | dropon.appendChild(element); |
536 | Sortable.options(oldParentNode).onChange(element); |
537 | Sortable.options(dropon).onChange(element); |
538 | } |
539 | }, |
540 | |
541 | unmark: function() { |
542 | if(Sortable._marker) Element.hide(Sortable._marker); |
543 | }, |
544 | |
545 | mark: function(dropon, position) { |
546 | // mark on ghosting only |
547 | var sortable = Sortable.options(dropon.parentNode); |
548 | if(sortable && !sortable.ghosting) return; |
549 | |
550 | if(!Sortable._marker) { |
551 | Sortable._marker = $('dropmarker') || document.createElement('DIV'); |
552 | Element.hide(Sortable._marker); |
553 | Element.addClassName(Sortable._marker, 'dropmarker'); |
554 | Sortable._marker.style.position = 'absolute'; |
555 | document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); |
556 | } |
557 | var offsets = Position.cumulativeOffset(dropon); |
558 | Sortable._marker.style.left = offsets[0] + 'px'; |
559 | Sortable._marker.style.top = offsets[1] + 'px'; |
560 | |
561 | if(position=='after') |
562 | if(sortable.overlap == 'horizontal') |
563 | Sortable._marker.style.left = (offsets[0]+dropon.clientWidth) + 'px'; |
564 | else |
565 | Sortable._marker.style.top = (offsets[1]+dropon.clientHeight) + 'px'; |
566 | |
567 | Element.show(Sortable._marker); |
568 | }, |
569 | |
570 | serialize: function(element) { |
571 | element = $(element); |
572 | var sortableOptions = this.options(element); |
573 | var options = Object.extend({ |
574 | tag: sortableOptions.tag, |
575 | only: sortableOptions.only, |
576 | name: element.id, |
577 | format: sortableOptions.format || /^[^_]*_(.*)$/ |
578 | }, arguments[1] || {}); |
579 | return $(this.findElements(element, options) || []).map( function(item) { |
580 | return (encodeURIComponent(options.name) + "[]=" + |
581 | encodeURIComponent(item.id.match(options.format) ? item.id.match(options.format)[1] : '')); |
582 | }).join("&"); |
583 | } |
584 | } |