oxjs/source/Ox.UI/js/List/Ox.IconList.js

260 lines
7.7 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 10:49:48 +00:00
/*@
Ox.IconList <f:Ox.Element> IconList Object
2012-05-22 13:14:40 +00:00
([options[, self]]) -> <o> IconList Object
2011-05-16 10:49:48 +00:00
options <o> Options object
2011-08-06 17:41:01 +00:00
borderRadius <n|0> border radius for icon images
2011-05-16 10:49:48 +00:00
centerSelection <b|false> scroll list so selection is always centered
defaultRatio <n|1> aspect ratio of icon placeholders
draggable <b|false> If true, items can be dragged
fixedRatio <b|n|false> if set to a number, icons have a fixed ratio
2011-05-16 10:49:48 +00:00
id <s|''> element id
item <f|null> called with data, sort, size,
2011-06-20 14:37:37 +00:00
extends data with information needed for constructor
itemConstructor <f|Ox.IconItem> contructor used to create item
2011-05-16 10:49:48 +00:00
items <f|null> items array or callback function
keys <a|[]> available item keys
max <n|-1> maximum selected selected items
min <n|0> minimum of selcted items
2011-09-29 17:25:50 +00:00
orientation <s|both> orientation ("horizontal", "vertical" or "both")
pageLength <n|100> Number of items per page (if orientation != "both")
2011-05-16 10:49:48 +00:00
selected <a|[]> array of selected items
size <n|128> list size
sort <a|[]> sort keys
self <o> shared private variable
@*/
2011-04-22 22:03:10 +00:00
Ox.IconList = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
2011-04-22 22:03:10 +00:00
.defaults({
2011-08-06 17:41:01 +00:00
borderRadius: 0,
2011-04-22 22:03:10 +00:00
centerSelection: false,
defaultRatio: 1,
draggable: false,
2012-02-01 11:57:21 +00:00
find: '',
fixedRatio: false,
2011-04-22 22:03:10 +00:00
id: '',
item: null,
2011-06-20 14:37:37 +00:00
itemConstructor: Ox.IconItem,
2011-04-22 22:03:10 +00:00
items: null,
keys: [],
max: -1,
min: 0,
orientation: 'both',
pageLength: 100,
2011-04-22 22:03:10 +00:00
selected: [],
size: 128,
sort: [],
2011-08-19 06:41:48 +00:00
unique: ''
2011-04-22 22:03:10 +00:00
})
.options(options || {});
if (self.options.fixedRatio) {
self.options.defaultRatio = self.options.fixedRatio;
}
2011-08-07 02:33:26 +00:00
self.iconWidth = self.options.size;
self.iconHeight = self.options.fixedRatio > 1
? Math.round(self.options.size / self.options.fixedRatio)
: self.options.size;
2011-08-07 02:33:26 +00:00
self.itemWidth = self.options.size;
self.itemHeight = self.iconHeight + self.options.size * 0.5;
2011-04-22 22:03:10 +00:00
that.setElement(
Ox.List({
2011-04-22 22:03:10 +00:00
centered: self.options.centered,
2011-08-19 06:41:48 +00:00
// fixme: change all occurences of construct to render
2011-04-22 22:03:10 +00:00
construct: constructItem,
draggable: self.options.draggable,
id: self.options.id,
itemHeight: self.itemHeight,
items: self.options.items,
itemWidth: self.itemWidth,
keys: self.options.keys,
max: self.options.max,
min: self.options.min,
orientation: self.options.orientation,
pageLength: self.options.pageLength,
2011-04-22 22:03:10 +00:00
selected: self.options.selected,
sort: self.options.sort,
type: 'icon',
unique: self.options.unique
2011-10-16 12:32:02 +00:00
}, Ox.clone(self)) // pass event handler
2011-08-28 06:23:15 +00:00
.addClass('OxIconList Ox' + Ox.toTitleCase(self.options.orientation))
.bindEvent({
select: function() {
self.options.selected = that.$element.options('selected');
}
})
);
2011-04-22 22:03:10 +00:00
updateKeys();
function constructItem(data) {
var isEmpty = Ox.isEmpty(data);
data = !isEmpty
? self.options.item(data, self.options.sort, self.options.size)
: {
width: Math.round(self.options.size * (
self.options.defaultRatio >= 1 ? 1 : self.options.defaultRatio
)),
height: Math.round(self.options.size / (
self.options.defaultRatio <= 1 ? 1 : self.options.defaultRatio
))
};
2011-08-19 06:41:48 +00:00
return self.options.itemConstructor(Ox.extend(data, {
2011-08-06 17:41:01 +00:00
borderRadius: self.options.borderRadius,
2012-02-01 11:57:21 +00:00
find: self.options.find,
2011-08-07 02:33:26 +00:00
iconHeight: self.iconHeight,
iconWidth: self.iconWidth,
imageHeight: data.height,
imageWidth: data.width,
itemHeight: self.itemHeight,
2012-05-26 15:48:19 +00:00
itemWidth: self.itemWidth
2011-08-07 02:33:26 +00:00
//height: Math.round(self.options.size / (ratio <= 1 ? 1 : ratio)),
//size: self.options.size,
//width: Math.round(self.options.size * (ratio >= 1 ? 1 : ratio))
2011-04-22 22:03:10 +00:00
}));
}
function updateKeys() {
that.$element.options({
2012-05-24 07:45:33 +00:00
keys: Ox.unique(
[self.options.sort[0].key].concat(self.options.keys)
2012-05-24 07:46:42 +00:00
)
2011-04-22 22:03:10 +00:00
});
}
2011-05-16 10:49:48 +00:00
/*@
setOption <f> set key/value
2011-08-19 06:41:48 +00:00
(key, value) -> <u> set key in options to value
2011-05-16 10:49:48 +00:00
@*/
2011-04-29 12:40:51 +00:00
self.setOption = function(key, value) {
2011-04-22 22:03:10 +00:00
if (key == 'items') {
that.$element.options(key, value);
} else if (key == 'selected') {
that.$element.options(key, value);
} else if (key == 'sort') {
2011-09-28 17:31:35 +00:00
updateKeys();
that.$element.options(key, value);
2011-04-22 22:03:10 +00:00
}
};
2011-04-22 22:03:10 +00:00
2011-05-16 10:49:48 +00:00
/*@
closePreview <f> close preview
2011-08-07 22:16:06 +00:00
() -> <o> the list
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.closePreview = function() {
that.$element.closePreview();
2011-08-07 22:16:06 +00:00
return that;
2011-04-22 22:03:10 +00:00
};
2012-05-21 10:38:18 +00:00
/*@
gainFocus <f> gainFocus
@*/
that.gainFocus = function() {
that.$element.gainFocus();
return that;
};
2012-05-21 10:38:18 +00:00
/*@
hasFocus <f> hasFocus
@*/
that.hasFocus = function() {
return that.$element.hasFocus();
};
2012-05-21 10:38:18 +00:00
/*@
loseFocus <f> loseFocus
@*/
that.loseFocus = function() {
that.$element.loseFocus();
return that;
};
2011-05-16 10:49:48 +00:00
/*@
2011-08-07 22:16:06 +00:00
paste <f> paste into list
() -> <o> the list
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.paste = function(data) {
that.$element.paste(data);
return that;
};
2011-05-16 10:49:48 +00:00
/*@
reloadList <f> reload list
2011-08-07 22:16:06 +00:00
() -> <o> the list
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.reloadList = function() {
that.$element.reloadList();
return that;
};
2011-05-16 10:49:48 +00:00
/*@
2011-08-07 22:16:06 +00:00
scrollToSelection <f> scroll list to selection
() -> <o> the list
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.scrollToSelection = function() {
that.$element.scrollToSelection();
2011-08-19 06:41:48 +00:00
return that;
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
2011-08-07 22:16:06 +00:00
size <f> get size of list
() -> <n> size
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.size = function() {
that.$element.size();
};
2011-09-28 17:31:35 +00:00
// fixme: deprecate, use options()
2011-05-16 10:49:48 +00:00
/*@
sortList <f> sort list
2011-08-07 22:16:06 +00:00
(key, operator) -> <o> the list
key <s> sort key
operator <s> sort operator ("+" or "-")
2011-05-16 10:49:48 +00:00
@*/
2011-04-22 22:03:10 +00:00
that.sortList = function(key, operator) {
self.options.sort = [{
key: key,
operator: operator
}];
updateKeys();
that.$element.sortList(key, operator);
2011-08-07 22:16:06 +00:00
return that;
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
value <f> get/set value of item in list
(id) -> <o> get all data for item
(id, key) -> <s> get value of key of item with id
(id, key, value) -> <f> set value, returns IconList
@*/
2011-04-22 22:03:10 +00:00
that.value = function(id, key, value) {
// fixme: make this accept id, {k: v, ...}
if (arguments.length == 1) {
return that.$element.value(id);
} else if (arguments.length == 2) {
return that.$element.value(id, key);
} else {
that.$element.value(id, key, value);
2011-10-24 21:50:54 +00:00
if (key == self.unique) {
// unique id has changed
self.options.selected = self.options.selected.map(function(id_) {
return id_ == id ? value : id_
});
}
if (key == self.options.sort[0].key) {
// sort key has changed
that.$element.sort();
}
2011-04-22 22:03:10 +00:00
return that;
}
};
2011-04-22 22:03:10 +00:00
return that;
};