Permalink
Browse files

- Added inject / eject methods to ART.Widget. They also handle regis…

…tration and unregistration. It is possible to register a widget with a parent widget and inject it into another element (such as a child element of the parent widget) with the second argument for inject.

 - Removed the redundant collection in ART.Widget.
 - The deferDraw method will exit immediately if the widget's element is not in the DOM. Draw is not called on initialize anymore, only on inject (and the other places where it used to be called from).
  • Loading branch information...
1 parent 1bcf5e3 commit 503bffee90289fef6b885c859a2ee97913c6cade @kamicane committed Apr 19, 2010
Showing with 31 additions and 13 deletions.
  1. +31 −13 Source/ART.Widget.js
View
@@ -10,22 +10,21 @@ provides: ART.Widget
(function(){
var Widget = ART.Widget = new Class({
+
Extends: UI.Widget,
+
options: {
tabIndex: -1
- }
-});
-
-var widgets = ART.widgets = [];
-
-Widget.implement({
+ },
initialize: function(options){
this.element = new Element('div').setStyles({display: 'inline-block', position: 'relative', outline: 'none'});
this.canvas = new ART;
$(this.canvas).setStyles({position: 'absolute', top: 0, left: 0}).inject(this.element);
this.currentSheet = {};
+ this.parent(options);
+
this.setTabIndex(this.options.tabIndex);
var self = this;
@@ -41,12 +40,6 @@ Widget.implement({
}
});
-
- this.parent(options);
-
- widgets.push(this);
-
- this.deferDraw();
anutron
anutron Apr 20, 2010

widgets don't draw on creation? what of widgets that aren't injected into others? what renders them?

kamicane
kamicane Apr 20, 2010 Owner

Inject is not limited to other widgets, but any element as well.

inject(document.body) //the widget draws, and is registered without any parent widget

},
/* tab indices */
@@ -74,10 +67,11 @@ Widget.implement({
},
deferDraw: function(){
+ if (!this.element.parentNode) return;
+
var self = this;
clearTimeout(this.drawTimer);
this.drawTimer = setTimeout(function(){
- // console.log('»', self.id, ':', 'The method', name, 'succeded in getting a redraw.');
self.draw();
}, 1);
},
@@ -150,6 +144,30 @@ Widget.implement({
return true;
},
+ /* DOM + Registration */
+
+ inject: function(widget, element){
+ element = (element) ? $(element) : $(widget);
+
+ if (element && this.element.parentNode !== element){
+ this.register(widget);
+ this.element.inject(element);
+ this.deferDraw();
+ }
+
+ return this;
+ },
+
+ eject: function(){
+ if (this.element.parentNode){ // continue only if the element is in the dom
+ this.element.dispose();
+ this.unregister();
+ // even though deferDraw will not fire when the element is not in the dom, this will cancel any pre-existing draw request.
+ clearTimeout(this.drawTimer);
+ }
+ return this;
+ },
+
/* $ */
toElement: function(){

1 comment on commit 503bffe

Inviz commented on 503bffe Apr 27, 2010

So painful to see someone doing this again... :)

Please sign in to comment.