2012-12-10 00:01:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.ColumnList <f> Column List Widget
|
|
|
|
experimental
|
|
|
|
@*/
|
|
|
|
|
|
|
|
Ox.ColumnList = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
|
|
|
.defaults({
|
|
|
|
columns: [],
|
2012-12-10 16:46:48 +00:00
|
|
|
custom: {},
|
2012-12-10 00:01:29 +00:00
|
|
|
items: [],
|
|
|
|
list: 'table',
|
2012-12-18 14:05:47 +00:00
|
|
|
query: {conditions: [], operator: '&'},
|
2012-12-18 10:50:44 +00:00
|
|
|
resize: null,
|
2012-12-10 16:46:48 +00:00
|
|
|
width: 768
|
2012-12-10 00:01:29 +00:00
|
|
|
})
|
|
|
|
.options(options || {})
|
|
|
|
.update({
|
2012-12-18 14:05:47 +00:00
|
|
|
query: updateQuery,
|
2012-12-10 16:46:48 +00:00
|
|
|
width: function() {
|
|
|
|
self.columnWidths = getColumnWidths();
|
2012-12-18 10:50:44 +00:00
|
|
|
self.$panel.size(0, self.columnWidths[0]);
|
|
|
|
self.$panel.size(2, self.columnWidths[2]);
|
|
|
|
self.$lists.forEach(function($list, i) {
|
|
|
|
$list.options({itemWidth: self.columnWidths[i]});
|
|
|
|
});
|
2012-12-10 16:46:48 +00:00
|
|
|
}
|
2012-12-10 00:01:29 +00:00
|
|
|
})
|
|
|
|
.addClass('OxColumnList');
|
|
|
|
|
2012-12-10 16:46:48 +00:00
|
|
|
self.columnWidths = getColumnWidths();
|
|
|
|
self.numberOfColumns = self.options.columns.length;
|
|
|
|
|
2012-12-18 14:05:47 +00:00
|
|
|
self.flatItems = [];
|
|
|
|
self.ids = [{}, {}, {}];
|
|
|
|
self.options.items.forEach(function(item0) {
|
|
|
|
item0.items.forEach(function(item1) {
|
|
|
|
self.ids[1][item1.id] = [item0.id];
|
|
|
|
item1.items.forEach(function(item2) {
|
|
|
|
self.ids[2][item2.id] = [item0.id, item1.id];
|
|
|
|
self.flatItems.push(item2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
self.api = Ox.api(self.flatItems);
|
|
|
|
|
2012-12-10 16:46:48 +00:00
|
|
|
self.$lists = self.options.columns.map(function(column, i) {
|
|
|
|
return Ox.CustomList({
|
|
|
|
item: self.options.columns[i].item,
|
|
|
|
itemHeight: self.options.columns[i].itemHeight,
|
2012-12-22 00:20:37 +00:00
|
|
|
items: getItems(i),
|
2012-12-10 16:46:48 +00:00
|
|
|
itemWidth: self.columnWidths[i],
|
|
|
|
keys: self.options.columns[i].keys,
|
2012-12-18 21:48:57 +00:00
|
|
|
// FIXME: undefined max will overwrite CustomList default
|
2012-12-18 10:50:44 +00:00
|
|
|
max: self.options.columns[i].max,
|
|
|
|
resize: self.options.resize,
|
2012-12-10 16:46:48 +00:00
|
|
|
scrollbarVisible: true,
|
2012-12-18 10:50:44 +00:00
|
|
|
selected: self.options.columns[i].selected,
|
|
|
|
sort: self.options.columns[i].sort,
|
2012-12-10 16:46:48 +00:00
|
|
|
unique: 'id'
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
key_left: function() {
|
2012-12-18 10:50:44 +00:00
|
|
|
if (i > 0) {
|
|
|
|
self.$lists[i - 1].gainFocus();
|
|
|
|
that.triggerEvent('select', {
|
|
|
|
id: self.options.columns[i - 1].id,
|
|
|
|
ids: self.$lists[i - 1].options('selected')
|
|
|
|
});
|
|
|
|
}
|
2012-12-10 16:46:48 +00:00
|
|
|
},
|
|
|
|
key_right: function() {
|
2012-12-18 10:50:44 +00:00
|
|
|
var index, object, selected = self.$lists[i].options('selected');
|
2012-12-10 16:46:48 +00:00
|
|
|
if (selected.length) {
|
|
|
|
if (i < self.numberOfColumns - 1) {
|
2012-12-18 10:50:44 +00:00
|
|
|
if (self.$lists[i + 1].options('selected').length == 0) {
|
|
|
|
self.$lists[i + 1].selectPosition(0);
|
2012-12-10 16:46:48 +00:00
|
|
|
}
|
2012-12-18 10:50:44 +00:00
|
|
|
self.$lists[i + 1].gainFocus();
|
|
|
|
that.triggerEvent('select', {
|
|
|
|
id: self.options.columns[i + 1].id,
|
|
|
|
ids: self.$lists[i + 1].options('selected')
|
|
|
|
});
|
2012-12-10 16:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
select: function(data) {
|
2012-12-18 10:50:44 +00:00
|
|
|
self.options.columns[i].selected = data.ids;
|
|
|
|
if (i < self.numberOfColumns - 1) {
|
2012-12-22 00:20:37 +00:00
|
|
|
self.$lists[i + 1].options({items: getItems(i + 1)});
|
2012-12-18 10:50:44 +00:00
|
|
|
}
|
|
|
|
if (i == 0 || data.ids.length) {
|
|
|
|
that.triggerEvent('select', {
|
|
|
|
id: column.id,
|
|
|
|
ids: data.ids
|
2012-12-10 16:46:48 +00:00
|
|
|
});
|
2012-12-18 10:50:44 +00:00
|
|
|
} else {
|
|
|
|
self.$lists[i - 1].gainFocus();
|
|
|
|
that.triggerEvent('select', {
|
|
|
|
id: self.options.columns[i - 1].id,
|
|
|
|
ids: self.$lists[i - 1].options('selected')
|
2012-12-10 16:46:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
self.$panel = Ox.SplitPanel({
|
|
|
|
elements: self.$lists.map(function($list, index) {
|
|
|
|
return Ox.extend(
|
|
|
|
{element: $list},
|
|
|
|
index == 1 ? {} : {size: self.columnWidths[index]}
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
orientation: 'horizontal'
|
|
|
|
});
|
|
|
|
|
|
|
|
that.setElement(self.$panel);
|
|
|
|
|
|
|
|
function getColumnWidths() {
|
|
|
|
return Ox.splitInt(self.options.width, 3);
|
|
|
|
}
|
|
|
|
|
2012-12-22 00:20:37 +00:00
|
|
|
function getItems(i) {
|
|
|
|
var items;
|
|
|
|
if (i == 0) {
|
|
|
|
items = self.options.items;
|
|
|
|
} else {
|
|
|
|
items = [];
|
|
|
|
self.options.columns[i - 1].selected.forEach(function(id) {
|
|
|
|
var index;
|
|
|
|
if (i == 1) {
|
|
|
|
index = Ox.getIndexById(self.options.items, id);
|
|
|
|
items = items.concat(
|
|
|
|
self.options.items[index].items
|
|
|
|
);
|
|
|
|
} else if (i == 2) {
|
|
|
|
index = [];
|
|
|
|
Ox.forEach(self.options.columns[0].selected, function(id_) {
|
|
|
|
index[0] = Ox.getIndexById(
|
|
|
|
self.options.items, id_
|
|
|
|
);
|
|
|
|
index[1] = Ox.getIndexById(
|
|
|
|
self.options.items[index[0]].items, id
|
|
|
|
);
|
|
|
|
if (index[1] > -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
items = items.concat(
|
|
|
|
self.options.items[index[0]].items[index[1]].items
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2012-12-18 14:05:47 +00:00
|
|
|
function updateQuery() {
|
|
|
|
if (self.options.query.conditions.length == 0) {
|
|
|
|
self.items = self.options.items;
|
|
|
|
} else {
|
|
|
|
self.api({
|
|
|
|
keys: ['id', '_ids'],
|
|
|
|
query: self.options.query
|
|
|
|
}, function(result) {
|
|
|
|
var ids = [[], [], []];
|
|
|
|
result.data.items.forEach(function(item) {
|
|
|
|
ids[0].push(self.ids[2][item.id][0]);
|
|
|
|
ids[1].push(self.ids[2][item.id][1]);
|
|
|
|
ids[2].push(item.id);
|
|
|
|
});
|
|
|
|
self.items = self.options.items.filter(function(item0) {
|
|
|
|
return Ox.contains(ids[0], item0.id);
|
|
|
|
});
|
|
|
|
self.items.forEach(function(item0) {
|
|
|
|
item0.items = item0.items.filter(function(item1) {
|
|
|
|
return Ox.contains(ids[1], item1.id);
|
|
|
|
});
|
|
|
|
item0.items.forEach(function(item1) {
|
|
|
|
item1.items = item1.items.filter(function(item2) {
|
|
|
|
return Ox.contains(ids[2], item2.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
self.$lists.forEach(function($list, i) {
|
|
|
|
$list.options({items: i == 0 ? self.items : []});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-12-18 21:48:57 +00:00
|
|
|
that.sort = function(id, sort) {
|
|
|
|
var index = Ox.isNumber(id) ? id
|
|
|
|
: Ox.getIndexById(self.options.columns, id);
|
|
|
|
self.$lists[index].options({sort: sort});
|
|
|
|
self.options.columns[index].sort = sort;
|
|
|
|
};
|
|
|
|
|
2012-12-10 00:01:29 +00:00
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|