Comparing version 0.3.0 to 0.3.1
{ | ||
"name": "ng-tasty", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"homepage": "https://github.com/Zizzamia/ng-tasty", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -5,3 +5,3 @@ /* | ||
* Version: 0.3.0 - 2014-10-22 | ||
* Version: 0.3.1 - 2014-11-10 | ||
* License: MIT | ||
@@ -104,3 +104,4 @@ */ | ||
'ngTasty.service.setProperty', | ||
'ngTasty.service.joinObjects' | ||
'ngTasty.service.joinObjects', | ||
'ngTasty.service.webSocket' | ||
]); | ||
@@ -169,5 +170,8 @@ | ||
.factory('joinObjects', ["setProperty", function(setProperty) { | ||
return function(objOne, objTwo) { | ||
return function(objOne, objTwo, listKeyNotJoin) { | ||
listKeyNotJoin = listKeyNotJoin || []; | ||
for (var attrname in objTwo) { | ||
setProperty(objOne, objTwo, attrname); | ||
if (listKeyNotJoin.indexOf(attrname) < 0) { | ||
setProperty(objOne, objTwo, attrname); | ||
} | ||
} | ||
@@ -178,2 +182,75 @@ return objOne; | ||
angular.module('ngTasty.service.webSocket', [ | ||
'ngTasty.service' | ||
]) | ||
.factory('WebSocket', function() { | ||
return function(url) { | ||
var blobURL = URL.createObjectURL(new Blob(['(', function() { | ||
var WSWorker = (function() { | ||
var _ws; | ||
var initialize = function(url) { | ||
_ws = new WebSocket(url); | ||
}; | ||
var on = function(event) { | ||
_ws.onmessage = function(response) { | ||
var data = JSON.parse(response.data); | ||
self.postMessage(data); | ||
}; | ||
}; | ||
var send = function(data) { | ||
_ws.send(data); | ||
}; | ||
return { | ||
initialize: initialize, | ||
on: on, | ||
send: send | ||
}; | ||
})(); | ||
self.addEventListener('message', function(e) { | ||
switch (e.data.cmd) { | ||
case 'ws_new': | ||
WSWorker.initialize(e.data.url); | ||
break; | ||
case 'ws_on': | ||
WSWorker.on(e.data.event, e.data.cb); | ||
break; | ||
case 'ws_send': | ||
WSWorker.send(JSON.stringify(e.data.data)); | ||
break; | ||
default: | ||
console.log('Unknown command: ' + e.data.cmd); | ||
} | ||
}); | ||
}.toString(), ')()'], { type: 'application/javascript' })); | ||
var _worker = new Worker(blobURL); | ||
URL.revokeObjectURL(blobURL); | ||
_worker.postMessage({ cmd: 'ws_new', url: url }); | ||
return { | ||
on: function(event, cb) { | ||
_worker.postMessage({ cmd: 'ws_on' }); | ||
_worker.addEventListener('message', function(e) { | ||
if (event === 'all' || e.data.type === event) { | ||
cb(e.data); | ||
} | ||
}); | ||
}, | ||
send: function(data) { | ||
_worker.postMessage({ cmd: 'ws_send', data: data }); | ||
} | ||
}; | ||
}; | ||
}); | ||
/** | ||
@@ -195,2 +272,8 @@ * @ngdoc directive | ||
.constant('tableConfig', { | ||
init: { | ||
'count': 5, | ||
'page': 1, | ||
'sortBy': undefined, | ||
'sortOrder': undefined | ||
}, | ||
query: { | ||
@@ -208,6 +291,6 @@ 'page': 'page', | ||
'use strict'; | ||
var listScopeToWatch; | ||
var listScopeToWatch, initTable; | ||
this.$scope = $scope; | ||
listScopeToWatch = ['filters', 'query', 'resource', 'resourceCallback']; | ||
listScopeToWatch = ['filters', 'init', 'query', 'resource', 'resourceCallback']; | ||
listScopeToWatch.forEach(function (scopeName) { | ||
@@ -245,2 +328,7 @@ var lastValue, parentGet, compare, parentSet, parentValueWatch; | ||
$scope.query = $scope.query || tableConfig.query; | ||
$scope.init = $scope.init || {}; | ||
$scope.init.count = $scope.init.count || tableConfig.init.count; | ||
$scope.init.page = $scope.init.page || tableConfig.init.page; | ||
$scope.init.sortBy = $scope.init.sortBy || tableConfig.init.sortBy; | ||
$scope.init.sortOrder = $scope.init.sortOrder || tableConfig.init.sortOrder; | ||
@@ -256,4 +344,4 @@ // Defualt variables | ||
$scope.pagination = { | ||
'count': 5, | ||
'page': 1, | ||
'count': $scope.init.count, | ||
'page': $scope.init.page, | ||
'pages': 1, | ||
@@ -263,3 +351,3 @@ 'size': 0 | ||
$scope.theadDirective = false; | ||
$scope.paginationDirective = false; | ||
$scope.paginationDirective = false; | ||
@@ -288,3 +376,3 @@ /* Set custom configs | ||
$scope.clientSide = false; | ||
} | ||
} | ||
@@ -300,2 +388,5 @@ // In TableController, by using `this` we build an API | ||
$scope.params[key] = value; | ||
if (['sortBy', 'sortOrder'].indexOf(key) >= 0) { | ||
$scope.header[key] = value; | ||
} | ||
}; | ||
@@ -306,3 +397,2 @@ | ||
$scope.setDirectivesValues = function (resource) { | ||
var sortBy; | ||
if (!angular.isObject(resource)) { | ||
@@ -326,11 +416,14 @@ throw 'AngularJS tastyTable directive: the resource '+ | ||
} | ||
sortBy = resource.sortBy || $scope.params.sortBy; | ||
sortBy = sortBy || resource.header[0].key; | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
'sortBy': $scope.params.sortBy, | ||
'sortOrder': $scope.params.sortOrder | ||
}; | ||
$scope.rows = resource.rows; | ||
$scope.pagination = resource.pagination || $scope.pagination; | ||
if ($scope.paginationDirective && resource.pagination) { | ||
$scope.pagination.count = resource.pagination.count; | ||
$scope.pagination.page = resource.pagination.page; | ||
$scope.pagination.pages = resource.pagination.pages; | ||
$scope.pagination.size = resource.pagination.size; | ||
} | ||
}; | ||
@@ -350,3 +443,5 @@ | ||
} | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
if ($scope.header.sortBy) { | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
} | ||
} | ||
@@ -371,4 +466,5 @@ if ($attrs.filters) { | ||
$scope.buildUrl = function(params, filters) { | ||
var urlQuery, value, url; | ||
var urlQuery, value, url, listKeyNotJoin; | ||
urlQuery = {}; | ||
listKeyNotJoin = ['sortBy', 'sortOrder', 'page', 'count']; | ||
if ($scope.theadDirective) { | ||
@@ -383,3 +479,3 @@ urlQuery = tastyUtil.setProperty(urlQuery, params, 'sortBy'); | ||
if ($attrs.filters) { | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters); | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters, listKeyNotJoin); | ||
} | ||
@@ -398,3 +494,3 @@ return Object.keys(urlQuery).map(function(key) { | ||
$scope.buildClientResource(); | ||
}, 100); | ||
}, 60); | ||
@@ -406,12 +502,17 @@ $scope.updateServerSideResource = tastyUtil.debounce(function() { | ||
}); | ||
}, 100); | ||
}, 60); | ||
$scope.initTable = function () { | ||
$scope.params['sortBy'] = undefined; | ||
$scope.params['sortOrder'] = 'asc'; | ||
$scope.params['page'] = 1; | ||
$scope.params['count'] = undefined; | ||
initTable = function () { | ||
if ($scope.clientSide) { | ||
$scope.params.sortBy = $scope.resource.sortBy || $scope.init.sortBy; | ||
$scope.params.sortOrder = $scope.resource.sortOrder || $scope.init.sortOrder; | ||
$scope.params.page = $scope.init.page; | ||
if ($scope.resource.pagination) { | ||
$scope.params.page = $scope.resource.pagination.page || $scope.init.page; | ||
} | ||
$scope.updateClientSideResource(); | ||
} else { | ||
$scope.params.sortBy = $scope.init.sortBy; | ||
$scope.params.sortOrder = $scope.init.sortOrder; | ||
$scope.params.page = $scope.init.page; | ||
$scope.updateServerSideResource(); | ||
@@ -445,2 +546,4 @@ } | ||
if (newValue !== oldValue) { | ||
$scope.params.sortBy = newValue.sortBy; | ||
$scope.params.sortOrder = newValue.sortOrder; | ||
$scope.updateClientSideResource(); | ||
@@ -452,3 +555,3 @@ } | ||
// Init table | ||
$scope.initTable(); | ||
initTable(); | ||
}]) | ||
@@ -484,10 +587,13 @@ .directive('tastyTable', function(){ | ||
'use strict'; | ||
var iconUp, iconDown; | ||
// Thead it's called | ||
tastyTable.activate('thead'); | ||
scope.bindOnce = tastyTable.bindOnce; | ||
scope.columns = []; | ||
iconUp = 'fa fa-sort-up'; | ||
iconDown = 'fa fa-sort-down'; | ||
scope.setColumns = function () { | ||
var lenHeader, width, i, active, sortable, sort; | ||
var lenHeader, width, i, active, sortable, sort, isSorted; | ||
scope.columns = []; | ||
@@ -499,2 +605,3 @@ lenHeader = scope.header.columns.length; | ||
active = false; | ||
isSorted = ''; | ||
if (scope.notSortBy) { | ||
@@ -508,2 +615,7 @@ sortable = scope.notSortBy.indexOf(column.key) < 0; | ||
sort = $filter('cleanFieldName')(column.key); | ||
if (scope.header.sortBy === '-' + sort) { | ||
isSorted = iconDown; | ||
} else if (scope.header.sortBy === sort) { | ||
isSorted = iconUp; | ||
} | ||
scope.columns.push({ | ||
@@ -515,7 +627,7 @@ 'key': column.key, | ||
'width': { 'width': width + '%' }, | ||
'isSortUp': scope.header.sortBy === '-' + sort, | ||
'isSortDown': scope.header.sortBy === sort | ||
'isSorted': isSorted | ||
}); | ||
}); | ||
if (scope.header.sortOrder === 'dsc' && | ||
if (scope.header.sortOrder === 'dsc' && | ||
scope.header.sortBy && | ||
scope.header.sortBy[0] !== '-') { | ||
@@ -530,12 +642,11 @@ scope.header.sortBy = '-' + scope.header.sortBy; | ||
} | ||
var columnName; | ||
var columnName, sortOrder; | ||
columnName = $filter('cleanFieldName')(column.key); | ||
if (scope.header.sortBy == columnName) { | ||
scope.header.sortBy = '-' + columnName; | ||
tastyTable.setParams('sortOrder', 'dsc'); | ||
if (scope.header.sortBy === columnName) { | ||
sortOrder = 'dsc'; | ||
} else { | ||
scope.header.sortBy = columnName; | ||
tastyTable.setParams('sortOrder', 'asc'); | ||
sortOrder = 'asc'; | ||
} | ||
tastyTable.setParams('sortBy', column.key); | ||
tastyTable.setParams('sortOrder', sortOrder); | ||
}; | ||
@@ -615,3 +726,2 @@ | ||
var maxItems, page; | ||
//scope.pagination.count = count; | ||
maxItems = count * scope.pagination.page; | ||
@@ -660,2 +770,3 @@ if (maxItems > scope.pagination.size) { | ||
setPaginationRanges = function () { | ||
scope.listItemsPerPageShow = []; | ||
scope.pagMinRange = scope.pagMinRange > 0 ? scope.pagMinRange : 1; | ||
@@ -670,8 +781,10 @@ scope.pagMaxRange = scope.pagMinRange + 5; | ||
scope.classPageMaxRange = scope.pagHideMaxRange ? 'disabled' : ''; | ||
for (var i = 2; i < scope.listItemsPerPage.length; i++) { | ||
if (scope.pagination.size < scope.listItemsPerPage[i]) { | ||
scope.listItemsPerPageShow = scope.listItemsPerPage.slice(0, i); | ||
for (var i = scope.listItemsPerPage.length; i >= 0; i--) { | ||
if (scope.pagination.size > scope.listItemsPerPage[i]) { | ||
scope.listItemsPerPageShow = scope.listItemsPerPage.slice(0, (i + 1)); | ||
break; | ||
} | ||
} | ||
scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange); | ||
@@ -721,4 +834,3 @@ }; | ||
' <span ng-bind="::column.name"></span>\n' + | ||
' <span ng-if="column.isSortUp" class="fa fa-sort-up"></span>\n' + | ||
' <span ng-if="column.isSortDown" class="fa fa-sort-down"></span>\n' + | ||
' <span ng-class="column.isSorted"></span>\n' + | ||
' </th> \n' + | ||
@@ -742,12 +854,12 @@ '</tr>'); | ||
' <li ng-class="classPageMinRange">\n' + | ||
' <span ng-click="page.previous()">«</span>\n' + | ||
' <a href="#" ng-click="page.previous()">«</a>\n' + | ||
' </li>\n' + | ||
' <li ng-repeat="numPage in rangePage" ng-class="classNumPage(numPage)">\n' + | ||
' <span ng-click="page.get(numPage)">\n' + | ||
' <a href="#" ng-click="page.get(numPage)">\n' + | ||
' <span ng-bind="numPage"></span>\n' + | ||
' <span class="sr-only" ng-if="classNumPage(numPage)">(current)</span>\n' + | ||
' </span>\n' + | ||
' </a>\n' + | ||
' </li>\n' + | ||
' <li ng-class="classPageMaxRange">\n' + | ||
' <span ng-click="page.remaining()">»</span>\n' + | ||
' <a href="#" ng-click="page.remaining()">»</a>\n' + | ||
' </li>\n' + | ||
@@ -754,0 +866,0 @@ ' </ul>\n' + |
@@ -5,5 +5,5 @@ /* | ||
* Version: 0.3.0 - 2014-10-22 | ||
* Version: 0.3.1 - 2014-11-10 | ||
* License: MIT | ||
*/ | ||
angular.module("ngTasty",["ngTasty.tpls","ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.tpls",["template/table/head.html","template/table/pagination.html"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-")}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo){for(var attrname in objTwo)setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},listItemsPerPage:[5,25,50,100],itemsPerPage:5,bindOnce:!0}).controller("TableController",["$scope","$attrs","$timeout","$filter","$parse","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,$parse,tableConfig,tastyUtil){"use strict";var listScopeToWatch;if(this.$scope=$scope,listScopeToWatch=["filters","query","resource","resourceCallback"],listScopeToWatch.forEach(function(scopeName){var lastValue,parentGet,compare,parentSet,parentValueWatch;$attrs[scopeName]&&(parentGet=$parse($attrs[scopeName]),compare=parentGet.literal?equals:function(a,b){return a===b||a!==a&&b!==b},parentSet=parentGet.assign,lastValue=$scope[scopeName]=parentGet($scope.$parent),parentValueWatch=function(parentValue){return compare(parentValue,$scope[scopeName])||(compare(parentValue,lastValue)?parentSet($scope.$parent,parentValue=$scope[scopeName]):$scope[scopeName]=parentValue),lastValue=parentValue},parentValueWatch.$stateful=!0,$scope.$parent.$watch($parse($attrs[scopeName],parentValueWatch),null,parentGet.literal))}),$scope.query=$scope.query||tableConfig.query,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:5,page:1,pages:1,size:0},$scope.theadDirective=!1,$scope.paginationDirective=!1,!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if(!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if(!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value},this.bindOnce=tableConfig.bindOnce,$scope.setDirectivesValues=function(resource){var sortBy;if(!angular.isObject(resource))throw"AngularJS tastyTable directive: the resource it's not an object";if(!resource.header&&!resource.rows)throw"AngularJS tastyTable directive: the resource has the property header or rows undefined";1===Object.keys(resource.header[0]).length&&(resource.header=resource.header.map(function(header){var key=Object.keys(header)[0];return{key:key,name:header[key]}})),sortBy=resource.sortBy||$scope.params.sortBy,sortBy=sortBy||resource.header[0].key,$scope.header={columns:resource.header,sortBy:sortBy,sortOrder:resource.sortOrder||$scope.params.sortOrder},$scope.rows=resource.rows,$scope.pagination=resource.pagination||$scope.pagination},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse,listSortBy;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,listSortBy=[function(item){return item[$scope.header.sortBy]}],$scope.header.columns[0].key!==$scope.header.sortBy&&listSortBy.push(function(item){return item[$scope.header.columns[0].key]}),$scope.rows=$filter("orderBy")($scope.rows,listSortBy,reverse)),$attrs.filters&&($scope.rows=$filter("filter")($scope.rows,$scope.filters)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value;return urlQuery={},$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},100),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope.filters),$scope.resourceCallback($scope.url,$scope.params).then(function(resource){$scope.setDirectivesValues(resource)})},100),$scope.initTable=function(){$scope.params.sortBy=void 0,$scope.params.sortOrder="asc",$scope.params.page=1,$scope.params.count=void 0,$scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource()},$attrs.filters&&$scope.$watch("filters",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.$watchCollection("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())}),$scope.resource&&$scope.$watch("resource",function(newValue,oldValue){newValue!==oldValue&&$scope.updateClientSideResource()},!0),$scope.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";tastyTable.activate("thead"),scope.bindOnce=tastyTable.bindOnce,scope.columns=[],scope.setColumns=function(){var lenHeader,width,active,sortable,sort;scope.columns=[],lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),sort=$filter("cleanFieldName")(column.key),scope.columns.push({key:column.key,name:column.name,active:active,sortable:sortable,width:{width:width+"%"},isSortUp:scope.header.sortBy==="-"+sort,isSortDown:scope.header.sortBy===sort})}),"dsc"===scope.header.sortOrder&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(column){if(scope.notSortBy&&scope.notSortBy.indexOf(column.key)>=0)return!1;var columnName;columnName=$filter("cleanFieldName")(column.key),scope.header.sortBy==columnName?(scope.header.sortBy="-"+columnName,tastyTable.setParams("sortOrder","dsc")):(scope.header.sortBy=columnName,tastyTable.setParams("sortOrder","asc")),tastyTable.setParams("sortBy",column.key)},scope.classToShow=function(column){var listClassToShow=[];return column.sortable&&listClassToShow.push("sortable"),column.active&&listClassToShow.push("active"),listClassToShow},tastyTable.$scope.$watchCollection("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setColumns())})}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent[$attrs.itemsPerPage]),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent[$attrs.listItemsPerPage]),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setPaginationRanges=function(){scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages,scope.classPageMinRange=scope.pagHideMinRange?"disabled":"",scope.classPageMaxRange=scope.pagHideMaxRange?"disabled":"";for(var i=2;i<scope.listItemsPerPage.length;i++)if(scope.pagination.size<scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.classPaginationCount=function(count){return count==scope.pagination.count?"active":""},scope.classNumPage=function(numPage){return numPage==scope.pagination.page?"active":!1},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watchCollection("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())}),scope.page.setCount(scope.itemsPerPage)}}}]),angular.module("template/table/head.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/head.html",'<tr>\n <th ng-repeat="column in columns track by $index" \n ng-class="classToShow(column)"\n ng-style="column.width" ng-click="sortBy(column)">\n <span ng-bind="::column.name"></span>\n <span ng-if="column.isSortUp" class="fa fa-sort-up"></span>\n <span ng-if="column.isSortDown" class="fa fa-sort-down"></span>\n </th> \n</tr>')}]),angular.module("template/table/pagination.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/pagination.html",'<div class="row">\n <div class="col-xs-3 text-left">\n <div class="btn-group">\n <button type="button" class="btn btn-default" \n ng-repeat="count in listItemsPerPageShow" \n ng-class="classPaginationCount(count)" \n ng-click="page.setCount(count)" ng-bind="count"></button>\n </div>\n </div>\n <div class="col-xs-6 text-center">\n <ul class="pagination">\n <li ng-class="classPageMinRange">\n <span ng-click="page.previous()">«</span>\n </li>\n <li ng-repeat="numPage in rangePage" ng-class="classNumPage(numPage)">\n <span ng-click="page.get(numPage)">\n <span ng-bind="numPage"></span>\n <span class="sr-only" ng-if="classNumPage(numPage)">(current)</span>\n </span>\n </li>\n <li ng-class="classPageMaxRange">\n <span ng-click="page.remaining()">»</span>\n </li>\n </ul>\n </div>\n <div class="col-xs-3 text-right">\n <p>Page <span ng-bind="pagination.page"></span> \n of <span ng-bind="pagination.pages"></span>,\n of <span ng-bind="pagination.size"></span> entries</p>\n </div>\n</div>')}]); | ||
angular.module("ngTasty",["ngTasty.tpls","ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.tpls",["template/table/head.html","template/table/pagination.html"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-")}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects","ngTasty.service.webSocket"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo,listKeyNotJoin){listKeyNotJoin=listKeyNotJoin||[];for(var attrname in objTwo)listKeyNotJoin.indexOf(attrname)<0&&setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.service.webSocket",["ngTasty.service"]).factory("WebSocket",function(){return function(url){var blobURL=URL.createObjectURL(new Blob(["(",function(){var WSWorker=function(){var _ws,initialize=function(url){_ws=new WebSocket(url)},on=function(){_ws.onmessage=function(response){var data=JSON.parse(response.data);self.postMessage(data)}},send=function(data){_ws.send(data)};return{initialize:initialize,on:on,send:send}}();self.addEventListener("message",function(e){switch(e.data.cmd){case"ws_new":WSWorker.initialize(e.data.url);break;case"ws_on":WSWorker.on(e.data.event,e.data.cb);break;case"ws_send":WSWorker.send(JSON.stringify(e.data.data));break;default:console.log("Unknown command: "+e.data.cmd)}})}.toString(),")()"],{type:"application/javascript"})),_worker=new Worker(blobURL);return URL.revokeObjectURL(blobURL),_worker.postMessage({cmd:"ws_new",url:url}),{on:function(event,cb){_worker.postMessage({cmd:"ws_on"}),_worker.addEventListener("message",function(e){("all"===event||e.data.type===event)&&cb(e.data)})},send:function(data){_worker.postMessage({cmd:"ws_send",data:data})}}}}),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{init:{count:5,page:1,sortBy:void 0,sortOrder:void 0},query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},listItemsPerPage:[5,25,50,100],itemsPerPage:5,bindOnce:!0}).controller("TableController",["$scope","$attrs","$timeout","$filter","$parse","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,$parse,tableConfig,tastyUtil){"use strict";var listScopeToWatch,initTable;if(this.$scope=$scope,listScopeToWatch=["filters","init","query","resource","resourceCallback"],listScopeToWatch.forEach(function(scopeName){var lastValue,parentGet,compare,parentSet,parentValueWatch;$attrs[scopeName]&&(parentGet=$parse($attrs[scopeName]),compare=parentGet.literal?equals:function(a,b){return a===b||a!==a&&b!==b},parentSet=parentGet.assign,lastValue=$scope[scopeName]=parentGet($scope.$parent),parentValueWatch=function(parentValue){return compare(parentValue,$scope[scopeName])||(compare(parentValue,lastValue)?parentSet($scope.$parent,parentValue=$scope[scopeName]):$scope[scopeName]=parentValue),lastValue=parentValue},parentValueWatch.$stateful=!0,$scope.$parent.$watch($parse($attrs[scopeName],parentValueWatch),null,parentGet.literal))}),$scope.query=$scope.query||tableConfig.query,$scope.init=$scope.init||{},$scope.init.count=$scope.init.count||tableConfig.init.count,$scope.init.page=$scope.init.page||tableConfig.init.page,$scope.init.sortBy=$scope.init.sortBy||tableConfig.init.sortBy,$scope.init.sortOrder=$scope.init.sortOrder||tableConfig.init.sortOrder,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:$scope.init.count,page:$scope.init.page,pages:1,size:0},$scope.theadDirective=!1,$scope.paginationDirective=!1,!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if(!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if(!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value,["sortBy","sortOrder"].indexOf(key)>=0&&($scope.header[key]=value)},this.bindOnce=tableConfig.bindOnce,$scope.setDirectivesValues=function(resource){if(!angular.isObject(resource))throw"AngularJS tastyTable directive: the resource it's not an object";if(!resource.header&&!resource.rows)throw"AngularJS tastyTable directive: the resource has the property header or rows undefined";1===Object.keys(resource.header[0]).length&&(resource.header=resource.header.map(function(header){var key=Object.keys(header)[0];return{key:key,name:header[key]}})),$scope.header={columns:resource.header,sortBy:$scope.params.sortBy,sortOrder:$scope.params.sortOrder},$scope.rows=resource.rows,$scope.paginationDirective&&resource.pagination&&($scope.pagination.count=resource.pagination.count,$scope.pagination.page=resource.pagination.page,$scope.pagination.pages=resource.pagination.pages,$scope.pagination.size=resource.pagination.size)},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse,listSortBy;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,listSortBy=[function(item){return item[$scope.header.sortBy]}],$scope.header.columns[0].key!==$scope.header.sortBy&&listSortBy.push(function(item){return item[$scope.header.columns[0].key]}),$scope.header.sortBy&&($scope.rows=$filter("orderBy")($scope.rows,listSortBy,reverse))),$attrs.filters&&($scope.rows=$filter("filter")($scope.rows,$scope.filters)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value,listKeyNotJoin;return urlQuery={},listKeyNotJoin=["sortBy","sortOrder","page","count"],$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters,listKeyNotJoin)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},60),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope.filters),$scope.resourceCallback($scope.url,$scope.params).then(function(resource){$scope.setDirectivesValues(resource)})},60),initTable=function(){$scope.clientSide?($scope.params.sortBy=$scope.resource.sortBy||$scope.init.sortBy,$scope.params.sortOrder=$scope.resource.sortOrder||$scope.init.sortOrder,$scope.params.page=$scope.init.page,$scope.resource.pagination&&($scope.params.page=$scope.resource.pagination.page||$scope.init.page),$scope.updateClientSideResource()):($scope.params.sortBy=$scope.init.sortBy,$scope.params.sortOrder=$scope.init.sortOrder,$scope.params.page=$scope.init.page,$scope.updateServerSideResource())},$attrs.filters&&$scope.$watch("filters",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.$watchCollection("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())}),$scope.resource&&$scope.$watch("resource",function(newValue,oldValue){newValue!==oldValue&&($scope.params.sortBy=newValue.sortBy,$scope.params.sortOrder=newValue.sortOrder,$scope.updateClientSideResource())},!0),initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";var iconUp,iconDown;tastyTable.activate("thead"),scope.bindOnce=tastyTable.bindOnce,scope.columns=[],iconUp="fa fa-sort-up",iconDown="fa fa-sort-down",scope.setColumns=function(){var lenHeader,width,active,sortable,sort,isSorted;scope.columns=[],lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,isSorted="",scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),sort=$filter("cleanFieldName")(column.key),scope.header.sortBy==="-"+sort?isSorted=iconDown:scope.header.sortBy===sort&&(isSorted=iconUp),scope.columns.push({key:column.key,name:column.name,active:active,sortable:sortable,width:{width:width+"%"},isSorted:isSorted})}),"dsc"===scope.header.sortOrder&&scope.header.sortBy&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(column){if(scope.notSortBy&&scope.notSortBy.indexOf(column.key)>=0)return!1;var columnName,sortOrder;columnName=$filter("cleanFieldName")(column.key),sortOrder=scope.header.sortBy===columnName?"dsc":"asc",tastyTable.setParams("sortBy",column.key),tastyTable.setParams("sortOrder",sortOrder)},scope.classToShow=function(column){var listClassToShow=[];return column.sortable&&listClassToShow.push("sortable"),column.active&&listClassToShow.push("active"),listClassToShow},tastyTable.$scope.$watchCollection("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setColumns())})}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent[$attrs.itemsPerPage]),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent[$attrs.listItemsPerPage]),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setPaginationRanges=function(){scope.listItemsPerPageShow=[],scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages,scope.classPageMinRange=scope.pagHideMinRange?"disabled":"",scope.classPageMaxRange=scope.pagHideMaxRange?"disabled":"";for(var i=scope.listItemsPerPage.length;i>=0;i--)if(scope.pagination.size>scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i+1);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.classPaginationCount=function(count){return count==scope.pagination.count?"active":""},scope.classNumPage=function(numPage){return numPage==scope.pagination.page?"active":!1},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watchCollection("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())}),scope.page.setCount(scope.itemsPerPage)}}}]),angular.module("template/table/head.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/head.html",'<tr>\n <th ng-repeat="column in columns track by $index" \n ng-class="classToShow(column)"\n ng-style="column.width" ng-click="sortBy(column)">\n <span ng-bind="::column.name"></span>\n <span ng-class="column.isSorted"></span>\n </th> \n</tr>')}]),angular.module("template/table/pagination.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/pagination.html",'<div class="row">\n <div class="col-xs-3 text-left">\n <div class="btn-group">\n <button type="button" class="btn btn-default" \n ng-repeat="count in listItemsPerPageShow" \n ng-class="classPaginationCount(count)" \n ng-click="page.setCount(count)" ng-bind="count"></button>\n </div>\n </div>\n <div class="col-xs-6 text-center">\n <ul class="pagination">\n <li ng-class="classPageMinRange">\n <a href="#" ng-click="page.previous()">«</a>\n </li>\n <li ng-repeat="numPage in rangePage" ng-class="classNumPage(numPage)">\n <a href="#" ng-click="page.get(numPage)">\n <span ng-bind="numPage"></span>\n <span class="sr-only" ng-if="classNumPage(numPage)">(current)</span>\n </a>\n </li>\n <li ng-class="classPageMaxRange">\n <a href="#" ng-click="page.remaining()">»</a>\n </li>\n </ul>\n </div>\n <div class="col-xs-3 text-right">\n <p>Page <span ng-bind="pagination.page"></span> \n of <span ng-bind="pagination.pages"></span>,\n of <span ng-bind="pagination.size"></span> entries</p>\n </div>\n</div>')}]); |
197
ng-tasty.js
@@ -5,3 +5,3 @@ /* | ||
* Version: 0.3.0 - 2014-10-22 | ||
* Version: 0.3.1 - 2014-11-10 | ||
* License: MIT | ||
@@ -103,3 +103,4 @@ */ | ||
'ngTasty.service.setProperty', | ||
'ngTasty.service.joinObjects' | ||
'ngTasty.service.joinObjects', | ||
'ngTasty.service.webSocket' | ||
]); | ||
@@ -168,5 +169,8 @@ | ||
.factory('joinObjects', ["setProperty", function(setProperty) { | ||
return function(objOne, objTwo) { | ||
return function(objOne, objTwo, listKeyNotJoin) { | ||
listKeyNotJoin = listKeyNotJoin || []; | ||
for (var attrname in objTwo) { | ||
setProperty(objOne, objTwo, attrname); | ||
if (listKeyNotJoin.indexOf(attrname) < 0) { | ||
setProperty(objOne, objTwo, attrname); | ||
} | ||
} | ||
@@ -177,2 +181,75 @@ return objOne; | ||
angular.module('ngTasty.service.webSocket', [ | ||
'ngTasty.service' | ||
]) | ||
.factory('WebSocket', function() { | ||
return function(url) { | ||
var blobURL = URL.createObjectURL(new Blob(['(', function() { | ||
var WSWorker = (function() { | ||
var _ws; | ||
var initialize = function(url) { | ||
_ws = new WebSocket(url); | ||
}; | ||
var on = function(event) { | ||
_ws.onmessage = function(response) { | ||
var data = JSON.parse(response.data); | ||
self.postMessage(data); | ||
}; | ||
}; | ||
var send = function(data) { | ||
_ws.send(data); | ||
}; | ||
return { | ||
initialize: initialize, | ||
on: on, | ||
send: send | ||
}; | ||
})(); | ||
self.addEventListener('message', function(e) { | ||
switch (e.data.cmd) { | ||
case 'ws_new': | ||
WSWorker.initialize(e.data.url); | ||
break; | ||
case 'ws_on': | ||
WSWorker.on(e.data.event, e.data.cb); | ||
break; | ||
case 'ws_send': | ||
WSWorker.send(JSON.stringify(e.data.data)); | ||
break; | ||
default: | ||
console.log('Unknown command: ' + e.data.cmd); | ||
} | ||
}); | ||
}.toString(), ')()'], { type: 'application/javascript' })); | ||
var _worker = new Worker(blobURL); | ||
URL.revokeObjectURL(blobURL); | ||
_worker.postMessage({ cmd: 'ws_new', url: url }); | ||
return { | ||
on: function(event, cb) { | ||
_worker.postMessage({ cmd: 'ws_on' }); | ||
_worker.addEventListener('message', function(e) { | ||
if (event === 'all' || e.data.type === event) { | ||
cb(e.data); | ||
} | ||
}); | ||
}, | ||
send: function(data) { | ||
_worker.postMessage({ cmd: 'ws_send', data: data }); | ||
} | ||
}; | ||
}; | ||
}); | ||
/** | ||
@@ -194,2 +271,8 @@ * @ngdoc directive | ||
.constant('tableConfig', { | ||
init: { | ||
'count': 5, | ||
'page': 1, | ||
'sortBy': undefined, | ||
'sortOrder': undefined | ||
}, | ||
query: { | ||
@@ -207,6 +290,6 @@ 'page': 'page', | ||
'use strict'; | ||
var listScopeToWatch; | ||
var listScopeToWatch, initTable; | ||
this.$scope = $scope; | ||
listScopeToWatch = ['filters', 'query', 'resource', 'resourceCallback']; | ||
listScopeToWatch = ['filters', 'init', 'query', 'resource', 'resourceCallback']; | ||
listScopeToWatch.forEach(function (scopeName) { | ||
@@ -244,2 +327,7 @@ var lastValue, parentGet, compare, parentSet, parentValueWatch; | ||
$scope.query = $scope.query || tableConfig.query; | ||
$scope.init = $scope.init || {}; | ||
$scope.init.count = $scope.init.count || tableConfig.init.count; | ||
$scope.init.page = $scope.init.page || tableConfig.init.page; | ||
$scope.init.sortBy = $scope.init.sortBy || tableConfig.init.sortBy; | ||
$scope.init.sortOrder = $scope.init.sortOrder || tableConfig.init.sortOrder; | ||
@@ -255,4 +343,4 @@ // Defualt variables | ||
$scope.pagination = { | ||
'count': 5, | ||
'page': 1, | ||
'count': $scope.init.count, | ||
'page': $scope.init.page, | ||
'pages': 1, | ||
@@ -262,3 +350,3 @@ 'size': 0 | ||
$scope.theadDirective = false; | ||
$scope.paginationDirective = false; | ||
$scope.paginationDirective = false; | ||
@@ -287,3 +375,3 @@ /* Set custom configs | ||
$scope.clientSide = false; | ||
} | ||
} | ||
@@ -299,2 +387,5 @@ // In TableController, by using `this` we build an API | ||
$scope.params[key] = value; | ||
if (['sortBy', 'sortOrder'].indexOf(key) >= 0) { | ||
$scope.header[key] = value; | ||
} | ||
}; | ||
@@ -305,3 +396,2 @@ | ||
$scope.setDirectivesValues = function (resource) { | ||
var sortBy; | ||
if (!angular.isObject(resource)) { | ||
@@ -325,11 +415,14 @@ throw 'AngularJS tastyTable directive: the resource '+ | ||
} | ||
sortBy = resource.sortBy || $scope.params.sortBy; | ||
sortBy = sortBy || resource.header[0].key; | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
'sortBy': $scope.params.sortBy, | ||
'sortOrder': $scope.params.sortOrder | ||
}; | ||
$scope.rows = resource.rows; | ||
$scope.pagination = resource.pagination || $scope.pagination; | ||
if ($scope.paginationDirective && resource.pagination) { | ||
$scope.pagination.count = resource.pagination.count; | ||
$scope.pagination.page = resource.pagination.page; | ||
$scope.pagination.pages = resource.pagination.pages; | ||
$scope.pagination.size = resource.pagination.size; | ||
} | ||
}; | ||
@@ -349,3 +442,5 @@ | ||
} | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
if ($scope.header.sortBy) { | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
} | ||
} | ||
@@ -370,4 +465,5 @@ if ($attrs.filters) { | ||
$scope.buildUrl = function(params, filters) { | ||
var urlQuery, value, url; | ||
var urlQuery, value, url, listKeyNotJoin; | ||
urlQuery = {}; | ||
listKeyNotJoin = ['sortBy', 'sortOrder', 'page', 'count']; | ||
if ($scope.theadDirective) { | ||
@@ -382,3 +478,3 @@ urlQuery = tastyUtil.setProperty(urlQuery, params, 'sortBy'); | ||
if ($attrs.filters) { | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters); | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters, listKeyNotJoin); | ||
} | ||
@@ -397,3 +493,3 @@ return Object.keys(urlQuery).map(function(key) { | ||
$scope.buildClientResource(); | ||
}, 100); | ||
}, 60); | ||
@@ -405,12 +501,17 @@ $scope.updateServerSideResource = tastyUtil.debounce(function() { | ||
}); | ||
}, 100); | ||
}, 60); | ||
$scope.initTable = function () { | ||
$scope.params['sortBy'] = undefined; | ||
$scope.params['sortOrder'] = 'asc'; | ||
$scope.params['page'] = 1; | ||
$scope.params['count'] = undefined; | ||
initTable = function () { | ||
if ($scope.clientSide) { | ||
$scope.params.sortBy = $scope.resource.sortBy || $scope.init.sortBy; | ||
$scope.params.sortOrder = $scope.resource.sortOrder || $scope.init.sortOrder; | ||
$scope.params.page = $scope.init.page; | ||
if ($scope.resource.pagination) { | ||
$scope.params.page = $scope.resource.pagination.page || $scope.init.page; | ||
} | ||
$scope.updateClientSideResource(); | ||
} else { | ||
$scope.params.sortBy = $scope.init.sortBy; | ||
$scope.params.sortOrder = $scope.init.sortOrder; | ||
$scope.params.page = $scope.init.page; | ||
$scope.updateServerSideResource(); | ||
@@ -444,2 +545,4 @@ } | ||
if (newValue !== oldValue) { | ||
$scope.params.sortBy = newValue.sortBy; | ||
$scope.params.sortOrder = newValue.sortOrder; | ||
$scope.updateClientSideResource(); | ||
@@ -451,3 +554,3 @@ } | ||
// Init table | ||
$scope.initTable(); | ||
initTable(); | ||
}]) | ||
@@ -483,10 +586,13 @@ .directive('tastyTable', function(){ | ||
'use strict'; | ||
var iconUp, iconDown; | ||
// Thead it's called | ||
tastyTable.activate('thead'); | ||
scope.bindOnce = tastyTable.bindOnce; | ||
scope.columns = []; | ||
iconUp = 'fa fa-sort-up'; | ||
iconDown = 'fa fa-sort-down'; | ||
scope.setColumns = function () { | ||
var lenHeader, width, i, active, sortable, sort; | ||
var lenHeader, width, i, active, sortable, sort, isSorted; | ||
scope.columns = []; | ||
@@ -498,2 +604,3 @@ lenHeader = scope.header.columns.length; | ||
active = false; | ||
isSorted = ''; | ||
if (scope.notSortBy) { | ||
@@ -507,2 +614,7 @@ sortable = scope.notSortBy.indexOf(column.key) < 0; | ||
sort = $filter('cleanFieldName')(column.key); | ||
if (scope.header.sortBy === '-' + sort) { | ||
isSorted = iconDown; | ||
} else if (scope.header.sortBy === sort) { | ||
isSorted = iconUp; | ||
} | ||
scope.columns.push({ | ||
@@ -514,7 +626,7 @@ 'key': column.key, | ||
'width': { 'width': width + '%' }, | ||
'isSortUp': scope.header.sortBy === '-' + sort, | ||
'isSortDown': scope.header.sortBy === sort | ||
'isSorted': isSorted | ||
}); | ||
}); | ||
if (scope.header.sortOrder === 'dsc' && | ||
if (scope.header.sortOrder === 'dsc' && | ||
scope.header.sortBy && | ||
scope.header.sortBy[0] !== '-') { | ||
@@ -529,12 +641,11 @@ scope.header.sortBy = '-' + scope.header.sortBy; | ||
} | ||
var columnName; | ||
var columnName, sortOrder; | ||
columnName = $filter('cleanFieldName')(column.key); | ||
if (scope.header.sortBy == columnName) { | ||
scope.header.sortBy = '-' + columnName; | ||
tastyTable.setParams('sortOrder', 'dsc'); | ||
if (scope.header.sortBy === columnName) { | ||
sortOrder = 'dsc'; | ||
} else { | ||
scope.header.sortBy = columnName; | ||
tastyTable.setParams('sortOrder', 'asc'); | ||
sortOrder = 'asc'; | ||
} | ||
tastyTable.setParams('sortBy', column.key); | ||
tastyTable.setParams('sortOrder', sortOrder); | ||
}; | ||
@@ -614,3 +725,2 @@ | ||
var maxItems, page; | ||
//scope.pagination.count = count; | ||
maxItems = count * scope.pagination.page; | ||
@@ -659,2 +769,3 @@ if (maxItems > scope.pagination.size) { | ||
setPaginationRanges = function () { | ||
scope.listItemsPerPageShow = []; | ||
scope.pagMinRange = scope.pagMinRange > 0 ? scope.pagMinRange : 1; | ||
@@ -669,8 +780,10 @@ scope.pagMaxRange = scope.pagMinRange + 5; | ||
scope.classPageMaxRange = scope.pagHideMaxRange ? 'disabled' : ''; | ||
for (var i = 2; i < scope.listItemsPerPage.length; i++) { | ||
if (scope.pagination.size < scope.listItemsPerPage[i]) { | ||
scope.listItemsPerPageShow = scope.listItemsPerPage.slice(0, i); | ||
for (var i = scope.listItemsPerPage.length; i >= 0; i--) { | ||
if (scope.pagination.size > scope.listItemsPerPage[i]) { | ||
scope.listItemsPerPageShow = scope.listItemsPerPage.slice(0, (i + 1)); | ||
break; | ||
} | ||
} | ||
scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange); | ||
@@ -677,0 +790,0 @@ }; |
@@ -5,5 +5,5 @@ /* | ||
* Version: 0.3.0 - 2014-10-22 | ||
* Version: 0.3.1 - 2014-11-10 | ||
* License: MIT | ||
*/ | ||
angular.module("ngTasty",["ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-")}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo){for(var attrname in objTwo)setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},listItemsPerPage:[5,25,50,100],itemsPerPage:5,bindOnce:!0}).controller("TableController",["$scope","$attrs","$timeout","$filter","$parse","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,$parse,tableConfig,tastyUtil){"use strict";var listScopeToWatch;if(this.$scope=$scope,listScopeToWatch=["filters","query","resource","resourceCallback"],listScopeToWatch.forEach(function(scopeName){var lastValue,parentGet,compare,parentSet,parentValueWatch;$attrs[scopeName]&&(parentGet=$parse($attrs[scopeName]),compare=parentGet.literal?equals:function(a,b){return a===b||a!==a&&b!==b},parentSet=parentGet.assign,lastValue=$scope[scopeName]=parentGet($scope.$parent),parentValueWatch=function(parentValue){return compare(parentValue,$scope[scopeName])||(compare(parentValue,lastValue)?parentSet($scope.$parent,parentValue=$scope[scopeName]):$scope[scopeName]=parentValue),lastValue=parentValue},parentValueWatch.$stateful=!0,$scope.$parent.$watch($parse($attrs[scopeName],parentValueWatch),null,parentGet.literal))}),$scope.query=$scope.query||tableConfig.query,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:5,page:1,pages:1,size:0},$scope.theadDirective=!1,$scope.paginationDirective=!1,!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if(!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if(!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value},this.bindOnce=tableConfig.bindOnce,$scope.setDirectivesValues=function(resource){var sortBy;if(!angular.isObject(resource))throw"AngularJS tastyTable directive: the resource it's not an object";if(!resource.header&&!resource.rows)throw"AngularJS tastyTable directive: the resource has the property header or rows undefined";1===Object.keys(resource.header[0]).length&&(resource.header=resource.header.map(function(header){var key=Object.keys(header)[0];return{key:key,name:header[key]}})),sortBy=resource.sortBy||$scope.params.sortBy,sortBy=sortBy||resource.header[0].key,$scope.header={columns:resource.header,sortBy:sortBy,sortOrder:resource.sortOrder||$scope.params.sortOrder},$scope.rows=resource.rows,$scope.pagination=resource.pagination||$scope.pagination},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse,listSortBy;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,listSortBy=[function(item){return item[$scope.header.sortBy]}],$scope.header.columns[0].key!==$scope.header.sortBy&&listSortBy.push(function(item){return item[$scope.header.columns[0].key]}),$scope.rows=$filter("orderBy")($scope.rows,listSortBy,reverse)),$attrs.filters&&($scope.rows=$filter("filter")($scope.rows,$scope.filters)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value;return urlQuery={},$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},100),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope.filters),$scope.resourceCallback($scope.url,$scope.params).then(function(resource){$scope.setDirectivesValues(resource)})},100),$scope.initTable=function(){$scope.params.sortBy=void 0,$scope.params.sortOrder="asc",$scope.params.page=1,$scope.params.count=void 0,$scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource()},$attrs.filters&&$scope.$watch("filters",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.$watchCollection("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())}),$scope.resource&&$scope.$watch("resource",function(newValue,oldValue){newValue!==oldValue&&$scope.updateClientSideResource()},!0),$scope.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";tastyTable.activate("thead"),scope.bindOnce=tastyTable.bindOnce,scope.columns=[],scope.setColumns=function(){var lenHeader,width,active,sortable,sort;scope.columns=[],lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),sort=$filter("cleanFieldName")(column.key),scope.columns.push({key:column.key,name:column.name,active:active,sortable:sortable,width:{width:width+"%"},isSortUp:scope.header.sortBy==="-"+sort,isSortDown:scope.header.sortBy===sort})}),"dsc"===scope.header.sortOrder&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(column){if(scope.notSortBy&&scope.notSortBy.indexOf(column.key)>=0)return!1;var columnName;columnName=$filter("cleanFieldName")(column.key),scope.header.sortBy==columnName?(scope.header.sortBy="-"+columnName,tastyTable.setParams("sortOrder","dsc")):(scope.header.sortBy=columnName,tastyTable.setParams("sortOrder","asc")),tastyTable.setParams("sortBy",column.key)},scope.classToShow=function(column){var listClassToShow=[];return column.sortable&&listClassToShow.push("sortable"),column.active&&listClassToShow.push("active"),listClassToShow},tastyTable.$scope.$watchCollection("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setColumns())})}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent[$attrs.itemsPerPage]),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent[$attrs.listItemsPerPage]),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setPaginationRanges=function(){scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages,scope.classPageMinRange=scope.pagHideMinRange?"disabled":"",scope.classPageMaxRange=scope.pagHideMaxRange?"disabled":"";for(var i=2;i<scope.listItemsPerPage.length;i++)if(scope.pagination.size<scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.classPaginationCount=function(count){return count==scope.pagination.count?"active":""},scope.classNumPage=function(numPage){return numPage==scope.pagination.page?"active":!1},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watchCollection("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())}),scope.page.setCount(scope.itemsPerPage)}}}]); | ||
angular.module("ngTasty",["ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-")}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects","ngTasty.service.webSocket"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo,listKeyNotJoin){listKeyNotJoin=listKeyNotJoin||[];for(var attrname in objTwo)listKeyNotJoin.indexOf(attrname)<0&&setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.service.webSocket",["ngTasty.service"]).factory("WebSocket",function(){return function(url){var blobURL=URL.createObjectURL(new Blob(["(",function(){var WSWorker=function(){var _ws,initialize=function(url){_ws=new WebSocket(url)},on=function(){_ws.onmessage=function(response){var data=JSON.parse(response.data);self.postMessage(data)}},send=function(data){_ws.send(data)};return{initialize:initialize,on:on,send:send}}();self.addEventListener("message",function(e){switch(e.data.cmd){case"ws_new":WSWorker.initialize(e.data.url);break;case"ws_on":WSWorker.on(e.data.event,e.data.cb);break;case"ws_send":WSWorker.send(JSON.stringify(e.data.data));break;default:console.log("Unknown command: "+e.data.cmd)}})}.toString(),")()"],{type:"application/javascript"})),_worker=new Worker(blobURL);return URL.revokeObjectURL(blobURL),_worker.postMessage({cmd:"ws_new",url:url}),{on:function(event,cb){_worker.postMessage({cmd:"ws_on"}),_worker.addEventListener("message",function(e){("all"===event||e.data.type===event)&&cb(e.data)})},send:function(data){_worker.postMessage({cmd:"ws_send",data:data})}}}}),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{init:{count:5,page:1,sortBy:void 0,sortOrder:void 0},query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},listItemsPerPage:[5,25,50,100],itemsPerPage:5,bindOnce:!0}).controller("TableController",["$scope","$attrs","$timeout","$filter","$parse","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,$parse,tableConfig,tastyUtil){"use strict";var listScopeToWatch,initTable;if(this.$scope=$scope,listScopeToWatch=["filters","init","query","resource","resourceCallback"],listScopeToWatch.forEach(function(scopeName){var lastValue,parentGet,compare,parentSet,parentValueWatch;$attrs[scopeName]&&(parentGet=$parse($attrs[scopeName]),compare=parentGet.literal?equals:function(a,b){return a===b||a!==a&&b!==b},parentSet=parentGet.assign,lastValue=$scope[scopeName]=parentGet($scope.$parent),parentValueWatch=function(parentValue){return compare(parentValue,$scope[scopeName])||(compare(parentValue,lastValue)?parentSet($scope.$parent,parentValue=$scope[scopeName]):$scope[scopeName]=parentValue),lastValue=parentValue},parentValueWatch.$stateful=!0,$scope.$parent.$watch($parse($attrs[scopeName],parentValueWatch),null,parentGet.literal))}),$scope.query=$scope.query||tableConfig.query,$scope.init=$scope.init||{},$scope.init.count=$scope.init.count||tableConfig.init.count,$scope.init.page=$scope.init.page||tableConfig.init.page,$scope.init.sortBy=$scope.init.sortBy||tableConfig.init.sortBy,$scope.init.sortOrder=$scope.init.sortOrder||tableConfig.init.sortOrder,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:$scope.init.count,page:$scope.init.page,pages:1,size:0},$scope.theadDirective=!1,$scope.paginationDirective=!1,!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if(!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if(!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value,["sortBy","sortOrder"].indexOf(key)>=0&&($scope.header[key]=value)},this.bindOnce=tableConfig.bindOnce,$scope.setDirectivesValues=function(resource){if(!angular.isObject(resource))throw"AngularJS tastyTable directive: the resource it's not an object";if(!resource.header&&!resource.rows)throw"AngularJS tastyTable directive: the resource has the property header or rows undefined";1===Object.keys(resource.header[0]).length&&(resource.header=resource.header.map(function(header){var key=Object.keys(header)[0];return{key:key,name:header[key]}})),$scope.header={columns:resource.header,sortBy:$scope.params.sortBy,sortOrder:$scope.params.sortOrder},$scope.rows=resource.rows,$scope.paginationDirective&&resource.pagination&&($scope.pagination.count=resource.pagination.count,$scope.pagination.page=resource.pagination.page,$scope.pagination.pages=resource.pagination.pages,$scope.pagination.size=resource.pagination.size)},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse,listSortBy;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,listSortBy=[function(item){return item[$scope.header.sortBy]}],$scope.header.columns[0].key!==$scope.header.sortBy&&listSortBy.push(function(item){return item[$scope.header.columns[0].key]}),$scope.header.sortBy&&($scope.rows=$filter("orderBy")($scope.rows,listSortBy,reverse))),$attrs.filters&&($scope.rows=$filter("filter")($scope.rows,$scope.filters)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value,listKeyNotJoin;return urlQuery={},listKeyNotJoin=["sortBy","sortOrder","page","count"],$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters,listKeyNotJoin)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},60),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope.filters),$scope.resourceCallback($scope.url,$scope.params).then(function(resource){$scope.setDirectivesValues(resource)})},60),initTable=function(){$scope.clientSide?($scope.params.sortBy=$scope.resource.sortBy||$scope.init.sortBy,$scope.params.sortOrder=$scope.resource.sortOrder||$scope.init.sortOrder,$scope.params.page=$scope.init.page,$scope.resource.pagination&&($scope.params.page=$scope.resource.pagination.page||$scope.init.page),$scope.updateClientSideResource()):($scope.params.sortBy=$scope.init.sortBy,$scope.params.sortOrder=$scope.init.sortOrder,$scope.params.page=$scope.init.page,$scope.updateServerSideResource())},$attrs.filters&&$scope.$watch("filters",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.$watchCollection("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())}),$scope.resource&&$scope.$watch("resource",function(newValue,oldValue){newValue!==oldValue&&($scope.params.sortBy=newValue.sortBy,$scope.params.sortOrder=newValue.sortOrder,$scope.updateClientSideResource())},!0),initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";var iconUp,iconDown;tastyTable.activate("thead"),scope.bindOnce=tastyTable.bindOnce,scope.columns=[],iconUp="fa fa-sort-up",iconDown="fa fa-sort-down",scope.setColumns=function(){var lenHeader,width,active,sortable,sort,isSorted;scope.columns=[],lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,isSorted="",scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),sort=$filter("cleanFieldName")(column.key),scope.header.sortBy==="-"+sort?isSorted=iconDown:scope.header.sortBy===sort&&(isSorted=iconUp),scope.columns.push({key:column.key,name:column.name,active:active,sortable:sortable,width:{width:width+"%"},isSorted:isSorted})}),"dsc"===scope.header.sortOrder&&scope.header.sortBy&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(column){if(scope.notSortBy&&scope.notSortBy.indexOf(column.key)>=0)return!1;var columnName,sortOrder;columnName=$filter("cleanFieldName")(column.key),sortOrder=scope.header.sortBy===columnName?"dsc":"asc",tastyTable.setParams("sortBy",column.key),tastyTable.setParams("sortOrder",sortOrder)},scope.classToShow=function(column){var listClassToShow=[];return column.sortable&&listClassToShow.push("sortable"),column.active&&listClassToShow.push("active"),listClassToShow},tastyTable.$scope.$watchCollection("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setColumns())})}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent[$attrs.itemsPerPage]),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent[$attrs.listItemsPerPage]),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,setPaginationRanges(),void 0)},setPaginationRanges=function(){scope.listItemsPerPageShow=[],scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages,scope.classPageMinRange=scope.pagHideMinRange?"disabled":"",scope.classPageMaxRange=scope.pagHideMaxRange?"disabled":"";for(var i=scope.listItemsPerPage.length;i>=0;i--)if(scope.pagination.size>scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i+1);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.classPaginationCount=function(count){return count==scope.pagination.count?"active":""},scope.classNumPage=function(numPage){return numPage==scope.pagination.page?"active":!1},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watchCollection("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())}),scope.page.setCount(scope.itemsPerPage)}}}]); |
{ | ||
"name": "ng-tasty", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "A lightweight, flexible, and tasty collection of reusable UI components for AngularJS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,3 +10,4 @@ /** | ||
'ngTasty.service.setProperty', | ||
'ngTasty.service.joinObjects' | ||
'ngTasty.service.joinObjects', | ||
'ngTasty.service.webSocket' | ||
]); | ||
@@ -75,5 +76,8 @@ | ||
.factory('joinObjects', function(setProperty) { | ||
return function(objOne, objTwo) { | ||
return function(objOne, objTwo, listKeyNotJoin) { | ||
listKeyNotJoin = listKeyNotJoin || []; | ||
for (var attrname in objTwo) { | ||
setProperty(objOne, objTwo, attrname); | ||
if (listKeyNotJoin.indexOf(attrname) < 0) { | ||
setProperty(objOne, objTwo, attrname); | ||
} | ||
} | ||
@@ -80,0 +84,0 @@ return objOne; |
@@ -17,2 +17,8 @@ /** | ||
.constant('tableConfig', { | ||
init: { | ||
'count': 5, | ||
'page': 1, | ||
'sortBy': undefined, | ||
'sortOrder': undefined | ||
}, | ||
query: { | ||
@@ -30,6 +36,6 @@ 'page': 'page', | ||
'use strict'; | ||
var listScopeToWatch; | ||
var listScopeToWatch, initTable; | ||
this.$scope = $scope; | ||
listScopeToWatch = ['filters', 'query', 'resource', 'resourceCallback']; | ||
listScopeToWatch = ['filters', 'init', 'query', 'resource', 'resourceCallback']; | ||
listScopeToWatch.forEach(function (scopeName) { | ||
@@ -67,2 +73,7 @@ var lastValue, parentGet, compare, parentSet, parentValueWatch; | ||
$scope.query = $scope.query || tableConfig.query; | ||
$scope.init = $scope.init || {}; | ||
$scope.init.count = $scope.init.count || tableConfig.init.count; | ||
$scope.init.page = $scope.init.page || tableConfig.init.page; | ||
$scope.init.sortBy = $scope.init.sortBy || tableConfig.init.sortBy; | ||
$scope.init.sortOrder = $scope.init.sortOrder || tableConfig.init.sortOrder; | ||
@@ -78,4 +89,4 @@ // Defualt variables | ||
$scope.pagination = { | ||
'count': 5, | ||
'page': 1, | ||
'count': $scope.init.count, | ||
'page': $scope.init.page, | ||
'pages': 1, | ||
@@ -85,3 +96,3 @@ 'size': 0 | ||
$scope.theadDirective = false; | ||
$scope.paginationDirective = false; | ||
$scope.paginationDirective = false; | ||
@@ -110,3 +121,3 @@ /* Set custom configs | ||
$scope.clientSide = false; | ||
} | ||
} | ||
@@ -122,2 +133,5 @@ // In TableController, by using `this` we build an API | ||
$scope.params[key] = value; | ||
if (['sortBy', 'sortOrder'].indexOf(key) >= 0) { | ||
$scope.header[key] = value; | ||
} | ||
}; | ||
@@ -128,3 +142,2 @@ | ||
$scope.setDirectivesValues = function (resource) { | ||
var sortBy; | ||
if (!angular.isObject(resource)) { | ||
@@ -148,11 +161,14 @@ throw 'AngularJS tastyTable directive: the resource '+ | ||
} | ||
sortBy = resource.sortBy || $scope.params.sortBy; | ||
sortBy = sortBy || resource.header[0].key; | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
'sortBy': $scope.params.sortBy, | ||
'sortOrder': $scope.params.sortOrder | ||
}; | ||
$scope.rows = resource.rows; | ||
$scope.pagination = resource.pagination || $scope.pagination; | ||
if ($scope.paginationDirective && resource.pagination) { | ||
$scope.pagination.count = resource.pagination.count; | ||
$scope.pagination.page = resource.pagination.page; | ||
$scope.pagination.pages = resource.pagination.pages; | ||
$scope.pagination.size = resource.pagination.size; | ||
} | ||
}; | ||
@@ -172,3 +188,5 @@ | ||
} | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
if ($scope.header.sortBy) { | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
} | ||
} | ||
@@ -193,4 +211,5 @@ if ($attrs.filters) { | ||
$scope.buildUrl = function(params, filters) { | ||
var urlQuery, value, url; | ||
var urlQuery, value, url, listKeyNotJoin; | ||
urlQuery = {}; | ||
listKeyNotJoin = ['sortBy', 'sortOrder', 'page', 'count']; | ||
if ($scope.theadDirective) { | ||
@@ -205,3 +224,3 @@ urlQuery = tastyUtil.setProperty(urlQuery, params, 'sortBy'); | ||
if ($attrs.filters) { | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters); | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters, listKeyNotJoin); | ||
} | ||
@@ -220,3 +239,3 @@ return Object.keys(urlQuery).map(function(key) { | ||
$scope.buildClientResource(); | ||
}, 100); | ||
}, 60); | ||
@@ -228,12 +247,17 @@ $scope.updateServerSideResource = tastyUtil.debounce(function() { | ||
}); | ||
}, 100); | ||
}, 60); | ||
$scope.initTable = function () { | ||
$scope.params['sortBy'] = undefined; | ||
$scope.params['sortOrder'] = 'asc'; | ||
$scope.params['page'] = 1; | ||
$scope.params['count'] = undefined; | ||
initTable = function () { | ||
if ($scope.clientSide) { | ||
$scope.params.sortBy = $scope.resource.sortBy || $scope.init.sortBy; | ||
$scope.params.sortOrder = $scope.resource.sortOrder || $scope.init.sortOrder; | ||
$scope.params.page = $scope.init.page; | ||
if ($scope.resource.pagination) { | ||
$scope.params.page = $scope.resource.pagination.page || $scope.init.page; | ||
} | ||
$scope.updateClientSideResource(); | ||
} else { | ||
$scope.params.sortBy = $scope.init.sortBy; | ||
$scope.params.sortOrder = $scope.init.sortOrder; | ||
$scope.params.page = $scope.init.page; | ||
$scope.updateServerSideResource(); | ||
@@ -267,2 +291,4 @@ } | ||
if (newValue !== oldValue) { | ||
$scope.params.sortBy = newValue.sortBy; | ||
$scope.params.sortOrder = newValue.sortOrder; | ||
$scope.updateClientSideResource(); | ||
@@ -274,3 +300,3 @@ } | ||
// Init table | ||
$scope.initTable(); | ||
initTable(); | ||
}) | ||
@@ -306,10 +332,13 @@ .directive('tastyTable', function(){ | ||
'use strict'; | ||
var iconUp, iconDown; | ||
// Thead it's called | ||
tastyTable.activate('thead'); | ||
scope.bindOnce = tastyTable.bindOnce; | ||
scope.columns = []; | ||
iconUp = 'fa fa-sort-up'; | ||
iconDown = 'fa fa-sort-down'; | ||
scope.setColumns = function () { | ||
var lenHeader, width, i, active, sortable, sort; | ||
var lenHeader, width, i, active, sortable, sort, isSorted; | ||
scope.columns = []; | ||
@@ -321,2 +350,3 @@ lenHeader = scope.header.columns.length; | ||
active = false; | ||
isSorted = ''; | ||
if (scope.notSortBy) { | ||
@@ -330,2 +360,7 @@ sortable = scope.notSortBy.indexOf(column.key) < 0; | ||
sort = $filter('cleanFieldName')(column.key); | ||
if (scope.header.sortBy === '-' + sort) { | ||
isSorted = iconDown; | ||
} else if (scope.header.sortBy === sort) { | ||
isSorted = iconUp; | ||
} | ||
scope.columns.push({ | ||
@@ -337,7 +372,7 @@ 'key': column.key, | ||
'width': { 'width': width + '%' }, | ||
'isSortUp': scope.header.sortBy === '-' + sort, | ||
'isSortDown': scope.header.sortBy === sort | ||
'isSorted': isSorted | ||
}); | ||
}); | ||
if (scope.header.sortOrder === 'dsc' && | ||
if (scope.header.sortOrder === 'dsc' && | ||
scope.header.sortBy && | ||
scope.header.sortBy[0] !== '-') { | ||
@@ -352,12 +387,11 @@ scope.header.sortBy = '-' + scope.header.sortBy; | ||
} | ||
var columnName; | ||
var columnName, sortOrder; | ||
columnName = $filter('cleanFieldName')(column.key); | ||
if (scope.header.sortBy == columnName) { | ||
scope.header.sortBy = '-' + columnName; | ||
tastyTable.setParams('sortOrder', 'dsc'); | ||
if (scope.header.sortBy === columnName) { | ||
sortOrder = 'dsc'; | ||
} else { | ||
scope.header.sortBy = columnName; | ||
tastyTable.setParams('sortOrder', 'asc'); | ||
sortOrder = 'asc'; | ||
} | ||
tastyTable.setParams('sortBy', column.key); | ||
tastyTable.setParams('sortOrder', sortOrder); | ||
}; | ||
@@ -437,3 +471,2 @@ | ||
var maxItems, page; | ||
//scope.pagination.count = count; | ||
maxItems = count * scope.pagination.page; | ||
@@ -482,2 +515,3 @@ if (maxItems > scope.pagination.size) { | ||
setPaginationRanges = function () { | ||
scope.listItemsPerPageShow = []; | ||
scope.pagMinRange = scope.pagMinRange > 0 ? scope.pagMinRange : 1; | ||
@@ -492,8 +526,10 @@ scope.pagMaxRange = scope.pagMinRange + 5; | ||
scope.classPageMaxRange = scope.pagHideMaxRange ? 'disabled' : ''; | ||
for (var i = 2; i < scope.listItemsPerPage.length; i++) { | ||
if (scope.pagination.size < scope.listItemsPerPage[i]) { | ||
scope.listItemsPerPageShow = scope.listItemsPerPage.slice(0, i); | ||
for (var i = scope.listItemsPerPage.length; i >= 0; i--) { | ||
if (scope.pagination.size > scope.listItemsPerPage[i]) { | ||
scope.listItemsPerPageShow = scope.listItemsPerPage.slice(0, (i + 1)); | ||
break; | ||
} | ||
} | ||
scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange); | ||
@@ -500,0 +536,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107471
14
2269