Author: abelevich
Date: 2011-01-27 12:15:16 -0500 (Thu, 27 Jan 2011)
New Revision: 21288
Removed:
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-manager.js
Modified:
trunk/core/impl/src/main/resources/META-INF/resources/richfaces-base-component.js
trunk/core/impl/src/main/resources/META-INF/resources/richfaces.js
trunk/ui/dnd/ui/src/main/java/org/richfaces/javascript/DnDScript.java
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-draggable.js
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-droppable.js
Log:
https://issues.jboss.org/browse/RF-10234 drag-n-drop: move droppable and draggable to
base-component
Modified:
trunk/core/impl/src/main/resources/META-INF/resources/richfaces-base-component.js
===================================================================
---
trunk/core/impl/src/main/resources/META-INF/resources/richfaces-base-component.js 2011-01-27
16:40:36 UTC (rev 21287)
+++
trunk/core/impl/src/main/resources/META-INF/resources/richfaces-base-component.js 2011-01-27
17:15:16 UTC (rev 21288)
@@ -296,6 +296,97 @@
}
};
})(params));
+
+ richfaces.BaseNonVisualComponent = function(componentId) {
+ this.id = componentId;
+ this.options = this.options || {};
+ };
+
+ richfaces.BaseNonVisualComponent.extend = function(child, h) {
+ return extend(richfaces.BaseNonVisualComponent, child, h);
+ };
+
+ richfaces.BaseNonVisualComponent.extendClass = function (methods) {
+ var DerivedClass = methods.init || richfaces.blankFunction;
+ var SupperClass = this;
+
+ SupperClass.extend(DerivedClass);
+
+ DerivedClass.extendClass = SupperClass.extendClass;
+
+ $.extend(DerivedClass.prototype, methods);
+
+ return DerivedClass;
+ };
+
+ $.extend(richfaces.BaseNonVisualComponent.prototype, (function (params) {
+ return {
+ name: "BaseNonVisualComponent",
+
+ toString: function() {
+ var result = [];
+ if (this.constructor.$super) {
+ result[result.length] = this.constructor.$super.toString();
+ }
+ result[result.length] = this.name;
+ return result.join(', ');
+ },
+
+ getValue: function() {
+ return;
+ },
+ /**
+ * Attach component object to DOM element by component id, DOM element or jQuery object
and returns the element
+ * Its required for the client side API calls and to clean up after ajax request or
document unload by
+ * calling destroy method
+ *
+ * @function
+ * @name RichFaces.BaseNonVisualComponent#attachToDom
+ * @param {string|DOMElement|jQuery} source - component id, DOM element or DOM elements
wrapped by jQuery
+ *
+ * @return {DOMElement}
+ * */
+ attachToDom: function(source) {
+ source = source || this.id;
+ var element = richfaces.getDomElement(source);
+ if (element) {
+ var container = element[richfaces.RICH_CONTAINER] =
element[richfaces.RICH_CONTAINER] || {};
+ if (container.attachedComponents){
+ container.attachedComponents[this.name] = this;
+ } else {
+ container.attachedComponents= {};
+ container.attachedComponents[this.name] = this;
+ }
+ }
+ return element;
+ },
+
+ /**
+ * Detach component object from DOM element by component id, DOM element or jQuery
object
+ *
+ * @function
+ * @name RichFaces.BaseNonVisualComponent#detach
+ * @param {string|DOMElement|jQuery} source - component id, DOM element or DOM elements
wrapped by jQuery
+ *
+ * */
+ detach: function(source) {
+ source = source || this.id;
+ var element = richfaces.getDomElement(source);
+ element && element[richfaces.RICH_CONTAINER] &&
(element[richfaces.RICH_CONTAINER].attachedComponents[this.name]=null);
+ },
+
+ /**
+ * Destroy method. Will be called before remove component from the page
+ *
+ * @function
+ * @name RichFaces.BaseNonVisualComponent#destroy
+ *
+ * */
+ destroy: function() {
+ }
+ };
+ })(params));
+
})(jQuery, window.RichFaces || (window.RichFaces={}));
Modified: trunk/core/impl/src/main/resources/META-INF/resources/richfaces.js
===================================================================
--- trunk/core/impl/src/main/resources/META-INF/resources/richfaces.js 2011-01-27 16:40:36
UTC (rev 21287)
+++ trunk/core/impl/src/main/resources/META-INF/resources/richfaces.js 2011-01-27 17:15:16
UTC (rev 21288)
@@ -71,6 +71,13 @@
element = element.parentNode;
}
};
+ richfaces.findNonVisualComponents = function (source){
+ var element = richfaces.getDomElement(source);
+
+ if (element) {
+ return (element[richfaces.RICH_CONTAINER] || {})["attachedComponents"];
+ }
+ };
// find component and call his method
richfaces.invokeMethod = function(source, method) {
@@ -85,9 +92,19 @@
richfaces.cleanComponent = function (source) {
var component = richfaces.$(source);
if (component) {
+ //TODO fire destroy event
component.destroy();
component.detach(source);
}
+ var attachedComponents = richfaces.findNonVisualComponents(source);
+ if (attachedComponents) {
+ for (var i in attachedComponents)
+ {
+ if (attachedComponents[i]){
+ attachedComponents[i].destroy();
+ }
+ }
+ }
};
richfaces.cleanDom = function(source) {
Modified: trunk/ui/dnd/ui/src/main/java/org/richfaces/javascript/DnDScript.java
===================================================================
--- trunk/ui/dnd/ui/src/main/java/org/richfaces/javascript/DnDScript.java 2011-01-27
16:40:36 UTC (rev 21287)
+++ trunk/ui/dnd/ui/src/main/java/org/richfaces/javascript/DnDScript.java 2011-01-27
17:15:16 UTC (rev 21288)
@@ -41,6 +41,7 @@
BASE_RESOURCES.add(new ResourceKey("jquery.js", null));
BASE_RESOURCES.add(new ResourceKey("jquery.position.js", null));
BASE_RESOURCES.add(new ResourceKey("richfaces.js", null));
+ BASE_RESOURCES.add(new ResourceKey("richfaces-base-component.js",
null));
BASE_RESOURCES.add(new ResourceKey("richfaces-event.js", null));
BASE_RESOURCES.add(new ResourceKey("jquery-ui-core.js",
"org.richfaces"));
BASE_RESOURCES.add(new ResourceKey("jquery-dnd.js",
"org.richfaces"));
Modified:
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-draggable.js
===================================================================
---
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-draggable.js 2011-01-27
16:40:36 UTC (rev 21287)
+++
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-draggable.js 2011-01-27
17:15:16 UTC (rev 21288)
@@ -17,12 +17,17 @@
};
rf.ui.Draggable = function(id, options) {
+ this.options = {};
+ $.extend(this.options, defaultOptions, options || {});
+ $super.constructor.call(this, id);
+
this.id = id;
- this.options = options;
-
+
this.namespace = this.namespace || "."
+ rf.Event.createNamespace(this.name, this.id);
+ this.parentId = this.options.parentId;
+ this.attachToDom(this.parentId);
this.dragElement = $(document.getElementById(this.options.parentId));
this.dragElement.draggable();
@@ -40,18 +45,24 @@
this.dragElement.data('type', this.options.type);
this.dragElement.data("init", true);
this.dragElement.data("id", this.id);
-
- this.dragElement.bind('dragstart', $.proxy(this.dragStart, this));
- this.dragElement.bind('drag', $.proxy(this.drag, this));
- this.dragElement.bind('dragstop', $.proxy(this.dragStop, this));
+ rf.Event.bind(this.dragElement, 'dragstart'+this.namespace, this.dragStart,
this);
+ rf.Event.bind(this.dragElement, 'drag'+this.namespace, this.drag, this);
+ rf.Event.bind(this.dragElement, 'dragstop'+this.namespace, this.dragStop,
this);
};
+ rf.BaseNonVisualComponent.extend(rf.ui.Draggable);
+
+ // define super class link
+ var $super = rf.ui.Draggable.$super;
+
var defaultOptions = {
};
$.extend(rf.ui.Draggable.prototype, ( function () {
return {
- dragStart: function(e, ui) {
+ name : "Draggable",
+ dragStart: function(e) {
+ var ui = e.rf.data;
var element = ui.helper[0];
this.parentElement = element.parentNode;
ui.helper.detach().appendTo("body").setPosition(e).show();
@@ -65,7 +76,8 @@
}
},
- drag: function(e, ui) {
+ drag: function(e) {
+ var ui = e.rf.data;
if(this.__isCustomDragIndicator()) {
var indicator = rf.$(this.options.indicator);
if(indicator) {
@@ -77,7 +89,8 @@
this.__clearDraggableCss(ui.helper);
},
- dragStop: function(e, ui){
+ dragStop: function(e){
+ var ui = e.rf.data;
ui.helper.hide().detach().appendTo(this.parentElement);
if(ui.helper[0] != this.dragElement[0]) {
//fix to prevent remove custom indicator from DOM tree. see jQuery
draggable._clear method for details
@@ -94,6 +107,14 @@
//draggable 'addClasses: false' doesn't work so clear jQuery style
element.removeClass("ui-draggable-dragging");
}
+ },
+
+ destroy : function() {
+ // clean up code here
+ this.detach(this.parentId);
+ rf.Event.unbind(this.dragElement, this.namespace);
+ // call parent's destroy method
+ $super.destroy.call(this);
}
}
})());
Modified:
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-droppable.js
===================================================================
---
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-droppable.js 2011-01-27
16:40:36 UTC (rev 21287)
+++
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-droppable.js 2011-01-27
17:15:16 UTC (rev 21288)
@@ -15,24 +15,44 @@
acceptClass : "rf-ind-acpt",
draggingClass : "rf-ind-drag"
};
+
+ var defaultOptions = {
+ };
rf.ui.Droppable = function(id, options) {
- this.options = options;
+ this.options = {};
+ $.extend(this.options, defaultOptions, options || {});
+ $super.constructor.call(this, id);
+
+ this.namespace = this.namespace || "."
+ + rf.Event.createNamespace(this.name, this.id);
+
this.id = id;
-
- this.dropElement = $(document.getElementById(this.options.parentId));
+
+ this.parentId = this.options.parentId;
+
+ this.attachToDom(this.parentId);
+
+ this.dropElement = $(document.getElementById(this.parentId));
this.dropElement.droppable({
addClasses : false
});
this.dropElement.data("init", true);
- this.dropElement.bind('drop', $.proxy(this.drop, this));
- this.dropElement.bind('dropover', $.proxy(this.dropover, this));
- this.dropElement.bind('dropout', $.proxy(this.dropout, this));
+
+ rf.Event.bind(this.dropElement, 'drop'+this.namespace, this.drop, this);
+ rf.Event.bind(this.dropElement, 'dropover'+this.namespace, this.dropover,
this);
+ rf.Event.bind(this.dropElement, 'dropout'+this.namespace, this.dropout, this);
+
};
-
+
+ rf.BaseNonVisualComponent.extend(rf.ui.Droppable);
+
+ var $super = rf.ui.Droppable.$super;
+
$.extend(rf.ui.Droppable.prototype, (function() {
return {
- drop : function(e, ui) {
+ drop : function(e) {
+ var ui = e.rf.data;
if (this.accept(ui.draggable)) {
this.__callAjax(e, ui);
}
@@ -47,7 +67,8 @@
}
},
- dropover : function(event, ui) {
+ dropover : function(e) {
+ var ui = e.rf.data;
var draggable = ui.draggable;
var dragIndicatorObj = rf.$(ui.helper.attr("id"));
if (dragIndicatorObj) {
@@ -69,7 +90,8 @@
}
},
- dropout : function(event, ui) {
+ dropout : function(e) {
+ var ui = e.rf.data;
var draggable = ui.draggable;
var dragIndicatorObj = rf.$(ui.helper.attr("id"));
if (dragIndicatorObj) {
@@ -107,7 +129,19 @@
ajaxFunc.call(this, e, dragSource);
}
}
+ },
+
+ destroy : function() {
+ // clean up code here
+ this.detach(this.parentId);
+ rf.Event.unbind(this.dropElement, this.namespace);
+
+ // call parent's destroy method
+ $super.destroy.call(this);
+
}
+
+
}
})());
Deleted:
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-manager.js
===================================================================
---
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-manager.js 2011-01-27
16:40:36 UTC (rev 21287)
+++
trunk/ui/dnd/ui/src/main/resources/META-INF/resources/org.richfaces/dnd-manager.js 2011-01-27
17:15:16 UTC (rev 21288)
@@ -1,51 +0,0 @@
-/*
- * code review by Pavel Yaschenko
- *
- * records in draggables and droppables should be cleaned up when remove component from
DOM
- *
- * draft code: something like this
- *
- *
- rf.ui.Draggable = function (id, options) {
- var c = rf.$(id);
- if (c) {
- var baseDestroy = c.destroy;
- c. destroy = f ()
- {
- rf.ui.DnDManager.removeDraggable(id)
- baseDestroy.call(this);
- }
- }
- // other code ...
- }
- */
-
-(function ($, rf) {
-
- rf.ui = rf.ui || {};
-
- rf.ui.DnDManager = rf.ui.DnDManager || {};
-
- $.extend(rf.ui.DnDManager, ( function () {
- var draggables = {};
- var droppables = {};
-
- return {
- draggable: function(event, id, options) {
- var draggable = draggables[id];
- if(!draggable) {
- options['event'] = event;
- draggables[id] = new rf.ui.Draggable(id, options);
- }
- },
-
- droppable: function(event, id, options) {
- var droppable = droppables[id];
- if(!droppable) {
- options['event'] = event;
- droppables[id] = new rf.ui.Droppable(id, options);
- }
- }
- }
- })());
-})(jQuery, window.RichFaces);
\ No newline at end of file