ng-checkbox
Version:
The component provides selection data and its statistics based on checkboxes selection.
1 lines • 3.63 kB
JavaScript
function NgCheckboxController(t,n){var o="_";this.ngCheckboxStatistics=n,this.$scope=t,this.superGroup=t.superGroup||"default",this.groups=t.groups?angular.isArray(t.groups)?t.groups:[t.groups]:[],this.head=t.head===!0||t.head===!1?t.head:!1,this.id=t.id?t.id:"ngCb"+o+_.uniqueId(this.groups?this.groups.join(o)+o:""),this.sum=angular.isArray(t.sum)?_.sum(t.sum):isNaN(t.sum)?0:t.sum,this.count=parseFloat(t.count)||1,this.unit=t.unit||"",this.setValue(t.ngModel)}NgCheckboxController.$inject=["$scope","ngCheckboxStatistics"],angular.module("ngCheckbox",["templates.ngCheckbox"]),function(t){try{t=angular.module("templates.ngCheckbox")}catch(n){t=angular.module("templates.ngCheckbox",[])}t.run(["$templateCache",function(t){t.put("ngCheckbox.tpl.html",'<label for="{{checkboxCtrl.id}}">\n <input type="checkbox" class="ngCheckbox" id="{{checkboxCtrl.id}}" ng-model="checkboxCtrl.ngModel" ng-change="checkboxCtrl.onValueChange(checkboxCtrl.ngModel)" />\n <span ng-transclude></span>\n</label>\n')}])}(),NgCheckboxController.prototype.setValue=function(t){this.ngModel=this.$scope.ngModel=t===!0||t===!1?t:!0},NgCheckboxController.prototype.onValueChange=function(t){this.setValue(t);var n=this.ngModel?this.id:null,o=this.ngModel?null:this.id;this.head?this.ngCheckboxStatistics.updateAndbroadcast(this.superGroup,this.groups,this.ngModel,n,o):this.ngCheckboxStatistics.updateAndbroadcast(this.superGroup,null,null,n,o)},angular.module("ngCheckbox").controller("NgCheckboxController",NgCheckboxController),angular.module("ngCheckbox").directive("ngCheckbox",["ngCheckboxStatistics",function(t){return{restrict:"E",transclude:!0,scope:{head:"=",id:"@",superGroup:"@",groups:"=",ngModel:"=?ngModel",sum:"=",count:"@",unit:"@"},templateUrl:"ngCheckbox.tpl.html",controllerAs:"checkboxCtrl",controller:"NgCheckboxController",link:function(n,o,e,u){u.groups&&t.registerCheckboxCtrl(u),n.$watch("ngModel",function(t){u.setValue(t)}),n.$on("$destroy",function(){u.groups&&t.unregisterCheckboxCtrl(u)})}}}]),angular.module("ngCheckbox").factory("ngCheckboxStatistics",["$timeout",function(t){var n,o=[],e={},u=function(t){o.push(t),g()},r=function(t){o.length>0&&(_.remove(o,{id:t.id}),t.ngModel&&g())},s=function(t,n,o){var o=o||"default";"function"==typeof t&&(e[o]=e[o]||[],e[o].push({callBack:t,thisArg:n||null}))},c=function(t){var n=[];for(var o in t)t.hasOwnProperty(o)&&n.push({unit:o,sum:t[o]});return n},i=function(t,n,e,u,r){var s={},i={},l=[],a={selectedId:u,unselectedId:r,selectedIds:l,superGroup:t,groups:s,count:0,sumByUnits:i};angular.forEach(o,function(o){o.superGroup===t&&angular.forEach(o.groups,function(t){n&&(0===n.length||n.indexOf(t)>-1)&&o.setValue(e),o.head||(s[t]=s[t]||{},s[t].count=s[t].count||0,s[t].sumByUnits=s[t].sumByUnits||{},s[t].sumByUnits[o.unit]=s[t].sumByUnits[o.unit]||0,i[o.unit]=i[o.unit]||0,o.ngModel&&(l.push(o.id),s[t].count+=o.count,a.count+=o.count,s[t].sumByUnits[o.unit]+=o.count*o.sum,i[o.unit]+=o.count*o.sum))})}),a.sumByUnits=c(a.sumByUnits);for(var h in a.groups)a.groups.hasOwnProperty(h)&&(a.groups[h].sumByUnits=c(a.groups[h].sumByUnits));return a},l=function(t,n){(t.callBack||angular.noop).apply(t.thisArg?t.thisArg:null,[n])},a=function(t,n,o,u,r){var s=e[t];angular.forEach(s,function(e){var s=i(t,n,o,u,r);l(e,s)})},h=function(){for(var t in e)e.hasOwnProperty(t)&&a(t)},g=function(o){t.cancel(n),n=t(function(){h()},o||20)},d=function(){o=[]},p=function(){d(),e={},n=void 0};return{addListener:s,registerCheckboxCtrl:u,unregisterCheckboxCtrl:r,update:i,broadcast:l,updateAndbroadcast:a,updateAndBroadcastAll:h,debounceUpdateAndBroadcastAll:g,reset:d,resetHard:p}}]);