oxjs/source/Ox.UI/js/Menu/Ox.MainMenu.js

265 lines
6.8 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 08:24:46 +00:00
/*@
Ox.MainMenu <f:Ox.Bar> MainMenu Object
() -> <f> MainMenu Object
(options) -> <f> MainMenu Object
(options, self) -> <f> MainMenu Object
options <o> Options object
2011-05-16 10:49:48 +00:00
extras <a|[]> extra menus
menus <a|[]> submenus
size <s|medium> can be small, medium, large
2011-05-16 08:24:46 +00:00
self <o> shared private variable
@*/
2011-04-22 22:03:10 +00:00
Ox.MainMenu = function(options, self) {
self = self || {};
var that = Ox.Bar({}, self)
2011-04-22 22:03:10 +00:00
.defaults({
extras: [],
menus: [],
size: 'medium'
})
.options(options || {})
.addClass('OxMainMenu Ox' + Ox.toTitleCase(self.options.size)) // fixme: bar should accept small/medium/large ... like toolbar
.click(click)
.mousemove(mousemove);
self.focused = false;
self.selected = -1;
that.menus = [];
that.titles = [];
that.layer = $('<div>').addClass('OxLayer');
self.options.menus.forEach(function(menu, position) {
2011-09-17 20:47:16 +00:00
addMenu(menu, position);
2011-04-22 22:03:10 +00:00
});
if (self.options.extras.length) {
that.extras = $('<div>')
.addClass('OxExtras')
.appendTo(that.$element);
self.options.extras.forEach(function(extra) {
extra.css({
float: 'left' // fixme: need class!
}).appendTo(that.extras);
});
}
2011-09-17 20:47:16 +00:00
function addMenu(menu, position) {
that.titles[position] = $('<div>')
.addClass('OxTitle')
.html(menu.title)
.data('position', position);
if (position == 0) {
if (that.titles.length == 1) {
that.titles[position].appendTo(that.$element);
} else {
that.titles[position].insertBefore(that.titles[1]);
}
} else {
that.titles[position].insertAfter(that.titles[position - 1])
}
that.menus[position] = Ox.Menu(Ox.extend(menu, {
element: that.titles[position],
mainmenu: that,
size: self.options.size
}))
.bindEvent({
hide: onHideMenu
});
}
2011-04-22 22:03:10 +00:00
function click(event) {
var $target = $(event.target),
position = typeof $target.data('position') != 'undefined'
? $target.data('position') : -1;
2011-04-22 22:03:10 +00:00
clickTitle(position);
}
function clickTitle(position) {
var selected = self.selected;
if (self.selected > -1) {
that.menus[self.selected].hideMenu();
}
if (position > -1) {
if (position != selected) {
self.focused = true;
self.selected = position;
that.titles[self.selected].addClass('OxSelected');
that.menus[self.selected].showMenu();
}
}
}
function mousemove(event) {
var $target = $(event.target),
focused,
position = typeof $target.data('position') != 'undefined'
? $target.data('position') : -1;
2011-04-22 22:03:10 +00:00
if (self.focused && position != self.selected) {
if (position > -1) {
clickTitle(position);
} else {
focused = self.focused;
that.menus[self.selected].hideMenu();
self.focused = focused;
}
}
}
function onHideMenu() {
if (self.selected > -1) {
that.titles[self.selected].removeClass('OxSelected');
self.selected = -1;
}
self.focused = false;
}
2011-09-17 20:47:16 +00:00
function removeMenu(position) {
that.titles[position].remove();
that.menus[position].remove();
2011-09-17 20:47:16 +00:00
}
2011-04-29 12:40:51 +00:00
self.setOption = function(key, value) {
2011-04-22 22:03:10 +00:00
};
that.addMenuAfter = function(id) {
};
that.addMenuBefore = function(id) {
};
2011-05-16 10:49:48 +00:00
/*@
checkItem <f> checkItem
@*/
2011-04-22 22:03:10 +00:00
that.checkItem = function(id) {
var ids = id.split('_'),
item = that.getItem(id);
if (item) {
if (item.options('group')) {
item.options('menu').checkItem(ids[ids.length - 1]);
} else {
item.options({checked: true});
}
}
return that;
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
disableItem <f> disableItem
@*/
2011-04-22 22:03:10 +00:00
that.disableItem = function(id) {
var item = that.getItem(id);
item && item.options({disabled: true});
return that;
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
enableItem <f> enableItem
@*/
2011-04-22 22:03:10 +00:00
that.enableItem = function(id) {
var item = that.getItem(id);
item && item.options({disabled: false});
return that;
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
getItem <f> getItem
@*/
2011-04-22 22:03:10 +00:00
that.getItem = function(id) {
var ids = id.split('_'),
item;
if (ids.length == 1) {
Ox.forEach(that.menus, function(menu) {
item = menu.getItem(id);
return !item;
});
} else {
item = that.getMenu(ids.shift()).getItem(ids.join('_'));
}
2011-11-04 15:54:28 +00:00
Ox.Log('Menu', 'getItem', id, item);
2011-04-22 22:03:10 +00:00
return item;
};
2011-05-16 10:49:48 +00:00
/*@
getMenu <f> getMenu
@*/
2011-04-22 22:03:10 +00:00
that.getMenu = function(id) {
var ids = id.split('_'),
menu;
if (ids.length == 1) {
Ox.forEach(that.menus, function(v) {
if (v.options('id') == id) {
menu = v;
return false;
}
});
} else {
menu = that.getMenu(ids.shift()).getSubmenu(ids.join('_'));
}
return menu;
};
that.removeMenu = function() {
};
2012-05-21 10:38:18 +00:00
/*@
replaceMenu <f> replace menu
(id, menu) -> <u> replace menu
@*/
2011-09-17 20:47:16 +00:00
that.replaceMenu = function(id, menu) {
var position = Ox.getIndexById(self.options.menus, id);
2011-09-17 20:47:16 +00:00
self.options.menus[position] = menu;
removeMenu(position);
addMenu(menu, position);
};
2011-05-16 10:49:48 +00:00
/*@
selectNextMenu <f> selectNextMenu
@*/
2011-04-22 22:03:10 +00:00
that.selectNextMenu = function() {
if (self.selected < self.options.menus.length - 1) {
clickTitle(self.selected + 1);
}
return that;
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
selectPreviousMenu <f> selectPreviousMenu
@*/
2011-04-22 22:03:10 +00:00
that.selectPreviousMenu = function() {
if (self.selected) {
clickTitle(self.selected - 1);
}
return that;
2011-04-22 22:03:10 +00:00
};
2012-05-21 10:38:18 +00:00
/*@
setItemTitle <f> setItemTitle
(id, title) -> <o> set item title
@*/
that.setItemTitle = function(id, title) {
var item = that.getItem(id);
item && item.options({title: title});
return that;
};
2011-05-16 10:49:48 +00:00
/*@
uncheckItem <f> uncheckItem
@*/
2011-04-22 22:03:10 +00:00
that.uncheckItem = function(id) {
var item = that.getItem(id);
item && item.options({checked: false});
return that;
2011-04-22 22:03:10 +00:00
};
return that;
};