r925 - branches/experimental/ui
Author: paul.bakaus
Date: Wed Nov 12 06:22:24 2008
New Revision: 925
Modified:
branches/experimental/ui/ui.menu.js
Log:
menu: implemented remove and replace methods (still missing: change)
Modified: branches/experimental/ui/ui.menu.js
==============================================================================
--- branches/experimental/ui/ui.menu.js (original)
+++ branches/experimental/ui/ui.menu.js Wed Nov 12 06:22:24 2008
@@ -386,49 +386,72 @@
},
+ _resolvePosition: function(position) {
+
+ var hash = '> ', old = '', append, dir = 'before';
-
- add: function(item, position) {
-
- var self = this;
- var item = $(item.constructor == String || item.jquery ? item :
this._generateMarkupFromJSON());
-
if(!position) {
- this.items.append(item);
+ append = true;
+ old = '> ul';
} else {
-
- position = position.split('/');
- var hash = '> ', old = '', append, dir = 'before';
+ position = (position+'').split('/');
for (var i=0; i < position.length; i++) {
old = hash; hash += 'ul > li:eq(' + (parseInt(position[i],10)-1)
+ ') ';
-
+
if(!$(hash, this.menu).length) {
-
if($(old + 'ul > li:eq(' + (parseInt(position[i],10)-2) + ')',
this.menu).length) {
hash = old + 'ul > li:eq(' + (parseInt(position[i],10)-2) + ')';
dir = 'after';
- } else {
- append = true;
- }
+ } else { append = true; }
break;
-
}
};
-
- if(!append) { //We want to have it before/after a node
- $(hash, this.menu)[dir](item);
- } else {
-
- if(!$(old, this.menu).find('ul').length) {
- $(old, this.menu).append('<ul></ul>');
- this._attachFlyoutStyles($(old, this.menu)[0]);
- }
-
- $(old, this.menu).find('ul').append(item);
+ }
+ return {
+ selector: append ? old : hash,
+ action: append ? 'append' : dir
+ };
+
+ },
+
+ remove: function(position) {
+
+ if(!position) return;
+ $(this._resolvePosition(position).selector, this.menu).remove();
+
+ },
+
+ replace: function(item, position) {
+
+ var position = this._resolvePosition(position);
+ if(position.action == 'append') return; //We can't replace something
that wasn't found
+
+ var oldItem = $(position.selector, this.menu);
+ this.add(item, position);
+ oldItem.remove();
+
+ },
+
+ add: function(item, position) {
+
+ var self = this;
+ var item = $(item.constructor == String || item.jquery ? item :
this._generateMarkupFromJSON());
+ position = position.selector ? position :
this._resolvePosition(position);
+
+ if(position.action != 'append') { //We want to have it before/after a
node
+ $(position.selector, this.menu)[position.action](item);
+ } else {
+
+ if(!$(position.selector, this.menu).find('ul').length) {
+ $(position.selector, this.menu).append('<ul></ul>');
+ this._attachFlyoutStyles($(position.selector, this.menu)[0]);
}
+
+ $(position.selector, this.menu).find('ul')[position.action](item);
}
+
if(this.options.type == 'drilldown') {