-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.min.js
1 lines (1 loc) · 8.58 KB
/
table.min.js
1
"use strict";var module=angular.module("90TechSAS.zl-table",[]);module.directive("zlTable",["$compile","$timeout","$templateCache",function(e,t,n){var l=["id","_id","uid","uuid","$uid"];function a(e){return _.reduce(e,(function(e,t){return t.name&&t.value&&(e+=t.name+"="+t.value+" "),e}),"")}return{restrict:"A",controllerAs:"ctrl",scope:{},bindToController:{zlTable:"=",columns:"=",update:"&",pagination:"=zlPagination",selectedData:"=",selectionChanged:"&",gridMode:"=",gridTemplate:"=",rowClickFn:"&zlRowClick",idField:"@"},compile:function(l){var i,r,o;i=l;var c=_.find(l.children(),"tagName","THEAD");if(!c)throw"zl-table: The table should have one thead child";var d=c.attributes,s=_.find(l.children(),"tagName","TBODY");if(!s)throw"zl-table: The table should have one tbody child";var u,p=s.attributes,g=function(e,t){var n=_.find(e.children,"tagName","TR");if(!n)throw"zl-table: The thead element should have one tr child";o=n.attributes;var l=_.find(t.children,"tagName","TR");if(!l)throw"zl-table: The tbody element should have one tr child";return r=l.attributes,_.compact(_.map(n.children,(function(e,t){if(!e.attributes.getNamedItem("id"))throw"zl-table: The head cells should have an id";return{id:e.attributes.getNamedItem("id").value,headTemplate:e.innerHTML,template:l.children[t].innerHTML}})))}(c,s),f=function(e,t){var n='<thead ng-if="!ctrl.gridMode"'+a(e)+"><tr "+a(t)+'><th ng-click="ctrl.selectAll()" ng-if="ctrl.selectedData"><input name="header-check" type="checkbox" ng-click="ctrl.selectAll(); $event.stopImmediatePropagation()" ng-checked="ctrl.areAllSelected()"/><label for="header-check" ng-click="ctrl.selectAll(); $event.stopImmediatePropagation()" ><span></span></label></th><th ng-repeat="col in availableColumns | zlColumnFilter:ctrl.columns track by col.id" id="{{col.id}}" ng-click="ctrl.order(col.id)" zl-drag-drop zl-drag="col.id" zl-drop="ctrl.dropColumn($data, col.id)"class="zl-col"ng-class="{\'zl-col-ordered\': ctrl.pagination.orderBy == col.id}"><div class="zl-th-container"ng-class="{\'zl-col-sortable\': ctrl.isSortable(col), \'zl-col-reverse\': ctrl.pagination.orderBy == col.id && ctrl.pagination.reverse}"><zl-template-compiler template="{{col.headTemplate}}"></zl-template-compiler><button ng-click="ctrl.dismiss(col.id)" class="zl-table-del-btn"></button></div></th></tr></thead>';return n}(d,o),m=function(e,t){var n='<tbody ng-if="!ctrl.gridMode"'+a(e)+"><tr "+a(t)+'class="noselect" ng-repeat="elt in ctrl.zlTable track by ctrl.getIdValue(elt, $index)" ng-click="ctrl.rowClick($event, elt)" ng-class="{\'zl-row-selected\': ctrl.isSelected(elt)}"><td ng-if="ctrl.selectedData" ng-click="ctrl.selectClick($event, elt); $event.stopImmediatePropagation()"><input name="{{elt._id}}" type="checkbox" ng-click="ctrl.selectClick($event, elt); $event.stopImmediatePropagation()" ng-checked="ctrl.isSelected(elt)"/><label for="{{elt._id}}" ng-click="ctrl.selectClick($event, elt); $event.stopImmediatePropagation()" ><span></span></label></td><td class="zl-td" ng-repeat="col in availableColumns | zlColumnFilter:ctrl.columns track by col.id"><zl-template-compiler template="{{col.template}}"></zl-template-compiler></td></tr></tbody>';return n}(p,r);return c.remove(),s.remove(),{pre:function(l,a){l.availableColumns=g,a.append(e(f)(l)),a.append(e(m)(l)),t((function(){null!=i[0].attributes.getNamedItem("grid-template")&&(u='<div ng-if="ctrl.gridMode"><div class="noselect" style="max-width:400px; float:left;margin:1%;" ng-repeat="elt in ctrl.zlTable | orderBy:ctrl.orderBy:ctrl.reverse"ng-class="{\'zl-row-selected\': ctrl.isSelected(elt)}"ng-click="ctrl.selectClick($event, elt)"><zl-template-compiler template="'+function(e){if(e)return e.replace(/"/g,""")}(n.get(i[0].attributes.getNamedItem("grid-template").value))+'"></zl-template-compiler></div></div><div style="clear:both;"></div>',a.after(e(u)(l)))}))}}},controller:["$scope",function(e){var t=this;function n(){return!_.difference(_.map(t.zlTable,(function(e){return e[t.idField]})),t.selectedData).length}function a(e,n){if(e.shiftKey){if(i(n))return;var l=t.selectedData[t.selectedData.length-1]||r(t.zlTable[0]),a=!1;_.each(t.zlTable,(function(e){r(e)!==l&&e!==n||(a=!a),a&&!i(e)&&t.selectedData.push(r(e))})),i(n)||t.selectedData.push(r(n)),t.selectionChanged({$selectedData:t.selectedData})}else t.rowClickFn&&t.rowClickFn({$event:e,$elt:n})}function i(e){return _.includes(t.selectedData,r(e))}function r(e,n){if(!t.idField){var a=_.find(l,(function(t){return e[t]}));a&&(t.idField=a)}return t.idField?e[t.idField]:n}function o(){t.update({$pagination:t.pagination})}e.$watchGroup([function(){return t.pagination.perPage},function(){return t.pagination.currentPage},function(){return t.columns}],(function(){t.pagination=t.pagination||{},t.pagination.currentPage=t.pagination.currentPage||0,t.pagination.perPage=t.pagination.perPage||10,o()}),!0),e.$watchCollection((function(){return t.columns}),(function(){t.columns.forEach((function(t){if(/^customFields\./.test(t.id)&&!_.find(e.availableColumns,{id:t.id})){var n=_.clone(_.find(e.availableColumns,{id:"customFields"}));n.id=t.id,n.label=t.label,e.availableColumns.push(n)}}))})),_.extend(t,{order:function(e){var n=_.find(t.columns,{id:e});n&&!1===n.sortable||(t.pagination.orderBy=e,this.reverse=!this.reverse,t.pagination.reverse=this.reverse,o())},display:function(e){return _.contains(t.columns,e)},dismiss:function(e){_.pull(t.columns,e)},rowClick:a,isSelected:i,selectClick:function(e,n){e.shiftKey&&!i(n)?a(e,n):(i(n)?_.remove(t.selectedData,(function(e){return e===r(n)})):t.selectedData.push(r(n)),t.selectionChanged({$selectedData:t.selectedData}))},selectAll:function(){n()?t.selectedData=[]:t.selectedData=_.map(t.zlTable,(function(e){return e[t.idField]}))},dropColumn:function(n,l){var a=_.findIndex(t.columns,{id:l}),i=_.findIndex(t.columns,{id:n});t.columns.splice(a,0,t.columns.splice(i,1)[0]),e.$apply()},availableColumns:e.availableColumns,areAllSelected:n,isSortable:function(e){var n=_.find(t.columns,{id:e.id});return n&&!1!==n.sortable},getIdValue:r})}]}}]),module.directive("zlPaginate",["$compile","$timeout",function(e,t){return{restrict:"E",controllerAs:"paginationCtrl",scope:{},template:'<button class="waves-effect waves-teal btn-flat" ng-if="paginationCtrl.pagination.currentPage != 0" ng-click="paginationCtrl.previousPage()"><</button><button class="waves-effect waves-teal btn-flat" ng-repeat="elt in paginationCtrl.paginateArray() track by $index" ng-click="paginationCtrl.page($index)">{{$index +1}}</button><button class="waves-effect waves-teal btn-flat" ng-if="paginationCtrl.pagination.currentPage < paginationCtrl.paginateArray().length -1" ng-click="paginationCtrl.nextPage()">></button>',bindToController:{update:"&",pagination:"=zlPagination"},controller:function(){var e=this;_.extend(e,{paginateArray:function(){var t=Math.ceil(e.pagination.totalElements/e.pagination.perPage);return new Array(isNaN(t)?0:t)},page:function(t){e.pagination.currentPage=t,e.update({$pagination:e.pagination})},previousPage:function(){e.page(e.pagination.currentPage-1)},nextPage:function(){e.page(e.pagination.currentPage+1)}})}}}]),module.directive("zlDragDrop",(function(){return{controller:function(){},scope:{},controllerAs:"dragDropCtrl",bindToController:{zlDrag:"=",zlDrop:"&"},link:function(e,t){var n=t[0];window.addEventListener("dragover",(function(e){e.preventDefault()}),!1),window.addEventListener("drop",(function(e){e.preventDefault()}),!1),e.dragDropCtrl.zlDrag&&(n.draggable=!0,n.addEventListener("dragstart",(function(t){return t.dataTransfer.effectAllowed="move",t.dataTransfer.setData("Text",e.dragDropCtrl.zlDrag),this.classList.add("drag"),!1}),!1),n.addEventListener("dragend",(function(e){return this.classList.remove("drag"),!1}),!1),n.addEventListener("dragover",(function(e){return e.dataTransfer.dropEffect="move",e.preventDefault&&e.preventDefault(),this.classList.add("over"),!1}),!1)),e.dragDropCtrl.zlDrop&&(n.addEventListener("drop",(function(t){return t.preventDefault(),t.stopPropagation(),t.stopPropagation&&t.stopPropagation(),this.classList.remove("over"),e.dragDropCtrl.zlDrop({$data:t.dataTransfer.getData("Text"),$event:t}),!1}),!1),n.addEventListener("dragenter",(function(e){return this.classList.add("over"),!1}),!1),n.addEventListener("dragleave",(function(e){return this.classList.remove("over"),!1}),!1))}}})),module.directive("zlTemplateCompiler",["$compile",function(e){return{restrict:"E",link:function(t,n,l){var a='<div style="display:inline;margin:auto;">'+l.template+"</div>";n.replaceWith(e(a)(t))}}}]),module.filter("zlColumnFilter",(function(){return function(e,t){var n=_.filter(t,"visible").map((e=>e.id));return _.sortBy(_.reject(e,(function(e){return!_.includes(n,e.id)})),(function(e){return n.indexOf(e.id)}))}}));