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

309 lines
9.2 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 10:49:48 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.IconList <f> 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
centered <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")
query <o> Query
2012-06-29 12:19:34 +00:00
selectAsYouType <s|''> If set to a key, enables select-as-you-type
2011-05-16 10:49:48 +00:00
selected <a|[]> array of selected items
size <n|128> list size
sort <a|[]> sort keys
sums <[s]|[]> Sums to be included in totals
self <o> Shared private variable
([options[, self]]) -> <o:Ox.List> IconList Object
2011-05-16 10:49:48 +00:00
@*/
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,
centered: 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,
query: {conditions: [], operator: '&'},
2012-06-29 12:19:34 +00:00
selectAsYouType: '',
2011-04-22 22:03:10 +00:00
selected: [],
size: 128,
sort: [],
sums: [],
2011-08-19 06:41:48 +00:00
unique: ''
2011-04-22 22:03:10 +00:00
})
2012-05-28 19:35:41 +00:00
.options(options || {})
.update({
items: function() {
2014-09-22 11:20:52 +00:00
self.$list.options({items: self.options.items});
},
query: function() {
2014-09-22 11:20:52 +00:00
self.$list.options({query: self.options.query});
2012-05-28 19:35:41 +00:00
},
selected: function() {
2014-09-22 11:20:52 +00:00
self.$list.options({selected: self.options.selected});
2012-05-28 19:35:41 +00:00
},
sort: function() {
updateKeys();
2014-09-22 11:20:52 +00:00
self.$list.options({sort: self.options.sort});
2012-05-28 19:35:41 +00:00
}
});
2011-04-22 22:03:10 +00:00
if (self.options.fixedRatio) {
self.options.defaultRatio = self.options.fixedRatio;
}
if (Ox.isArray(self.options.items)) {
self.options.keys = Ox.unique(Ox.flatten(
self.options.items.map(function(item) {
return Object.keys(item);
})
));
}
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
2014-09-22 11:20:52 +00:00
self.$list = 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,
query: self.options.query,
2012-06-29 12:19:34 +00:00
selectAsYouType: self.options.selectAsYouType,
2011-04-22 22:03:10 +00:00
selected: self.options.selected,
sort: self.options.sort,
sums: self.options.sums,
2011-04-22 22:03:10 +00:00
type: 'icon',
unique: self.options.unique
})
2011-08-28 06:23:15 +00:00
.addClass('OxIconList Ox' + Ox.toTitleCase(self.options.orientation))
.bindEvent(function(data, event) {
if (event == 'select') {
self.options.selected = data.ids;
}
that.triggerEvent(event, data);
2014-09-22 11:20:52 +00:00
});
that.setElement(self.$list);
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() {
2014-09-22 11:20:52 +00:00
self.$list.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
/*@
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() {
2014-09-22 11:20:52 +00:00
self.$list.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() {
2014-09-22 11:20:52 +00:00
self.$list.gainFocus();
return that;
};
2014-02-12 13:56:54 +00:00
that.getPasteIndex = function() {
2014-09-22 11:20:52 +00:00
return self.$list.getPasteIndex();
2014-02-12 13:56:54 +00:00
};
2012-05-21 10:38:18 +00:00
/*@
hasFocus <f> hasFocus
@*/
that.hasFocus = function() {
2014-09-22 11:20:52 +00:00
return self.$list.hasFocus();
};
that.invertSelection = function() {
2014-09-22 11:20:52 +00:00
self.$list.invertSelection();
return that;
};
2012-05-21 10:38:18 +00:00
/*@
loseFocus <f> loseFocus
@*/
that.loseFocus = function() {
2014-09-22 11:20:52 +00:00
self.$list.loseFocus();
return that;
};
/*@
openPreview <f> openPreview
@*/
that.openPreview = function() {
2014-09-22 11:20:52 +00:00
self.$list.openPreview();
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() {
2014-09-22 11:20:52 +00:00
self.$list.reloadList();
2011-04-22 22:03:10 +00:00
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() {
2014-09-22 11:20:52 +00:00
self.$list.scrollToSelection();
2011-08-19 06:41:48 +00:00
return that;
2011-04-22 22:03:10 +00:00
};
that.selectAll = function() {
2014-09-22 11:20:52 +00:00
self.$list.selectAll();
return that;
};
that.selectPosition = function(pos) {
2014-09-22 11:20:52 +00:00
self.$list.selectPosition(pos);
return that;
};
that.selectSelected = function(offset) {
2014-09-22 11:20:52 +00:00
self.$list.selectSelected(offset);
return that;
};
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() {
2014-09-22 11:20:52 +00:00
self.$list.size();
return that;
2011-04-22 22:03:10 +00:00
};
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();
2014-09-22 11:20:52 +00:00
self.$list.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
(id, {key: value, ...}) -> <f> set values, returns IconList
2011-05-16 10:49:48 +00:00
@*/
that.value = function() {
var args = Ox.slice(arguments),
id = args.shift(),
sort = false;
2011-04-22 22:03:10 +00:00
if (arguments.length == 1) {
2014-09-22 11:20:52 +00:00
return self.$list.value(id);
} else if (arguments.length == 2 && Ox.isString(arguments[1])) {
2014-09-22 11:20:52 +00:00
return self.$list.value(id, arguments[1]);
2011-04-22 22:03:10 +00:00
} else {
Ox.forEach(Ox.makeObject(args), function(value, key) {
2014-09-22 11:20:52 +00:00
self.$list.value(id, key, value);
if (key == self.unique) {
// unique id has changed
self.options.selected = self.options.selected.map(function(id_) {
return id_ == id ? value : id_
});
id = value;
}
if (key == self.options.sort[0].key) {
// sort key has changed
sort = true;
}
});
2014-09-22 11:20:52 +00:00
sort && self.$list.sort();
2011-04-22 22:03:10 +00:00
return that;
}
};
2011-04-22 22:03:10 +00:00
return that;
};