Devices: NodesLearnt table integration with FuelUx Datagrid.
[controller.git] / opendaylight / web / root / src / main / resources / js / datasource.js
1 /*
2  * Fuel UX Data components - static data source
3  * https://github.com/ExactTarget/fuelux-data
4  *
5  * Copyright (c) 2012 ExactTarget
6  * Licensed under the MIT license.
7  */
8
9 (function (root, factory) {
10         if (typeof define === 'function' && define.amd) {
11                 define(['underscore'], factory);
12         } else {
13                 root.StaticDataSource = factory();
14         }
15 }(this, function () {
16
17         var StaticDataSource = function (options) {
18                 this._formatter = options.formatter;
19                 this._columns = options.columns;
20                 this._delay = options.delay || 0;
21                 this._data = options.data;
22         };
23
24         StaticDataSource.prototype = {
25
26                 columns: function () {
27                         return this._columns;
28                 },
29
30                 data: function (options, callback) {
31                         var self = this;
32
33                         setTimeout(function () {
34                                 var data = $.extend(true, [], self._data);
35
36                                 // SEARCHING
37                                 if (options.search) {
38                                         data = _.filter(data, function (item) {
39                                                 var match = false;
40
41                                                 _.each(item, function (prop) {
42                                                         if (_.isString(prop) || _.isFinite(prop)) {
43                                                                 if (prop.toString().toLowerCase().indexOf(options.search.toLowerCase()) !== -1) match = true;
44                                                         }
45                                                 });
46
47                                                 return match;
48                                         });
49                                 }
50
51                                 // FILTERING
52                                 if (options.filter) {
53                                         data = _.filter(data, function (item) {
54                                                 switch(options.filter.value) {
55                                                         case 'lt5m':
56                                                                 if(item.population < 5000000) return true;
57                                                                 break;
58                                                         case 'gte5m':
59                                                                 if(item.population >= 5000000) return true;
60                                                                 break;
61                                                         default:
62                                                                 return true;
63                                                                 break;
64                                                 }
65                                         });
66                                 }
67
68                                 var count = data.length;
69
70                                 // SORTING
71                                 if (options.sortProperty) {
72                                         data = _.sortBy(data, options.sortProperty);
73                                         if (options.sortDirection === 'desc') data.reverse();
74                                 }
75
76                                 // PAGING
77                                 var startIndex = options.pageIndex * options.pageSize;
78                                 var endIndex = startIndex + options.pageSize;
79                                 var end = (endIndex > count) ? count : endIndex;
80                                 var pages = Math.ceil(count / options.pageSize);
81                                 var page = options.pageIndex + 1;
82                                 var start = startIndex + 1;
83
84                                 data = data.slice(startIndex, endIndex);
85
86                                 if (self._formatter) self._formatter(data);
87
88                                 callback({ data: data, start: start, end: end, count: count, pages: pages, page: page });
89
90                         }, this._delay)
91                 }
92         };
93
94         return StaticDataSource;
95 }));