Author: reebalazs
Date: Thu May 17 10:48:25 2007
New Revision: 43449
Modified:
kukit/azax/branch/1.1-ree-plugin-dad/plugins/draganddrop/browser/kss_dragdrop.js
Log:
Add support for sortables
Modified:
kukit/azax/branch/1.1-ree-plugin-dad/plugins/draganddrop/browser/kss_dragdrop.js
==============================================================================
---
kukit/azax/branch/1.1-ree-plugin-dad/plugins/draganddrop/browser/kss_dragdrop.js
(original)
+++
kukit/azax/branch/1.1-ree-plugin-dad/plugins/draganddrop/browser/kss_dragdrop.js
Thu May 17 10:48:25 2007
@@ -75,7 +75,7 @@
kukit.draganddrop.DragAndDropEvent.prototype.__bind_drag__ =
function(opers_by_eventname, node) {
// set options for dnd
- var options = {}
+ var options = {};
if (opers_by_eventname.drag) {
var oper = opers_by_eventname.drag;
oper.completeParms([], {'constraint' : 'none', 'revert': ''},
'dad-drag event binding');
@@ -85,7 +85,7 @@
throw 'Unknown dad-drag constraint "' + oper.parms.constraint +
'"';
}
if (oper.hasExecuteActions())
- options.onDrag = opers_by_eventname.drag.makeExecuteActionsHook();
+ options.onDrag = oper.makeExecuteActionsHook();
if (oper.parms.constraint != 'none')
options.constraint = oper.parms.constraint;
}
@@ -123,7 +123,7 @@
kukit.draganddrop.DragAndDropEvent.prototype.__bind_drop__ =
function(opers_by_eventname, node) {
// set options for dnd
- var options = {}
+ var options = {};
if (opers_by_eventname.hover) {
var oper = opers_by_eventname.hover;
oper.completeParms([], {'hoverclass': ''}, 'dad-hover event binding');
@@ -189,3 +189,171 @@
});
kukit.commandsGlobalRegistry.registerFromAction('dad-resetPosition',
kukit.cr.makeSelectorCommand);
+
+/*
+ * Event binder for sortables
+ *
+ * Comments:
+ *
+ * In particular there are the following problems:
+ *
+ * 1. The Sortables code builds on Draggable and Droppable in such a way that
it hides
+ * Draggable and Droppable, and it is not possible to extend them or use
their original
+ * event hooks in case they are used from Sortable. This means that we have
a totally
+ * different way of working with Sortables then with drag and drop.
+ *
+ * 2. The Sortables define an onUpdate event that is taking the entire list of
contained
+ * ids as an attribute. Since we rather want a "drop" event, this gets
implemented but
+ * again, since we cannot access the original drag and drop events from
here, we make
+ * this emulated that requires a painful processing of the entire list.
+ *
+ * 3. The Sortables object is a singleton. This is a problem because all
sortable container
+ * will act as one common pool. This is a problem because this way we
cannot have more
+ * independent sortable pools in the screen. This is a limitation that
unfortunately
+ * we cannot live with. (Note. in kss we could use "binder ids" to have
this work in a natural
+ * way, in case the lib supported it.)
+ *
+ * The above problems seems to be solvable only by changing the original
scriptaculous code,
+ * or at least include the necessary hooks into it so that we can use
inheritence to extend
+ * it.
+ *
+ */
+
+kukit.draganddrop.SortableEvent = function() {
+};
+
+// This one takes a list of items, items contain nodes and opers_by_eventname
+kukit.draganddrop.SortableEvent.prototype.__bind__ = function(allitems) {
+ var bindings = [];
+ var containment = [];
+ for (var i=0; i < allitems.length; i++) {
+ var item = allitems[i];
+ var node = item.node;
+ var opers_by_eventname = item.opers_by_eventname;
+ // set options for dnd
+ var options = {};
+ if (! node || ! node.id) {
+ throw 'dad-sortupdate and dad-sortchange must bind to nodes with
id.';
+ }
+ var tagname = node.tagName.toLowerCase();
+ if (tagname == 'table' || tagname == 'tnode' || tagname == 'tbody' ||
tagname == 'tr') {
+ throw 'dad-sortupdate and dad-sortchange is not allowed to bind on
the following nodes: table, tnode, tbody, tr';
+ }
+ if (opers_by_eventname.sortupdate) {
+ var oper = opers_by_eventname.sortupdate;
+ oper.completeParms([], {'constraint' : 'none', 'overlap':
'vertical', 'tag': 'li', 'dropOnEmpty': 'true'}, 'dad-sortupdate event
binding');
+ if (oper.parms.constraint != 'horizontal' && oper.parms.constraint
!= 'vertical' && oper.parms.constraint != 'none') {
+ throw 'Unknown dad-sortupdate constraint "' +
oper.parms.constraint + '"';
+ }
+ oper.evalBool('dropOnEmpty');
+ if (oper.parms.overlap != 'horizontal' && oper.parms.overlap !=
'vertical') {
+ throw 'Unknown dad-sortupdate overlap "' + oper.parms.overlap
+ '"';
+ }
+ options.overlap = oper.parms.overlap;
+ if (oper.parms.constraint != 'none')
+ options.constraint = oper.parms.constraint;
+ if (oper.hasExecuteActions())
+ var f = oper.makeExecuteActionsHook();
+ var self = this;
+ options.onUpdate = function(element) {
+ var defaultparms = self.collectDefaultParms(element);
+ f({defaultparms: defaultparms});
+ // continue with the drop event, if we dropped here.
+ if (defaultparms.dropid) {
+ self.__continue_event__('sortdrop', oper.node,
defaultparms);
+ }
+ };
+ options.tag = oper.parms.tag;
+ options.dropOnEmpty = oper.parms.dropOnEmpty;
+ }
+ if (opers_by_eventname.sortchange) {
+ var oper = opers_by_eventname.sortchange;
+ oper.completeParms([], {}, 'dad-sortchange event binding');
+ if (oper.hasExecuteActions())
+ options.onChange = oper.makeExecuteActionsHook();
+ }
+ // make container sortable
+ bindings.push({node: node, options: options});
+ containment.push(node);
+ }
+ // Now bind all the nodes together, this was needed because we wanted to
collect
+ // the containers.
+ for (var i=0; i < bindings.length; i++) {
+ var binding = bindings[i];
+ binding.options.containment = containment;
+ var options = binding.options;
+ if (! options.contraints)
+ // need to set it to false, because the default value
+ // is really vertical...
+ options.constraint = false;
+ // store the original ids
+ Sortable.create(binding.node, options);
+ // store original order on options
+ var realoptions = Sortable.options(binding.node);
+ realoptions.originalIds = this.getOrderedIds(binding.node);
+ }
+};
+
+// all the next havoc is to emulate a "drop" event with the necessary default
parms.
+// This could become unnecessary if the original code supported our needs.
+kukit.draganddrop.SortableEvent.prototype.collectDefaultParms =
function(element) {
+ // element is the container node now.
+ var parms = {};
+ var newIds = this.getOrderedIds(element);
+ parms.idlist = newIds.join(' ');
+ // Take the result. We need which element is dropped to where, and not the
+ // entire list. Unfortunately this is not possible without copypasting the
+ // full scriptaculous code and inserting the necessary hooks. So instead we
+ // take the result as list, and try to deduct the difference.
+ var options = Sortable.options(element);
+ var originalIds = options.originalIds;
+ // update the list in the options
+ options.originalIds = newIds;
+ if (originalIds.length > newIds.length) {
+ // we did not drop an element here.
+ return parms;
+ }
+ // find the first place where they differ
+ for (var i=0; i<originalIds.length; i++) {
+ if (originalIds[i] != newIds[i]) {
+ break;
+ }
+ }
+ // We found the first differing element. See the next one...
+ if (newIds[i+1] == originalIds[i]) {
+ parms.dragid = newIds[i];
+ parms.dropid = newIds[i+1];
+ } else if (newIds[i] == originalIds[i+1]) {
+ // now we found the dragged elem, we just need to find
+ // where it was dropped
+ parms.dragid = originalIds[i];
+ for (i=i+1; i<originalIds.length; i++) {
+ if (newIds[i] == parms.dragid) {
+ break;
+ }
+ }
+ parms.dropid = newIds[i+1];
+ } else {
+ throw 'Internal error, mismatch in sortables';
+ }
+ // supply the rest of the parameters
+ parms.drag = document.getElementById(parms.dragid);
+ parms.drop = document.getElementById(parms.dropid);
+ return parms;
+};
+
+kukit.draganddrop.SortableEvent.prototype.getOrderedIds = function(element) {
+ var options = Sortable.options(element);
+ if (options.tree) {
+ throw 'Tree not implemented';
+ } else {
+ var options = Object.extend(Sortable.options(element), arguments[1] ||
{});
+ return $(Sortable.findElements(element, options) || []).map(
function(item) {
+ return item.id;});
+ }
+};
+
+if (kukit.draganddrop.base_library_present) {
+ kukit.eventsGlobalRegistry.registerForAllEvents('dad', ['sortupdate',
'sortchange', 'sortdrop'], kukit.draganddrop.SortableEvent, '__bind__', null,
'allnodes');
+}
+
_______________________________________________
Kukit-checkins mailing list
[email protected]
http://codespeak.net/mailman/listinfo/kukit-checkins