From a0e6a8e469b138b10e9b63f8ad7ee5ab2ae60979 Mon Sep 17 00:00:00 2001 From: danfergo Date: Mon, 7 Aug 2017 19:23:19 +0100 Subject: [PATCH] updating addChip to handle each render() element, when an array is returned --- dist/angular-chips.js | 78 +++++++++++++++++++++----------------- dist/angular-chips.min.js | 2 +- src/js/directives/chips.js | 10 ++++- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/dist/angular-chips.js b/dist/angular-chips.js index 0b3847f..1c83278 100644 --- a/dist/angular-chips.js +++ b/dist/angular-chips.js @@ -9,6 +9,10 @@ return obj && angular.isFunction(obj.then); } + function isArray(obj) { + return Object.prototype.toString.call(obj) === '[object Array]'; + } + /* * update values to ngModel reference */ @@ -106,7 +110,7 @@ } else { updatedData = data } if (!updatedData) { - return false; + return false; } if (isPromiseLike(updatedData)) { @@ -115,6 +119,10 @@ }); scope.chips.list.push(new DeferChip(data, updatedData)); scope.$apply(); + } else if(isArray(updatedData)){ + for(var i = 0; i < updatedData.length; i++){ + update(updatedData[i]); + } } else { update(updatedData); } @@ -271,6 +279,40 @@ } })(); +(function() { + angular.module('angular.chips') + .factory('DomUtil', function() { + return DomUtil; + }); + /*Dom related functionality*/ + function DomUtil(element) { + /* + * addclass will append class to the given element + * ng-class will do the same functionality, in our case + * we don't have access to scope so we are using this util methods + */ + var utilObj = {}; + + utilObj.addClass = function(className) { + utilObj.removeClass(element, className); + element.attr('class', element.attr('class') + ' ' + className); + return utilObj; + }; + + utilObj.removeClass = function(className) { + var classes = element.attr('class').split(' '); + var classIndex = classes.indexOf(className); + if (classIndex !== -1) { + classes.splice(classIndex, 1); + } + element.attr('class', classes.join(' ')); + return utilObj; + }; + + return utilObj; + } +})(); + (function() { angular.module('angular.chips') .directive('chipTmpl', ChipTmpl); @@ -364,40 +406,6 @@ } })(); -(function() { - angular.module('angular.chips') - .factory('DomUtil', function() { - return DomUtil; - }); - /*Dom related functionality*/ - function DomUtil(element) { - /* - * addclass will append class to the given element - * ng-class will do the same functionality, in our case - * we don't have access to scope so we are using this util methods - */ - var utilObj = {}; - - utilObj.addClass = function(className) { - utilObj.removeClass(element, className); - element.attr('class', element.attr('class') + ' ' + className); - return utilObj; - }; - - utilObj.removeClass = function(className) { - var classes = element.attr('class').split(' '); - var classIndex = classes.indexOf(className); - if (classIndex !== -1) { - classes.splice(classIndex, 1); - } - element.attr('class', classes.join(' ')); - return utilObj; - }; - - return utilObj; - } -})(); - (function() { ChipControlLinkFun.$inject = ["scope", "iElement", "iAttrs", "chipsCtrl"]; angular.module('angular.chips') diff --git a/dist/angular-chips.min.js b/dist/angular-chips.min.js index 0d7fdb1..e2c21fa 100644 --- a/dist/angular-chips.min.js +++ b/dist/angular-chips.min.js @@ -1 +1 @@ -!function(){function e(e){return e&&angular.isFunction(e.then)}function n(e){return{add:function(n){var t=angular.copy(e.$modelValue)||[];t.push(n),e.$setViewValue(t)},"delete":function(n){var t=angular.copy(e.$modelValue);t.splice(n,1),e.$setViewValue(t)},deleteByValue:function(n){var t,i;for(t=0;tl.chips.list.length-1?0:e}}if(""!==(error=o(a)))throw error;var f=n(s),h=u.hasOwnProperty("defer"),m=i(u.render);l.chips.list,l.chips.addChip=function(n){function i(e){l.chips.list.push(e),f.add(e)}var r,o;return void 0!==l.render&&""!==m?(o={},o[m]=n,r=l.render(o)):r=n,!!r&&(e(r)?(r.then(function(e){f.add(e)}),l.chips.list.push(new t(n,r)),l.$apply()):i(r),!0)},l.chips.deleteChip=function(e){var n=l.chips.list.splice(e,1)[0];return n.isFailed?void l.$apply():void(n instanceof t?f.deleteByValue(n.defer):f["delete"](e))},s.$render=function(){if(h&&s.$modelValue){var e,n=[];for(e=0;e"),C=a.html();C=C.substr(C.indexOf("")-"".length),a.find("chip-tmpl").remove();var $=angular.element(C);$.attr("ng-repeat","chip in chips.list track by $index"),$.attr("ng-class","{'chip-failed':chip.isFailed}"),$.attr("tabindex","-1"),$.attr("index","{{$index+1}}"),g.append($);var y=r(g)(l);a.prepend(y),a.on("click",function(e){"CHIPS"===e.target.nodeName&&a.find("input")[0].focus()}),a.find("input").on("focus",function(){v=null}),l.chips.handleKeyDown=function(e){function n(){var n=parseInt(document.activeElement.getAttribute("index"))||(t=a.find("chip-tmpl")).length;t=a.find("chip-tmpl"),t[n-1].focus(),v=p(n-1),"INPUT"!==e.target.nodeName&&t[v(e.keyCode)].focus()}if(!("INPUT"!==e.target.nodeName&&"CHIP-TMPL"!==e.target.nodeName||0===a.find("chip-tmpl").length&&""===e.target.value)){var t;if(8===e.keyCode){if("INPUT"===e.target.nodeName&&""===e.target.value)n(),e.preventDefault();else if("CHIP-TMPL"===e.target.nodeName){var i=a.find("chip-tmpl");i.length>0&&parseInt(e.target.getAttribute("index"))-1===i.length&&a.find("chip-tmpl")[v(37)].focus()}}else 37!==e.keyCode&&39!==e.keyCode||(null===v?n():a.find("chip-tmpl")[v(e.keyCode)].focus())}},a.on("keydown",l.chips.handleKeyDown),c(a).addClass("chip-out-focus")}return a.$inject=["scope","iElement","iAttrs","ngModelCtrl","transcludefn"],{restrict:"E",scope:{render:"&?"},transclude:!0,require:"ngModel",link:a,controller:"chipsController",controllerAs:"chips",template:"
"}}function o(e){return 0===e.find("chip-tmpl").length?"should have chip-tmpl":e.find("chip-tmpl").length>1?"should have only one chip-tmpl":""}function l(e,n,t){this.setFocus=function(e){e?t(n).removeClass("chip-out-focus").addClass("chip-in-focus"):t(n).removeClass("chip-in-focus").addClass("chip-out-focus")},this.removeChip=function(e,n){this.deleteChip(n)}}r.$inject=["$compile","$timeout","DomUtil"],l.$inject=["$scope","$element","DomUtil"],angular.module("angular.chips",[]).directive("chips",r).controller("chipsController",l)}(),function(){function e(){return{restrict:"E",transclude:!0,link:function(e,n,t,i,r){r(e,function(e){n.append(e)}),n.on("keydown",function(n){8===n.keyCode&&(e.$broadcast("chip:delete"),n.preventDefault())})}}}angular.module("angular.chips").directive("chipTmpl",e)}(),function(){function e(){return{restrict:"A",require:"^?chips",link:function(e,n,t,i){function r(e,n){var t;if(n.search("\\(")>0&&(n=n.substr(0,n.search("\\("))),void 0!==n)if(n.split(".").length>1){var i=n.split(".");t=e;for(var r=0;rc.chips.list.length-1?0:e}}if(""!==(error=l(u)))throw error;var h=t(d),m=s.hasOwnProperty("defer"),v=r(s.render);c.chips.list,c.chips.addChip=function(t){function r(e){c.chips.list.push(e),h.add(e)}var o,l;if(void 0!==c.render&&""!==v?(l={},l[v]=t,o=c.render(l)):o=t,!o)return!1;if(e(o))o.then(function(e){h.add(e)}),c.chips.list.push(new i(t,o)),c.$apply();else if(n(o))for(var a=0;a"),$=u.html();$=$.substr($.indexOf("")-"".length),u.find("chip-tmpl").remove();var y=angular.element($);y.attr("ng-repeat","chip in chips.list track by $index"),y.attr("ng-class","{'chip-failed':chip.isFailed}"),y.attr("tabindex","-1"),y.attr("index","{{$index+1}}"),C.append(y);var k=o(C)(c);u.prepend(k),u.on("click",function(e){"CHIPS"===e.target.nodeName&&u.find("input")[0].focus()}),u.find("input").on("focus",function(){g=null}),c.chips.handleKeyDown=function(e){function n(){var n=parseInt(document.activeElement.getAttribute("index"))||(t=u.find("chip-tmpl")).length;t=u.find("chip-tmpl"),t[n-1].focus(),g=f(n-1),"INPUT"!==e.target.nodeName&&t[g(e.keyCode)].focus()}if(!("INPUT"!==e.target.nodeName&&"CHIP-TMPL"!==e.target.nodeName||0===u.find("chip-tmpl").length&&""===e.target.value)){var t;if(8===e.keyCode){if("INPUT"===e.target.nodeName&&""===e.target.value)n(),e.preventDefault();else if("CHIP-TMPL"===e.target.nodeName){var i=u.find("chip-tmpl");i.length>0&&parseInt(e.target.getAttribute("index"))-1===i.length&&u.find("chip-tmpl")[g(37)].focus()}}else 37!==e.keyCode&&39!==e.keyCode||(null===g?n():u.find("chip-tmpl")[g(e.keyCode)].focus())}},u.on("keydown",c.chips.handleKeyDown),a(u).addClass("chip-out-focus")}return u.$inject=["scope","iElement","iAttrs","ngModelCtrl","transcludefn"],{restrict:"E",scope:{render:"&?"},transclude:!0,require:"ngModel",link:u,controller:"chipsController",controllerAs:"chips",template:"
"}}function l(e){return 0===e.find("chip-tmpl").length?"should have chip-tmpl":e.find("chip-tmpl").length>1?"should have only one chip-tmpl":""}function c(e,n,t){this.setFocus=function(e){e?t(n).removeClass("chip-out-focus").addClass("chip-in-focus"):t(n).removeClass("chip-in-focus").addClass("chip-out-focus")},this.removeChip=function(e,n){this.deleteChip(n)}}o.$inject=["$compile","$timeout","DomUtil"],c.$inject=["$scope","$element","DomUtil"],angular.module("angular.chips",[]).directive("chips",o).controller("chipsController",c)}(),function(){function e(){return{restrict:"E",transclude:!0,link:function(e,n,t,i,r){r(e,function(e){n.append(e)}),n.on("keydown",function(n){8===n.keyCode&&(e.$broadcast("chip:delete"),n.preventDefault())})}}}angular.module("angular.chips").directive("chipTmpl",e)}(),function(){function e(){return{restrict:"A",require:"^?chips",link:function(e,n,t,i){function r(e,n){var t;if(n.search("\\(")>0&&(n=n.substr(0,n.search("\\("))),void 0!==n)if(n.split(".").length>1){var i=n.split(".");t=e;for(var r=0;r