r1272 - in branches/dev/menu: tests/visual/menu ui
Author: paul.bakaus
Date: Mon Dec 29 07:35:10 2008
New Revision: 1272
Modified:
branches/dev/menu/tests/visual/menu/menu.html
branches/dev/menu/ui/ui.menu.js
Log:
menu: fixed add/remove/replace syntax, now uses jQuery selectors and a
third argument to define the insertion type ('before/after/append')
Modified: branches/dev/menu/tests/visual/menu/menu.html
==============================================================================
--- branches/dev/menu/tests/visual/menu/menu.html (original)
+++ branches/dev/menu/tests/visual/menu/menu.html Mon Dec 29 07:35:10 2008
@@ -53,8 +53,8 @@
},
open: function(event, ui) {
- $(this).menu('add', '<li><a href="#">Test Item 1</a></li>', '7');
- $(this).menu('add', '<li><a href="#">Test Item 2</a><ul><li><a
href="#">Test Item Sub</a></li></ul></li>', '1/1');
+ //$(this).menu('add', '<li><a href="#">Test Item 1</a></li>', '>
li:eq(5)', 'after');
+ $(this).menu('add', '<li><a href="#">Test Item 2</a><ul><li><a
href="#">Test Item Sub</a></li></ul></li>', ' > li:eq(0)', 'append');
//console.log('Opened menu');
},
Modified: branches/dev/menu/ui/ui.menu.js
==============================================================================
--- branches/dev/menu/ui/ui.menu.js (original)
+++ branches/dev/menu/ui/ui.menu.js Mon Dec 29 07:35:10 2008
@@ -421,72 +421,37 @@
},
- _resolvePosition: function(position) {
-
- var hash = '> ', old = '', append, dir = 'before';
-
- if(!position) {
- append = true;
- old = '> ul';
- } else {
- 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; }
- break;
- }
- };
- }
-
- return {
- selector: append ? old : hash,
- action: append ? 'append' : dir
- };
-
- },
-
remove: function(position) {
-
- if(!position) return;
- $(this._resolvePosition(position).selector, this.menu).remove();
-
+ $(position, this.items).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);
+ var old = $(position, this.items);
+ if(old.length) return;
+
this.add(item, position);
- oldItem.remove();
+ old.remove();
},
- add: function(item, position) {
+ add: function(item, position, type) {
var self = this;
var item = $(item.constructor == String || item.jquery ? item :
this._generateMarkupFromJSON(item));
- position = position.selector || position.jquery ? position :
this._resolvePosition(position);
- if(position.action && position.action != 'append') { //We want to have
it before/after a node
- $(position.selector, this.menu)[position.action](item);
+ if(type != 'append') { //We want to have it before/after a node
+ $(position, this.items)[type](item);
} else {
- if(!$(position.selector || position, this.menu).find('ul').length) {
- $(position.selector || position, this.menu).append('<ul></ul>');
- this._attachFlyoutStyles($(position.selector || position,
this.menu)[0]);
+ if(!$(position, this.items).find('ul').length) {
+ $(position, this.items).append('<ul></ul>');
+ this._attachFlyoutStyles($(position, this.items)[0]);
}
//We have to remove the outer ul that served as a wrapper, because we
already have a ul
if(item.is('ul')) item = $('> *', item);
-
- $(position.selector || position, this.menu).find('ul').append(item);
+ $(position, this.items).find('ul:eq(0)').append(item);
}