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

152 lines
4.4 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 08:24:46 +00:00
/*@
Ox.MenuItem <f:Ox.Element> MenuItem Object
() -> <f> MenuItem Object
(options) -> <f> MenuItem Object
(options, self) -> <f> MenuItem Object
options <o> Options object
2011-05-16 10:49:48 +00:00
bind <a|[]> fixme: what's this?
checked <f|null>
disabled <b|false>
group <s|''>
icon <s|''> icon
id <s|''> id
items <a|[]> items
keyboard <s|''> keyboard
menu <o|null> menu
position <n|0> position
title <a|[]> title
2011-05-16 08:24:46 +00:00
self <o> shared private variable
@*/
2011-04-22 22:03:10 +00:00
Ox.MenuItem = function(options, self) {
self = self || {};
var that = Ox.Element('<tr>', self)
2011-04-22 22:03:10 +00:00
.defaults({
bind: [], // fixme: what's this?
checked: null,
disabled: false,
group: '',
icon: '',
id: '',
items: [],
keyboard: '',
2011-11-02 18:21:49 +00:00
maxWidth: 0,
2011-04-22 22:03:10 +00:00
menu: null, // fixme: is passing the menu to 100s of menu items really memory-neutral?
position: 0,
title: [],
})
.options(Ox.extend(Ox.clone(options), {
2011-04-22 22:03:10 +00:00
keyboard: parseKeyboard(options.keyboard || self.defaults.keyboard),
title: Ox.toArray(options.title || self.defaults.title)
}))
.addClass('OxItem' + (self.options.disabled ? ' OxDisabled' : ''))
2011-04-22 22:03:10 +00:00
/*
.attr({
id: Ox.toCamelCase(self.options.menu.options('id') + '/' + self.options.id)
})
*/
.data('group', self.options.group); // fixme: why?
if (self.options.group && self.options.checked === null) {
self.options.checked = false;
}
// construct
that.append(
that.$status = $('<td>', {
'class': 'OxCell OxStatus',
html: self.options.checked ? Ox.UI.symbols.check : ''
})
)
.append(
that.$icon = $('<td>', {
'class': 'OxCell OxIcon'
})
.append(self.options.icon
? $('<img>', {src: self.options.icon}) : null
2011-04-22 22:03:10 +00:00
)
)
.append(
that.$title = $('<td>', {
'class': 'OxCell OxTitle',
css: self.options.maxWidth
? {maxWidth: self.options.maxWidth - 46}
: {},
html: Ox.isString(self.options.title[0])
? self.options.title[0]
: $('<div>').html(self.options.title[0]).html()
2011-04-22 22:03:10 +00:00
})
)
.append(
$('<td>', {
'class': 'OxCell OxModifiers',
html: self.options.keyboard.modifiers.map(function(modifier) {
2011-04-22 22:03:10 +00:00
return Ox.UI.symbols[modifier];
}).join('')
})
)
.append(
$('<td>', {
'class': 'OxCell Ox'
+ (self.options.items.length ? 'Submenu' : 'Key'),
html: self.options.items.length
? Ox.UI.symbols.triangle_right
: Ox.UI.symbols[self.options.keyboard.key]
|| self.options.keyboard.key.toUpperCase()
2011-04-22 22:03:10 +00:00
})
);
function parseKeyboard(str) {
var modifiers = str.split(' '),
key = modifiers.pop();
return {
modifiers: modifiers,
key: key
};
}
2011-05-16 10:49:48 +00:00
/*@
setOption <f> setOption
@*/
2011-04-29 12:40:51 +00:00
self.setOption = function(key, value) {
2011-04-22 22:03:10 +00:00
if (key == 'checked') {
that.$status.html(value ? Ox.UI.symbols.check : '')
} else if (key == 'disabled') {
that.toggleClass('OxDisabled');
2011-04-22 22:03:10 +00:00
} else if (key == 'title') {
self.options.title = Ox.toArray(value);
that.$title.html(self.options.title[0]);
}
};
2011-04-22 22:03:10 +00:00
that.toggle = function() {
// toggle id and title
};
2011-05-16 10:49:48 +00:00
/*@
toggleChecked <f> toggleChecked
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.toggleChecked = function() {
that.options({checked: !self.options.checked});
return that;
2011-04-22 22:03:10 +00:00
};
that.toggleDisabled = function() {
};
2011-05-16 10:49:48 +00:00
/*@
toggleTitle <f> toggleTitle
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.toggleTitle = function() {
2011-10-04 02:26:24 +00:00
that.options({title: Ox.clone(self.options.title).reverse()});
return that;
2011-04-22 22:03:10 +00:00
};
return that;
};