X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fweb%2Froot%2Fsrc%2Fmain%2Fresources%2Fjs%2Fdatasource.js;fp=opendaylight%2Fweb%2Froot%2Fsrc%2Fmain%2Fresources%2Fjs%2Fdatasource.js;h=8226df0cfecc58b08fb669252eec8768f59d271a;hb=a98556048975c29541849689396e58c813b7558e;hp=0000000000000000000000000000000000000000;hpb=abe7cd0f3412d447cf054e618ce1c7c7fa096745;p=controller.git diff --git a/opendaylight/web/root/src/main/resources/js/datasource.js b/opendaylight/web/root/src/main/resources/js/datasource.js new file mode 100755 index 0000000000..8226df0cfe --- /dev/null +++ b/opendaylight/web/root/src/main/resources/js/datasource.js @@ -0,0 +1,95 @@ +/* + * Fuel UX Data components - static data source + * https://github.com/ExactTarget/fuelux-data + * + * Copyright (c) 2012 ExactTarget + * Licensed under the MIT license. + */ + +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + define(['underscore'], factory); + } else { + root.StaticDataSource = factory(); + } +}(this, function () { + + var StaticDataSource = function (options) { + this._formatter = options.formatter; + this._columns = options.columns; + this._delay = options.delay || 0; + this._data = options.data; + }; + + StaticDataSource.prototype = { + + columns: function () { + return this._columns; + }, + + data: function (options, callback) { + var self = this; + + setTimeout(function () { + var data = $.extend(true, [], self._data); + + // SEARCHING + if (options.search) { + data = _.filter(data, function (item) { + var match = false; + + _.each(item, function (prop) { + if (_.isString(prop) || _.isFinite(prop)) { + if (prop.toString().toLowerCase().indexOf(options.search.toLowerCase()) !== -1) match = true; + } + }); + + return match; + }); + } + + // FILTERING + if (options.filter) { + data = _.filter(data, function (item) { + switch(options.filter.value) { + case 'lt5m': + if(item.population < 5000000) return true; + break; + case 'gte5m': + if(item.population >= 5000000) return true; + break; + default: + return true; + break; + } + }); + } + + var count = data.length; + + // SORTING + if (options.sortProperty) { + data = _.sortBy(data, options.sortProperty); + if (options.sortDirection === 'desc') data.reverse(); + } + + // PAGING + var startIndex = options.pageIndex * options.pageSize; + var endIndex = startIndex + options.pageSize; + var end = (endIndex > count) ? count : endIndex; + var pages = Math.ceil(count / options.pageSize); + var page = options.pageIndex + 1; + var start = startIndex + 1; + + data = data.slice(startIndex, endIndex); + + if (self._formatter) self._formatter(data); + + callback({ data: data, start: start, end: end, count: count, pages: pages, page: page }); + + }, this._delay) + } + }; + + return StaticDataSource; +}));