2017-02-18 22:13:29 +00:00
|
|
|
define(['snabbdom'], function (V) {
|
2017-01-29 23:51:08 +00:00
|
|
|
'use strict';
|
2017-02-18 22:13:29 +00:00
|
|
|
V = V.default;
|
2016-05-27 21:59:01 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
return function (headings, sortIndex, renderRow) {
|
2017-02-18 22:13:29 +00:00
|
|
|
var self = this;
|
2016-05-22 11:23:43 +00:00
|
|
|
var data;
|
|
|
|
var sortReverse = false;
|
2017-02-18 22:13:29 +00:00
|
|
|
self.el = document.createElement('table');
|
2015-04-07 15:41:17 +00:00
|
|
|
|
|
|
|
function sortTable(i) {
|
2016-05-22 11:23:43 +00:00
|
|
|
sortReverse = i === sortIndex ? !sortReverse : false;
|
|
|
|
sortIndex = i;
|
2015-04-07 15:41:17 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
updateView();
|
2015-04-07 15:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sortTableHandler(i) {
|
2016-05-22 12:51:30 +00:00
|
|
|
return function () {
|
|
|
|
sortTable(i);
|
|
|
|
};
|
2015-04-07 15:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateView() {
|
2016-05-22 11:23:43 +00:00
|
|
|
var children = [];
|
2015-04-07 15:41:17 +00:00
|
|
|
|
|
|
|
if (data.length !== 0) {
|
|
|
|
var th = headings.map(function (d, i) {
|
2017-02-04 19:01:49 +00:00
|
|
|
var name = _.t(d.name);
|
2016-05-22 12:51:30 +00:00
|
|
|
var properties = {
|
|
|
|
onclick: sortTableHandler(i),
|
2017-01-29 23:51:08 +00:00
|
|
|
className: 'sort-header'
|
2016-05-22 12:51:30 +00:00
|
|
|
};
|
2015-04-07 15:41:17 +00:00
|
|
|
|
2017-02-04 19:01:49 +00:00
|
|
|
if (d.class) {
|
|
|
|
properties.className += ' ' + d.class;
|
|
|
|
properties.title = name;
|
|
|
|
name = '';
|
|
|
|
}
|
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (sortIndex === i) {
|
2017-01-29 23:51:08 +00:00
|
|
|
properties.className += sortReverse ? ' sort-up' : ' sort-down';
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-07 15:41:17 +00:00
|
|
|
|
2017-02-18 22:13:29 +00:00
|
|
|
return V.h('th', { props: properties }, name);
|
2016-05-22 11:23:43 +00:00
|
|
|
});
|
2015-04-07 15:41:17 +00:00
|
|
|
|
2016-05-22 11:23:43 +00:00
|
|
|
var links = data.slice(0).sort(headings[sortIndex].sort);
|
2015-04-07 15:41:17 +00:00
|
|
|
|
2016-05-22 12:51:30 +00:00
|
|
|
if (headings[sortIndex].reverse ? !sortReverse : sortReverse) {
|
2016-05-22 11:23:43 +00:00
|
|
|
links = links.reverse();
|
2016-05-22 12:51:30 +00:00
|
|
|
}
|
2015-04-07 15:41:17 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
children.push(V.h('thead', V.h('tr', th)));
|
|
|
|
children.push(V.h('tbody', links.map(renderRow)));
|
2015-04-07 15:41:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var elNew = V.h('table', children);
|
2017-02-18 22:13:29 +00:00
|
|
|
self.el = V.patch(self.el, elNew);
|
2015-04-07 15:41:17 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 22:13:29 +00:00
|
|
|
self.setData = function setData(d) {
|
2016-05-22 11:23:43 +00:00
|
|
|
data = d;
|
|
|
|
updateView();
|
|
|
|
};
|
2015-04-07 15:41:17 +00:00
|
|
|
|
2017-02-18 22:13:29 +00:00
|
|
|
return self;
|
2016-05-22 11:23:43 +00:00
|
|
|
};
|
|
|
|
});
|