-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-buttonbar-min-0.1.js
More file actions
1 lines (1 loc) · 4.38 KB
/
angular-buttonbar-min-0.1.js
File metadata and controls
1 lines (1 loc) · 4.38 KB
1
angular.module("angular-buttonbar",[]).directive("buttonBar",[function(){var e=function(e){if(!e.dataProvider||!e.dataProvider.length)return e.options.selectedItems=[],void(e.options.selectedIndices=[]);if(e.options.selectedItems||(e.options.selectedItems=[]),e.options.selectedIndices||(e.options.selectedIndices=[]),e.options.selectedItems.length==e.options.selectedIndices.length)return void(e.options.selectedItems.length?(e.options.selectedItem=e.options.selectedItems[0],e.options.selectedIndex=e.options.selectedIndices[0]):(e.options.selectedItem=null,e.options.selectedIndex=-1));if(e.options.selectedItems.length){e.options.selectedIndices=[];for(var t in e.options.selectedItems){var s=e.dataProvider.indexOf(e.options.selectedItems[t]);-1!=s&&e.options.selectedIndices.push(s)}e.options.selectedItems=[];for(var o in e.options.selectedIndices)e.options.selectedItems.push(e.dataProvider[e.options.selectedIndices[o]])}else if(e.options.selectedIndices.length){e.options.selectedItems=[];for(var t in e.options.selectedIndices)e.options.selectedIndices[t]>=0&&e.options.selectedIndices[t]<e.dataProvider.length&&e.options.selectedItems.push(e.dataProvider[e.options.selectedIndices[t]]);e.options.selectedIndices=[];for(var o in e.options.selectedItems)e.options.selectedIndices.push(e.dataProvider.indexOf(e.options.selectedItems[o]))}e.options.selectedItems.length?(e.options.selectedItem=e.options.selectedItems[0],e.options.selectedIndex=e.options.selectedIndices[0]):(e.options.selectedItem=null,e.options.selectedIndex=-1)},t=function(t,s){t.options.enableMultiSelect&&e(t);for(var o=0;t.dataProvider&&o<t.dataProvider.length;o++){var n=t.dataProvider[o],i=document.createElement("span");if(i.innerHTML=t.options.labelFunction?t.options.labelFunction(n):"object"==typeof n&&t.options.labelField?n[t.options.labelField]:n,i.setAttribute("class",t.options.buttonStyleName?t.options.buttonStyleName:"btn btn-default"),t.options.enableMultiSelect){for(var d in t.options.selectedItems)if(n==t.options.selectedItems[d]){i.setAttribute("class",(t.options.buttonStyleName?t.options.buttonStyleName:"btn btn-default")+" active");break}}else(n==t.options.selectedItem||o==t.options.selectedIndex)&&(i.setAttribute("class",(t.options.buttonStyleName?t.options.buttonStyleName:"btn btn-default")+" active"),t.selectedButton=i);i.setAttribute("index",o),i.style.borderRadius="0px",0==o&&(i.style.borderBottomLeftRadius="3px",i.style.borderTopLeftRadius="3px"),o==t.dataProvider.length-1?(i.style.borderBottomRightRadius="3px",i.style.borderTopRightRadius="3px"):i.style.borderRight="none",s.append(i),angular.element(i).on("click",function(s){if(t.options.enableMultiSelect){var o=!1;angular.element(s.currentTarget).hasClass("active")?angular.element(s.currentTarget).removeClass("active"):(angular.element(s.currentTarget).addClass("active"),o=!0);var n=parseInt(s.currentTarget.getAttribute("index"));o?(t.options.selectedItems.push(t.dataProvider[n]),t.options.selectedIndices.push(parseInt(n))):(-1!=t.options.selectedItems.indexOf(t.dataProvider[n])&&t.options.selectedItems.indexOf(t.dataProvider[n])<t.options.selectedItems.length&&t.options.selectedItems.splice(t.options.selectedItems.indexOf(t.dataProvider[n]),1),-1!=t.options.selectedIndices.indexOf(parseInt(n))&&t.options.selectedIndices.indexOf(parseInt(n))<t.options.selectedIndices.length&&t.options.selectedIndices.splice(t.options.selectedIndices.indexOf(parseInt(n)),1)),e(t),t.options.onChange&&t.options.onChange({selectedItem:t.options.selectedItem,selectedIndex:t.options.selectedIndex,selectedItems:t.options.selectedItems,selectedIndices:t.options.selectedIndices})}else{if(t.options.selectedIndex==s.currentTarget.getAttribute("index"))return t.options.selectedIndex=-1,t.options.selectedItem=null,angular.element(s.currentTarget).removeClass("active"),void(t.options.onChange&&t.options.onChange({selectedItem:t.options.selectedItem,selectedIndex:t.options.selectedIndex}));t.selectedButton&&angular.element(t.selectedButton).removeClass("active"),t.selectedButton=s.currentTarget,angular.element(t.selectedButton).addClass("active"),t.options.selectedIndex=s.currentTarget.getAttribute("index"),t.options.selectedItem=t.dataProvider[t.options.selectedIndex],t.options.onChange&&t.options.onChange({selectedItem:t.options.selectedItem,selectedIndex:t.options.selectedIndex})}})}};return{restrict:"E",template:'<div class="button-bar"></div>',scope:{dataProvider:"=source",options:"=options"},link:t}}]);