nya-bs-select.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. /**
  2. * nya-bootstrap-select v2.1.6
  3. * Copyright 2014 Nyasoft
  4. * Licensed under MIT license
  5. */
  6. (function(){
  7. 'use strict';
  8. var uid = 0;
  9. function nextUid() {
  10. return ++uid;
  11. }
  12. /**
  13. * Checks if `obj` is a window object.
  14. *
  15. * @private
  16. * @param {*} obj Object to check
  17. * @returns {boolean} True if `obj` is a window obj.
  18. */
  19. function isWindow(obj) {
  20. return obj && obj.window === obj;
  21. }
  22. /**
  23. * @ngdoc function
  24. * @name angular.isString
  25. * @module ng
  26. * @kind function
  27. *
  28. * @description
  29. * Determines if a reference is a `String`.
  30. *
  31. * @param {*} value Reference to check.
  32. * @returns {boolean} True if `value` is a `String`.
  33. */
  34. function isString(value){return typeof value === 'string';}
  35. /**
  36. * @param {*} obj
  37. * @return {boolean} Returns true if `obj` is an array or array-like object (NodeList, Arguments,
  38. * String ...)
  39. */
  40. function isArrayLike(obj) {
  41. if (obj == null || isWindow(obj)) {
  42. return false;
  43. }
  44. var length = obj.length;
  45. if (obj.nodeType === 1 && length) {
  46. return true;
  47. }
  48. return isString(obj) || Array.isArray(obj) || length === 0 ||
  49. typeof length === 'number' && length > 0 && (length - 1) in obj;
  50. }
  51. /**
  52. * Creates a new object without a prototype. This object is useful for lookup without having to
  53. * guard against prototypically inherited properties via hasOwnProperty.
  54. *
  55. * Related micro-benchmarks:
  56. * - http://jsperf.com/object-create2
  57. * - http://jsperf.com/proto-map-lookup/2
  58. * - http://jsperf.com/for-in-vs-object-keys2
  59. *
  60. * @returns {Object}
  61. */
  62. function createMap() {
  63. return Object.create(null);
  64. }
  65. /**
  66. * Computes a hash of an 'obj'.
  67. * Hash of a:
  68. * string is string
  69. * number is number as string
  70. * object is either result of calling $$hashKey function on the object or uniquely generated id,
  71. * that is also assigned to the $$hashKey property of the object.
  72. *
  73. * @param obj
  74. * @returns {string} hash string such that the same input will have the same hash string.
  75. * The resulting string key is in 'type:hashKey' format.
  76. */
  77. function hashKey(obj, nextUidFn) {
  78. var objType = typeof obj,
  79. key;
  80. if (objType == 'function' || (objType == 'object' && obj !== null)) {
  81. if (typeof (key = obj.$$hashKey) == 'function') {
  82. // must invoke on object to keep the right this
  83. key = obj.$$hashKey();
  84. } else if (key === undefined) {
  85. key = obj.$$hashKey = (nextUidFn || nextUid)();
  86. }
  87. } else {
  88. key = obj;
  89. }
  90. return objType + ':' + key;
  91. }
  92. //TODO: use with caution. if an property of element in array doesn't exist in group, the resultArray may lose some element.
  93. function sortByGroup(array ,group, property) {
  94. var unknownGroup = [],
  95. i, j,
  96. resultArray = [];
  97. for(i = 0; i < group.length; i++) {
  98. for(j = 0; j < array.length;j ++) {
  99. if(!array[j][property]) {
  100. unknownGroup.push(array[j]);
  101. } else if(array[j][property] === group[i]) {
  102. resultArray.push(array[j]);
  103. }
  104. }
  105. }
  106. resultArray = resultArray.concat(unknownGroup);
  107. return resultArray;
  108. }
  109. /**
  110. * Return the DOM siblings between the first and last node in the given array.
  111. * @param {Array} array like object
  112. * @returns {jqLite} jqLite collection containing the nodes
  113. */
  114. function getBlockNodes(nodes) {
  115. // TODO(perf): just check if all items in `nodes` are siblings and if they are return the original
  116. // collection, otherwise update the original collection.
  117. var node = nodes[0];
  118. var endNode = nodes[nodes.length - 1];
  119. var blockNodes = [node];
  120. do {
  121. node = node.nextSibling;
  122. if (!node) break;
  123. blockNodes.push(node);
  124. } while (node !== endNode);
  125. return angular.element(blockNodes);
  126. }
  127. var getBlockStart = function(block) {
  128. return block.clone[0];
  129. };
  130. var getBlockEnd = function(block) {
  131. return block.clone[block.clone.length - 1];
  132. };
  133. var updateScope = function(scope, index, valueIdentifier, value, keyIdentifier, key, arrayLength, group) {
  134. // TODO(perf): generate setters to shave off ~40ms or 1-1.5%
  135. scope[valueIdentifier] = value;
  136. if (keyIdentifier) scope[keyIdentifier] = key;
  137. scope.$index = index;
  138. scope.$first = (index === 0);
  139. scope.$last = (index === (arrayLength - 1));
  140. scope.$middle = !(scope.$first || scope.$last);
  141. // jshint bitwise: false
  142. scope.$odd = !(scope.$even = (index&1) === 0);
  143. // jshint bitwise: true
  144. if(group) {
  145. scope.$group = group;
  146. }
  147. };
  148. var setElementIsolateScope = function(element, scope) {
  149. element.data('isolateScope', scope);
  150. };
  151. var contains = function(array, element) {
  152. var length = array.length,
  153. i;
  154. if(length === 0) {
  155. return false;
  156. }
  157. for(i = 0;i < length; i++) {
  158. if(deepEquals(element, array[i])) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. };
  164. var indexOf = function(array, element) {
  165. var length = array.length,
  166. i;
  167. if(length === 0) {
  168. return -1;
  169. }
  170. for(i = 0; i < length; i++) {
  171. if(deepEquals(element, array[i])) {
  172. return i;
  173. }
  174. }
  175. return -1;
  176. };
  177. /**
  178. * filter the event target for the nya-bs-option element.
  179. * Use this method with event delegate. (attach a event handler on an parent element and listen the special children elements)
  180. * @param target event.target node
  181. * @param parent {object} the parent, where the event handler attached.
  182. * @param selector {string}|{object} a class or DOM element
  183. * @return the filtered target or null if no element satisfied the selector.
  184. */
  185. var filterTarget = function(target, parent, selector) {
  186. var elem = target,
  187. className, type = typeof selector;
  188. if(target == parent) {
  189. return null;
  190. } else {
  191. do {
  192. if(type === 'string') {
  193. className = ' ' + elem.className + ' ';
  194. if(elem.nodeType === 1 && className.replace(/[\t\r\n\f]/g, ' ').indexOf(selector) >= 0) {
  195. return elem;
  196. }
  197. } else {
  198. if(elem == selector) {
  199. return elem;
  200. }
  201. }
  202. } while((elem = elem.parentNode) && elem != parent && elem.nodeType !== 9);
  203. return null;
  204. }
  205. };
  206. var getClassList = function(element) {
  207. var classList,
  208. className = element.className.replace(/[\t\r\n\f]/g, ' ').trim();
  209. classList = className.split(' ');
  210. for(var i = 0; i < classList.length; i++) {
  211. if(/\s+/.test(classList[i])) {
  212. classList.splice(i, 1);
  213. i--;
  214. }
  215. }
  216. return classList;
  217. };
  218. // work with node element
  219. var hasClass = function(element, className) {
  220. var classList = getClassList(element);
  221. return classList.indexOf(className) !== -1;
  222. };
  223. // query children by class(one or more)
  224. var queryChildren = function(element, classList) {
  225. var children = element.children(),
  226. length = children.length,
  227. child,
  228. valid,
  229. classes;
  230. if(length > 0) {
  231. for(var i = 0; i < length; i++) {
  232. child = children.eq(i);
  233. valid = true;
  234. classes = getClassList(child[0]);
  235. if(classes.length > 0) {
  236. for(var j = 0; j < classList.length; j++) {
  237. if(classes.indexOf(classList[j]) === -1) {
  238. valid = false;
  239. break;
  240. }
  241. }
  242. }
  243. if(valid) {
  244. return child;
  245. }
  246. }
  247. }
  248. return [];
  249. };
  250. /**
  251. * Current support only drill down one level.
  252. * case insensitive
  253. * @param element
  254. * @param keyword
  255. */
  256. var hasKeyword = function(element, keyword) {
  257. var childElements,
  258. index, length;
  259. if(element.text().toLowerCase().indexOf(keyword.toLowerCase()) !== -1) {
  260. return true;
  261. } else {
  262. childElements = element.children();
  263. length = childElements.length;
  264. for(index = 0; index < length; index++) {
  265. if(childElements.eq(index).text().toLowerCase().indexOf(keyword.toLowerCase()) !== -1) {
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. };
  272. function sibling( cur, dir ) {
  273. while ( (cur = cur[dir]) && cur.nodeType !== 1) {}
  274. return cur;
  275. }
  276. // map global property to local variable.
  277. var jqLite = angular.element;
  278. var deepEquals = angular.equals;
  279. var deepCopy = angular.copy;
  280. var extend = angular.extend;
  281. var nyaBsSelect = angular.module('nya.bootstrap.select', []);
  282. /**
  283. * A service for configuration. the configuration is shared globally.
  284. * Testing ci build --jpmckearin
  285. */
  286. nyaBsSelect.provider('nyaBsConfig', function() {
  287. var locale = null;
  288. // default localized text. cannot be modified.
  289. var defaultText = {
  290. 'en-us': {
  291. defaultNoneSelection: 'Nothing selected',
  292. noSearchResult: 'NO SEARCH RESULT',
  293. numberItemSelected: '%d items selected',
  294. selectAll: 'Select All',
  295. deselectAll: 'Deselect All'
  296. }
  297. };
  298. // localized text which actually being used.
  299. var interfaceText = deepCopy(defaultText);
  300. /**
  301. * Merge with default localized text.
  302. * @param localeId a string formatted as languageId-countryId
  303. * @param obj localized text object.
  304. */
  305. this.setLocalizedText = function(localeId, obj) {
  306. if(!localeId) {
  307. throw new Error('localeId must be a string formatted as languageId-countryId');
  308. }
  309. if(!interfaceText[localeId]) {
  310. interfaceText[localeId] = {};
  311. }
  312. interfaceText[localeId] = extend(interfaceText[localeId], obj);
  313. };
  314. /**
  315. * Force to use a special locale id. if localeId is null. reset to user-agent locale.
  316. * @param localeId a string formatted as languageId-countryId
  317. */
  318. this.useLocale = function(localeId) {
  319. locale = localeId;
  320. };
  321. /**
  322. * get the localized text according current locale or forced locale
  323. * @returns localizedText
  324. */
  325. this.$get = ['$locale', function($locale){
  326. var localizedText;
  327. if(locale) {
  328. localizedText = interfaceText[locale];
  329. } else {
  330. localizedText = interfaceText[$locale.id];
  331. }
  332. if(!localizedText) {
  333. localizedText = defaultText['en-us'];
  334. }
  335. return localizedText;
  336. }];
  337. });
  338. nyaBsSelect.controller('nyaBsSelectCtrl', function(){
  339. var self = this;
  340. // keyIdentifier and valueIdentifier are set by nyaBsOption directive
  341. // used by nyaBsSelect directive to retrieve key and value from each nyaBsOption's child scope.
  342. self.keyIdentifier = null;
  343. self.valueIdentifier = null;
  344. self.isMultiple = false;
  345. // Should be override by nyaBsSelect directive and called by nyaBsOption directive when collection is changed.
  346. self.onCollectionChange = function(){};
  347. // for debug
  348. self.setId = function(id) {
  349. self.id = id || 'id#' + Math.floor(Math.random() * 10000);
  350. };
  351. });
  352. nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', '$compile', 'nyaBsConfig', function ($parse, $document, $timeout, $compile, nyaBsConfig) {
  353. var DEFAULT_NONE_SELECTION = 'Nothing selected';
  354. var DROPDOWN_TOGGLE = '<button class="btn btn-default dropdown-toggle" type="button">' +
  355. '<span class="pull-left filter-option"></span>' +
  356. '<span class="pull-left special-title"></span>' +
  357. '&nbsp;' +
  358. '<span class="caret"></span>' +
  359. '</button>';
  360. var DROPDOWN_CONTAINER = '<div class="dropdown-menu open"></div>';
  361. var SEARCH_BOX = '<div class="bs-searchbox">' +
  362. '<input type="text" class="form-control">' +
  363. '</div>';
  364. var DROPDOWN_MENU = '<ul class="dropdown-menu inner"></ul>';
  365. var NO_SEARCH_RESULT = '<li class="no-search-result"><span>NO SEARCH RESULT</span></li>';
  366. var ACTIONS_BOX = '<div class="bs-actionsbox">' +
  367. '<div class="btn-group btn-group-sm btn-block">' +
  368. '<button class="actions-btn bs-select-all btn btn-default">SELECT ALL</button>' +
  369. '<button class="actions-btn bs-deselect-all btn btn-default">DESELECT ALL</button>' +
  370. '</div>' +
  371. '</div>';
  372. return {
  373. restrict: 'ECA',
  374. require: ['ngModel', 'nyaBsSelect'],
  375. controller: 'nyaBsSelectCtrl',
  376. compile: function nyaBsSelectCompile (tElement, tAttrs){
  377. tElement.addClass('btn-group');
  378. /**
  379. * get the default text when nothing is selected. can be template
  380. * @param scope, if provided, will try to compile template with given scope, will not attempt to compile the pure text.
  381. * @returns {*}
  382. */
  383. var getDefaultNoneSelectionContent = function(scope) {
  384. // text node or jqLite element.
  385. var content;
  386. if(tAttrs.titleTpl) {
  387. // use title-tpl attribute value.
  388. content = jqLite(tAttrs.titleTpl);
  389. } else if(tAttrs.title) {
  390. // use title attribute value.
  391. content = document.createTextNode(tAttrs.title);
  392. } else if(localizedText.defaultNoneSelectionTpl){
  393. // use localized text template.
  394. content = jqLite(localizedText.defaultNoneSelectionTpl);
  395. } else if(localizedText.defaultNoneSelection) {
  396. // use localized text.
  397. content = document.createTextNode(localizedText.defaultNoneSelection);
  398. } else {
  399. // use default.
  400. content = document.createTextNode(DEFAULT_NONE_SELECTION);
  401. }
  402. if(scope && (tAttrs.titleTpl || localizedText.defaultNoneSelectionTpl)) {
  403. return $compile(content)(scope);
  404. }
  405. return content;
  406. };
  407. var options = tElement.children(),
  408. dropdownToggle = jqLite(DROPDOWN_TOGGLE),
  409. dropdownContainer = jqLite(DROPDOWN_CONTAINER),
  410. dropdownMenu = jqLite(DROPDOWN_MENU),
  411. searchBox,
  412. noSearchResult,
  413. actionsBox,
  414. classList,
  415. length,
  416. index,
  417. liElement,
  418. localizedText = nyaBsConfig,
  419. isMultiple = typeof tAttrs.multiple !== 'undefined',
  420. nyaBsOptionValue;
  421. classList = getClassList(tElement[0]);
  422. classList.forEach(function(className) {
  423. if(/btn-(?:primary|info|success|warning|danger|inverse)/.test(className)) {
  424. tElement.removeClass(className);
  425. dropdownToggle.removeClass('btn-default');
  426. dropdownToggle.addClass(className);
  427. }
  428. if(/btn-(?:lg|sm|xs)/.test(className)) {
  429. tElement.removeClass(className);
  430. dropdownToggle.addClass(className);
  431. }
  432. if(className === 'form-control') {
  433. dropdownToggle.addClass(className);
  434. }
  435. });
  436. dropdownMenu.append(options);
  437. // add tabindex to children anchor elements if not present.
  438. // tabindex attribute will give an anchor element ability to be get focused.
  439. length = options.length;
  440. for(index = 0; index < length; index++) {
  441. liElement = options.eq(index);
  442. if(liElement.hasClass('nya-bs-option') || liElement.attr('nya-bs-option')) {
  443. liElement.find('a').attr('tabindex', '0');
  444. // In order to be compatible with old version, we should copy value of value attribute into data-value attribute.
  445. // For the reason we use data-value instead, see http://nya.io/AngularJS/Beware-Of-Using-value-Attribute-On-list-element/
  446. nyaBsOptionValue = liElement.attr('value');
  447. if(angular.isString(nyaBsOptionValue) && nyaBsOptionValue !== '') {
  448. liElement.attr('data-value', nyaBsOptionValue);
  449. liElement.removeAttr('value');
  450. }
  451. }
  452. }
  453. if(tAttrs.liveSearch === 'true') {
  454. searchBox = jqLite(SEARCH_BOX);
  455. if(tAttrs.noSearchTitle) {
  456. NO_SEARCH_RESULT = NO_SEARCH_RESULT.replace('NO SEARCH RESULT', tAttrs.noSearchTitle);
  457. } else if (tAttrs.noSearchTitleTpl) {
  458. NO_SEARCH_RESULT = NO_SEARCH_RESULT.replace('NO SEARCH RESULT', tAttrs.noSearchTitleTpl);
  459. }else {
  460. // set localized text
  461. if(localizedText.noSearchResultTpl) {
  462. NO_SEARCH_RESULT = NO_SEARCH_RESULT.replace('NO SEARCH RESULT', localizedText.noSearchResultTpl);
  463. } else if(localizedText.noSearchResult) {
  464. NO_SEARCH_RESULT = NO_SEARCH_RESULT.replace('NO SEARCH RESULT', localizedText.noSearchResult);
  465. }
  466. }
  467. noSearchResult = jqLite(NO_SEARCH_RESULT);
  468. dropdownContainer.append(searchBox);
  469. dropdownMenu.append(noSearchResult);
  470. }
  471. if (tAttrs.actionsBox === 'true' && isMultiple) {
  472. // set localizedText
  473. if (localizedText.selectAllTpl) {
  474. ACTIONS_BOX = ACTIONS_BOX.replace('SELECT ALL', localizedText.selectAllTpl);
  475. } else if (localizedText.selectAll) {
  476. ACTIONS_BOX = ACTIONS_BOX.replace('SELECT ALL', localizedText.selectAll);
  477. }
  478. if (localizedText.deselectAllTpl) {
  479. ACTIONS_BOX = ACTIONS_BOX.replace('DESELECT ALL', localizedText.deselectAllTpl);
  480. } else if (localizedText.selectAll) {
  481. ACTIONS_BOX = ACTIONS_BOX.replace('DESELECT ALL', localizedText.deselectAll);
  482. }
  483. actionsBox = jqLite(ACTIONS_BOX);
  484. dropdownContainer.append(actionsBox);
  485. }
  486. // set default none selection text
  487. jqLite(dropdownToggle[0].querySelector('.special-title')).append(getDefaultNoneSelectionContent());
  488. dropdownContainer.append(dropdownMenu);
  489. tElement.append(dropdownToggle);
  490. tElement.append(dropdownContainer);
  491. return function nyaBsSelectLink ($scope, $element, $attrs, ctrls) {
  492. var ngCtrl = ctrls[0],
  493. nyaBsSelectCtrl = ctrls[1],
  494. liHeight,
  495. isDisabled = false,
  496. previousTabIndex,
  497. valueExpFn,
  498. valueExpGetter = $parse(nyaBsSelectCtrl.valueExp),
  499. isMultiple = typeof $attrs.multiple !== 'undefined';
  500. // find element from current $element root. because the compiled element may be detached from DOM tree by ng-if or ng-switch.
  501. var dropdownToggle = jqLite($element[0].querySelector('.dropdown-toggle')),
  502. dropdownContainer = dropdownToggle.next(),
  503. dropdownMenu = jqLite(dropdownContainer[0].querySelector('.dropdown-menu.inner')),
  504. searchBox = jqLite(dropdownContainer[0].querySelector('.bs-searchbox')),
  505. noSearchResult = jqLite(dropdownMenu[0].querySelector('.no-search-result')),
  506. actionsBox = jqLite(dropdownContainer[0].querySelector('.bs-actionsbox'));
  507. if(nyaBsSelectCtrl.valueExp) {
  508. valueExpFn = function(scope, locals) {
  509. return valueExpGetter(scope, locals);
  510. };
  511. }
  512. // for debug
  513. nyaBsSelectCtrl.setId($element.attr('id'));
  514. if (isMultiple) {
  515. nyaBsSelectCtrl.isMultiple = true;
  516. // required validator
  517. ngCtrl.$isEmpty = function(value) {
  518. return !value || value.length === 0;
  519. };
  520. }
  521. if(typeof $attrs.disabled !== 'undefined') {
  522. $scope.$watch($attrs.disabled, function(disabled){
  523. if(disabled) {
  524. dropdownToggle.addClass('disabled');
  525. dropdownToggle.attr('disabled', 'disabled');
  526. previousTabIndex = dropdownToggle.attr('tabindex');
  527. dropdownToggle.attr('tabindex', '-1');
  528. isDisabled = true;
  529. } else {
  530. dropdownToggle.removeClass('disabled');
  531. dropdownToggle.removeAttr('disabled');
  532. if(previousTabIndex) {
  533. dropdownToggle.attr('tabindex', previousTabIndex);
  534. } else {
  535. dropdownToggle.removeAttr('tabindex');
  536. }
  537. isDisabled = false;
  538. }
  539. });
  540. }
  541. /**
  542. * Do some check on modelValue. remove no existing value
  543. * @param values
  544. * @param deepWatched
  545. */
  546. nyaBsSelectCtrl.onCollectionChange = function (values, deepWatched) {
  547. var valuesForSelect = [],
  548. index,
  549. modelValueChanged = false,
  550. // Due to ngModelController compare reference with the old modelValue, we must set an new array instead of modifying the old one.
  551. // See: https://github.com/angular/angular.js/issues/1751
  552. modelValue = deepCopy(ngCtrl.$modelValue);
  553. if(!modelValue) {
  554. return;
  555. }
  556. /**
  557. * Behavior change, since 2.1.0, we don't want to reset model to null or empty array when options' collection is not prepared.
  558. */
  559. if(Array.isArray(values) && values.length > 0) {
  560. if(valueExpFn) {
  561. for(index = 0; index < values.length; index++) {
  562. valuesForSelect.push(valueExpFn($scope, values[index]));
  563. }
  564. } else {
  565. for(index = 0; index < values.length; index++) {
  566. if(nyaBsSelectCtrl.valueIdentifier) {
  567. valuesForSelect.push(values[index][nyaBsSelectCtrl.valueIdentifier]);
  568. } else if(nyaBsSelectCtrl.keyIdentifier) {
  569. valuesForSelect.push(values[index][nyaBsSelectCtrl.keyIdentifier]);
  570. }
  571. }
  572. }
  573. if(isMultiple) {
  574. for(index = 0; index < modelValue.length; index++) {
  575. if(!contains(valuesForSelect, modelValue[index])) {
  576. modelValueChanged = true;
  577. modelValue.splice(index, 1);
  578. index--;
  579. }
  580. }
  581. if(modelValueChanged) {
  582. // modelValue changed.
  583. ngCtrl.$setViewValue(modelValue);
  584. updateButtonContent();
  585. }
  586. } else {
  587. if(!contains(valuesForSelect, modelValue)) {
  588. modelValue = valuesForSelect[0];
  589. ngCtrl.$setViewValue(modelValue);
  590. updateButtonContent();
  591. }
  592. }
  593. }
  594. /**
  595. * if we set deep-watch="true" on nyaBsOption directive,
  596. * we need to refresh dropdown button content whenever a change happened in collection.
  597. */
  598. if(deepWatched) {
  599. updateButtonContent();
  600. }
  601. };
  602. // view --> model
  603. dropdownMenu.on('click', function menuEventHandler (event) {
  604. if(isDisabled) {
  605. return;
  606. }
  607. if(jqLite(event.target).hasClass('dropdown-header')) {
  608. return;
  609. }
  610. var nyaBsOptionNode = filterTarget(event.target, dropdownMenu[0], 'nya-bs-option'),
  611. nyaBsOption;
  612. if(nyaBsOptionNode !== null) {
  613. nyaBsOption = jqLite(nyaBsOptionNode);
  614. if(nyaBsOption.hasClass('disabled')) {
  615. return;
  616. }
  617. selectOption(nyaBsOption);
  618. }
  619. });
  620. // if click the outside of dropdown menu, close the dropdown menu
  621. var outClick = function(event) {
  622. if(filterTarget(event.target, $element.parent()[0], $element[0]) === null) {
  623. if($element.hasClass('open')) {
  624. $element.triggerHandler('blur');
  625. }
  626. $element.removeClass('open');
  627. }
  628. };
  629. $document.on('click', outClick);
  630. dropdownToggle.on('blur', function() {
  631. if(!$element.hasClass('open')) {
  632. $element.triggerHandler('blur');
  633. }
  634. });
  635. dropdownToggle.on('click', function() {
  636. var nyaBsOptionNode;
  637. $element.toggleClass('open');
  638. if($element.hasClass('open') && typeof liHeight === 'undefined') {
  639. calcMenuSize();
  640. }
  641. if($attrs.liveSearch === 'true' && $element.hasClass('open')) {
  642. searchBox.children().eq(0)[0].focus();
  643. nyaBsOptionNode = findFocus(true);
  644. if(nyaBsOptionNode) {
  645. dropdownMenu.children().removeClass('active');
  646. jqLite(nyaBsOptionNode).addClass('active');
  647. }
  648. } else if($element.hasClass('open')) {
  649. nyaBsOptionNode = findFocus(true);
  650. if(nyaBsOptionNode) {
  651. setFocus(nyaBsOptionNode);
  652. }
  653. }
  654. });
  655. // actions box
  656. if ($attrs.actionsBox === 'true' && isMultiple) {
  657. actionsBox.find('button').eq(0).on('click', function () {
  658. setAllOptions(true);
  659. });
  660. actionsBox.find('button').eq(1).on('click', function () {
  661. setAllOptions(false);
  662. });
  663. }
  664. // live search
  665. if($attrs.liveSearch === 'true') {
  666. searchBox.children().on('input', function(){
  667. var searchKeyword = searchBox.children().val(),
  668. found = 0,
  669. options = dropdownMenu.children(),
  670. length = options.length,
  671. index,
  672. option,
  673. nyaBsOptionNode;
  674. if(searchKeyword) {
  675. for(index = 0; index < length; index++) {
  676. option = options.eq(index);
  677. if(option.hasClass('nya-bs-option')) {
  678. if(!hasKeyword(option.find('a'), searchKeyword)) {
  679. option.addClass('not-match');
  680. } else {
  681. option.removeClass('not-match');
  682. found++;
  683. }
  684. }
  685. }
  686. if(found === 0) {
  687. noSearchResult.addClass('show');
  688. } else {
  689. noSearchResult.removeClass('show');
  690. }
  691. } else {
  692. for(index = 0; index < length; index++) {
  693. option = options.eq(index);
  694. if(option.hasClass('nya-bs-option')) {
  695. option.removeClass('not-match');
  696. }
  697. }
  698. noSearchResult.removeClass('show');
  699. }
  700. nyaBsOptionNode = findFocus(true);
  701. if(nyaBsOptionNode) {
  702. options.removeClass('active');
  703. jqLite(nyaBsOptionNode).addClass('active');
  704. }
  705. });
  706. }
  707. // model --> view
  708. ngCtrl.$render = function() {
  709. var modelValue = ngCtrl.$modelValue,
  710. index,
  711. bsOptionElements = dropdownMenu.children(),
  712. length = bsOptionElements.length,
  713. value;
  714. if(typeof modelValue === 'undefined') {
  715. // if modelValue is undefined. uncheck all option
  716. for(index = 0; index < length; index++) {
  717. if(bsOptionElements.eq(index).hasClass('nya-bs-option')) {
  718. bsOptionElements.eq(index).removeClass('selected');
  719. }
  720. }
  721. } else {
  722. for(index = 0; index < length; index++) {
  723. if(bsOptionElements.eq(index).hasClass('nya-bs-option')) {
  724. value = getOptionValue(bsOptionElements.eq(index));
  725. if(isMultiple) {
  726. if(contains(modelValue, value)) {
  727. bsOptionElements.eq(index).addClass('selected');
  728. } else {
  729. bsOptionElements.eq(index).removeClass('selected');
  730. }
  731. } else {
  732. if(deepEquals(modelValue, value)) {
  733. bsOptionElements.eq(index).addClass('selected');
  734. } else {
  735. bsOptionElements.eq(index).removeClass('selected');
  736. }
  737. }
  738. }
  739. }
  740. }
  741. //console.log(nyaBsSelectCtrl.id + ' render end');
  742. updateButtonContent();
  743. };
  744. // simple keyboard support
  745. $element.on('keydown', function(event){
  746. var keyCode = event.keyCode;
  747. if(keyCode !== 27 && keyCode !== 13 && keyCode !== 38 && keyCode !== 40) {
  748. // we only handle special keys. don't waste time to traverse the dom tree.
  749. return;
  750. }
  751. // prevent a click event to be fired.
  752. event.preventDefault();
  753. if(isDisabled) {
  754. event.stopPropagation();
  755. return;
  756. }
  757. var toggleButton = filterTarget(event.target, $element[0], dropdownToggle[0]),
  758. menuContainer,
  759. searchBoxContainer,
  760. liElement,
  761. nyaBsOptionNode;
  762. if($attrs.liveSearch === 'true') {
  763. searchBoxContainer = filterTarget(event.target, $element[0], searchBox[0]);
  764. } else {
  765. menuContainer = filterTarget(event.target, $element[0], dropdownContainer[0])
  766. }
  767. if(toggleButton) {
  768. // press enter to active dropdown
  769. if((keyCode === 13 || keyCode === 38 || keyCode === 40) && !$element.hasClass('open')) {
  770. event.stopPropagation();
  771. $element.addClass('open');
  772. // calculate menu size
  773. if(typeof liHeight === 'undefined') {
  774. calcMenuSize();
  775. }
  776. // if live search enabled. give focus to search box.
  777. if($attrs.liveSearch === 'true') {
  778. searchBox.children().eq(0)[0].focus();
  779. // find the focusable node but we will use active
  780. nyaBsOptionNode = findFocus(true);
  781. if(nyaBsOptionNode) {
  782. // remove previous active state
  783. dropdownMenu.children().removeClass('active');
  784. // set active to first focusable element
  785. jqLite(nyaBsOptionNode).addClass('active');
  786. }
  787. } else {
  788. // otherwise, give focus to first menu item.
  789. nyaBsOptionNode = findFocus(true);
  790. if(nyaBsOptionNode) {
  791. setFocus(nyaBsOptionNode);
  792. }
  793. }
  794. }
  795. // press enter or escape to de-active dropdown
  796. //if((keyCode === 13 || keyCode === 27) && $element.hasClass('open')) {
  797. // $element.removeClass('open');
  798. // event.stopPropagation();
  799. //}
  800. } else if(menuContainer) {
  801. if(keyCode === 27) {
  802. // escape pressed
  803. dropdownToggle[0].focus();
  804. if($element.hasClass('open')) {
  805. $element.triggerHandler('blur');
  806. }
  807. $element.removeClass('open');
  808. event.stopPropagation();
  809. } else if(keyCode === 38) {
  810. event.stopPropagation();
  811. // up arrow key
  812. nyaBsOptionNode = findNextFocus(event.target.parentNode, 'previousSibling');
  813. if(nyaBsOptionNode) {
  814. setFocus(nyaBsOptionNode);
  815. } else {
  816. nyaBsOptionNode = findFocus(false);
  817. if(nyaBsOptionNode) {
  818. setFocus(nyaBsOptionNode);
  819. }
  820. }
  821. } else if(keyCode === 40) {
  822. event.stopPropagation();
  823. // down arrow key
  824. nyaBsOptionNode = findNextFocus(event.target.parentNode, 'nextSibling');
  825. if(nyaBsOptionNode) {
  826. setFocus(nyaBsOptionNode);
  827. } else {
  828. nyaBsOptionNode = findFocus(true);
  829. if(nyaBsOptionNode) {
  830. setFocus(nyaBsOptionNode);
  831. }
  832. }
  833. } else if(keyCode === 13) {
  834. event.stopPropagation();
  835. // enter pressed
  836. liElement = jqLite(event.target.parentNode);
  837. if(liElement.hasClass('nya-bs-option')) {
  838. selectOption(liElement);
  839. if(!isMultiple) {
  840. dropdownToggle[0].focus();
  841. }
  842. }
  843. }
  844. } else if(searchBoxContainer) {
  845. if(keyCode === 27) {
  846. dropdownToggle[0].focus();
  847. $element.removeClass('open');
  848. event.stopPropagation();
  849. } else if(keyCode === 38) {
  850. // up
  851. event.stopPropagation();
  852. liElement = findActive();
  853. if(liElement) {
  854. nyaBsOptionNode = findNextFocus(liElement[0], 'previousSibling');
  855. if(nyaBsOptionNode) {
  856. liElement.removeClass('active');
  857. jqLite(nyaBsOptionNode).addClass('active');
  858. } else {
  859. nyaBsOptionNode = findFocus(false);
  860. if(nyaBsOptionNode) {
  861. liElement.removeClass('active');
  862. jqLite(nyaBsOptionNode).addClass('active');
  863. }
  864. }
  865. }
  866. } else if(keyCode === 40) {
  867. // down
  868. event.stopPropagation();
  869. liElement = findActive();
  870. if(liElement) {
  871. nyaBsOptionNode = findNextFocus(liElement[0], 'nextSibling');
  872. if(nyaBsOptionNode) {
  873. liElement.removeClass('active');
  874. jqLite(nyaBsOptionNode).addClass('active');
  875. } else {
  876. nyaBsOptionNode = findFocus(true);
  877. if(nyaBsOptionNode) {
  878. liElement.removeClass('active');
  879. jqLite(nyaBsOptionNode).addClass('active');
  880. }
  881. }
  882. }
  883. } else if(keyCode === 13) {
  884. // select an option.
  885. liElement = findActive();
  886. if(liElement) {
  887. selectOption(liElement);
  888. if(!isMultiple) {
  889. dropdownToggle[0].focus();
  890. }
  891. }
  892. }
  893. }
  894. });
  895. function findActive() {
  896. var list = dropdownMenu.children(),
  897. i, liElement,
  898. length = list.length;
  899. for(i = 0; i < length; i++) {
  900. liElement = list.eq(i);
  901. if(liElement.hasClass('active') && liElement.hasClass('nya-bs-option') && !liElement.hasClass('not-match')) {
  902. return liElement;
  903. }
  904. }
  905. return null;
  906. }
  907. /**
  908. * setFocus on a nya-bs-option element. it actually set focus on its child anchor element.
  909. * @param elem a nya-bs-option element.
  910. */
  911. function setFocus(elem) {
  912. var childList = elem.childNodes,
  913. length = childList.length,
  914. child;
  915. for(var i = 0; i < length; i++) {
  916. child = childList[i];
  917. if(child.nodeType === 1 && child.tagName.toLowerCase() === 'a') {
  918. child.focus();
  919. break;
  920. }
  921. }
  922. }
  923. function findFocus(fromFirst) {
  924. var firstLiElement;
  925. if(fromFirst) {
  926. firstLiElement = dropdownMenu.children().eq(0);
  927. } else {
  928. firstLiElement = dropdownMenu.children().eq(dropdownMenu.children().length - 1);
  929. }
  930. // focus on selected element
  931. for(var i = 0; i < dropdownMenu.children().length; i++) {
  932. var childElement = dropdownMenu.children().eq(i);
  933. if (!childElement.hasClass('not-match') && childElement.hasClass('selected')) {
  934. return dropdownMenu.children().eq(i)[0];
  935. }
  936. }
  937. if(firstLiElement.hasClass('nya-bs-option') && !firstLiElement.hasClass('disabled') && !firstLiElement.hasClass('not-match')) {
  938. return firstLiElement[0];
  939. } else {
  940. if(fromFirst) {
  941. return findNextFocus(firstLiElement[0], 'nextSibling');
  942. } else {
  943. return findNextFocus(firstLiElement[0], 'previousSibling');
  944. }
  945. }
  946. }
  947. /**
  948. * find next focusable element on direction
  949. * @param from the element traversed from
  950. * @param direction can be 'nextSibling' or 'previousSibling'
  951. * @returns the element if found, otherwise return null.
  952. */
  953. function findNextFocus(from, direction) {
  954. if(from && !hasClass(from, 'nya-bs-option')) {
  955. return;
  956. }
  957. var next = from;
  958. while ((next = sibling(next, direction)) && next.nodeType) {
  959. if(hasClass(next,'nya-bs-option') && !hasClass(next, 'disabled') && !hasClass(next, 'not-match')) {
  960. return next
  961. }
  962. }
  963. return null;
  964. }
  965. /**
  966. *
  967. */
  968. function setAllOptions(selectAll) {
  969. if (!isMultiple || isDisabled)
  970. return;
  971. var liElements,
  972. wv,
  973. viewValue;
  974. liElements = dropdownMenu[0].querySelectorAll('.nya-bs-option');
  975. if (liElements.length > 0) {
  976. wv = ngCtrl.$viewValue;
  977. // make a deep copy enforce ngModelController to call its $render method.
  978. // See: https://github.com/angular/angular.js/issues/1751
  979. viewValue = Array.isArray(wv) ? deepCopy(wv) : [];
  980. for (var i = 0; i < liElements.length; i++) {
  981. var nyaBsOption = jqLite(liElements[i]);
  982. if (nyaBsOption.hasClass('disabled'))
  983. continue;
  984. var value, index;
  985. // if user specify the value attribute. we should use the value attribute
  986. // otherwise, use the valueIdentifier specified field in target scope
  987. value = getOptionValue(nyaBsOption);
  988. if (typeof value !== 'undefined') {
  989. index = indexOf(viewValue, value);
  990. if (selectAll && index == -1) {
  991. // check element
  992. viewValue.push(value);
  993. nyaBsOption.addClass('selected');
  994. } else if (!selectAll && index != -1) {
  995. // uncheck element
  996. viewValue.splice(index, 1);
  997. nyaBsOption.removeClass('selected');
  998. }
  999. }
  1000. }
  1001. // update view value regardless
  1002. ngCtrl.$setViewValue(viewValue);
  1003. $scope.$digest();
  1004. updateButtonContent();
  1005. }
  1006. }
  1007. /**
  1008. * select an option represented by nyaBsOption argument. Get the option's value and update model.
  1009. * if isMultiple = true, doesn't close dropdown menu. otherwise close the menu.
  1010. * @param nyaBsOption the jqLite wrapped `nya-bs-option` element.
  1011. */
  1012. function selectOption(nyaBsOption) {
  1013. var value,
  1014. viewValue,
  1015. wv = ngCtrl.$viewValue,
  1016. index;
  1017. // if user specify the value attribute. we should use the value attribute
  1018. // otherwise, use the valueIdentifier specified field in target scope
  1019. value = getOptionValue(nyaBsOption);
  1020. if(typeof value !== 'undefined') {
  1021. if(isMultiple) {
  1022. // make a deep copy enforce ngModelController to call its $render method.
  1023. // See: https://github.com/angular/angular.js/issues/1751
  1024. viewValue = Array.isArray(wv) ? deepCopy(wv) : [];
  1025. index = indexOf(viewValue, value);
  1026. if(index === -1) {
  1027. // check element
  1028. viewValue.push(value);
  1029. nyaBsOption.addClass('selected');
  1030. } else {
  1031. // uncheck element
  1032. viewValue.splice(index, 1);
  1033. nyaBsOption.removeClass('selected');
  1034. }
  1035. } else {
  1036. dropdownMenu.children().removeClass('selected');
  1037. viewValue = value;
  1038. nyaBsOption.addClass('selected');
  1039. }
  1040. }
  1041. // update view value regardless
  1042. ngCtrl.$setViewValue(viewValue);
  1043. $scope.$digest();
  1044. if(!isMultiple) {
  1045. // in single selection mode. close the dropdown menu
  1046. if($element.hasClass('open')) {
  1047. $element.triggerHandler('blur');
  1048. }
  1049. $element.removeClass('open');
  1050. dropdownToggle[0].focus();
  1051. }
  1052. updateButtonContent();
  1053. }
  1054. /**
  1055. * get a value of current nyaBsOption. according to different setting.
  1056. * - if `nya-bs-option` directive is used to populate options and a `value` attribute is specified. use expression of the attribute value.
  1057. * - if `nya-bs-option` directive is used to populate options and no other settings, use the valueIdentifier or keyIdentifier to retrieve value from scope of current nyaBsOption.
  1058. * - if `nya-bs-option` class is used on static options. use literal value of the `value` attribute.
  1059. * @param nyaBsOption a jqLite wrapped `nya-bs-option` element
  1060. */
  1061. function getOptionValue(nyaBsOption) {
  1062. var scopeOfOption;
  1063. if(valueExpFn) {
  1064. // here we use the scope bound by ourselves in the nya-bs-option.
  1065. scopeOfOption = nyaBsOption.data('isolateScope');
  1066. return valueExpFn(scopeOfOption);
  1067. } else {
  1068. if(nyaBsSelectCtrl.valueIdentifier || nyaBsSelectCtrl.keyIdentifier) {
  1069. scopeOfOption = nyaBsOption.data('isolateScope');
  1070. return scopeOfOption[nyaBsSelectCtrl.valueIdentifier] || scopeOfOption[nyaBsSelectCtrl.keyIdentifier];
  1071. } else {
  1072. return nyaBsOption.attr('data-value');
  1073. }
  1074. }
  1075. }
  1076. function getOptionText(nyaBsOption) {
  1077. var item = nyaBsOption.find('a');
  1078. if(item.children().length === 0 || item.children().eq(0).hasClass('check-mark')) {
  1079. // if the first child is check-mark or has no children, means the option text is text node
  1080. return item[0].firstChild.cloneNode(false);
  1081. } else {
  1082. // otherwise we clone the first element of the item
  1083. return item.children().eq(0)[0].cloneNode(true);
  1084. }
  1085. }
  1086. function updateButtonContent() {
  1087. var viewValue = ngCtrl.$viewValue;
  1088. $element.triggerHandler('change');
  1089. var filterOption = jqLite(dropdownToggle[0].querySelector('.filter-option'));
  1090. var specialTitle = jqLite(dropdownToggle[0].querySelector('.special-title'));
  1091. if(typeof viewValue === 'undefined') {
  1092. /**
  1093. * Select empty option when model is undefined.
  1094. */
  1095. dropdownToggle.addClass('show-special-title');
  1096. filterOption.empty();
  1097. return;
  1098. }
  1099. if(isMultiple && viewValue.length === 0) {
  1100. dropdownToggle.addClass('show-special-title');
  1101. filterOption.empty();
  1102. } else {
  1103. dropdownToggle.removeClass('show-special-title');
  1104. $timeout(function() {
  1105. var bsOptionElements = dropdownMenu.children(),
  1106. value,
  1107. nyaBsOption,
  1108. index,
  1109. length = bsOptionElements.length,
  1110. optionTitle,
  1111. selection = [],
  1112. match,
  1113. count;
  1114. if(isMultiple && $attrs.selectedTextFormat === 'count') {
  1115. count = 1;
  1116. } else if(isMultiple && $attrs.selectedTextFormat && (match = $attrs.selectedTextFormat.match(/\s*count\s*>\s*(\d+)\s*/))) {
  1117. count = parseInt(match[1], 10);
  1118. }
  1119. // data-selected-text-format="count" or data-selected-text-format="count>x"
  1120. if((typeof count !== 'undefined') && viewValue.length > count) {
  1121. filterOption.empty();
  1122. if(localizedText.numberItemSelectedTpl) {
  1123. filterOption.append(jqLite(localizedText.numberItemSelectedTpl.replace('%d', viewValue.length)));
  1124. } else if(localizedText.numberItemSelected) {
  1125. filterOption.append(document.createTextNode(localizedText.numberItemSelected.replace('%d', viewValue.length)));
  1126. } else {
  1127. filterOption.append(document.createTextNode(viewValue.length + ' items selected'));
  1128. }
  1129. return;
  1130. }
  1131. // data-selected-text-format="values" or the number of selected items is less than count
  1132. for(index = 0; index < length; index++) {
  1133. nyaBsOption = bsOptionElements.eq(index);
  1134. if(nyaBsOption.hasClass('nya-bs-option')) {
  1135. value = getOptionValue(nyaBsOption);
  1136. if(isMultiple) {
  1137. if(Array.isArray(viewValue) && contains(viewValue, value)) {
  1138. // if option has an title attribute. use the title value as content show in button.
  1139. // otherwise get very first child element.
  1140. optionTitle = nyaBsOption.attr('title');
  1141. if(optionTitle) {
  1142. selection.push(document.createTextNode(optionTitle));
  1143. } else {
  1144. selection.push(getOptionText(nyaBsOption));
  1145. }
  1146. }
  1147. } else {
  1148. if(deepEquals(viewValue, value)) {
  1149. optionTitle = nyaBsOption.attr('title');
  1150. if(optionTitle) {
  1151. selection.push(document.createTextNode(optionTitle));
  1152. } else {
  1153. selection.push(getOptionText(nyaBsOption));
  1154. }
  1155. }
  1156. }
  1157. }
  1158. }
  1159. if(selection.length === 0) {
  1160. filterOption.empty();
  1161. dropdownToggle.addClass('show-special-title');
  1162. } else if(selection.length === 1) {
  1163. dropdownToggle.removeClass('show-special-title');
  1164. // either single or multiple selection will show the only selected content.
  1165. filterOption.empty();
  1166. filterOption.append(selection[0]);
  1167. } else {
  1168. dropdownToggle.removeClass('show-special-title');
  1169. filterOption.empty();
  1170. for(index = 0; index < selection.length; index++) {
  1171. filterOption.append(selection[index]);
  1172. if(index < selection.length -1) {
  1173. filterOption.append(document.createTextNode(', '));
  1174. }
  1175. }
  1176. }
  1177. });
  1178. }
  1179. }
  1180. // will called only once.
  1181. function calcMenuSize(){
  1182. var liElements = dropdownMenu.find('li'),
  1183. length = liElements.length,
  1184. liElement,
  1185. i;
  1186. for(i = 0; i < length; i++) {
  1187. liElement = liElements.eq(i);
  1188. if(liElement.hasClass('nya-bs-option') || liElement.attr('nya-bs-option')) {
  1189. liHeight = liElement[0].clientHeight;
  1190. break;
  1191. }
  1192. }
  1193. if(/\d+/.test($attrs.size)) {
  1194. var dropdownSize = parseInt($attrs.size, 10);
  1195. dropdownMenu.css('max-height', (dropdownSize * liHeight) + 'px');
  1196. dropdownMenu.css('overflow-y', 'auto');
  1197. }
  1198. }
  1199. $scope.$on('$destroy', function() {
  1200. dropdownMenu.off();
  1201. dropdownToggle.off();
  1202. if (searchBox.off) searchBox.off();
  1203. $document.off('click', outClick);
  1204. });
  1205. };
  1206. }
  1207. };
  1208. }]);
  1209. nyaBsSelect.directive('nyaBsOption', ['$parse', function($parse){
  1210. //00000011111111111111100000000022222222222222200000003333333333333330000000000000004444444444000000000000000000055555555550000000000000000000006666666666000000
  1211. var BS_OPTION_REGEX = /^\s*(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+group\s+by\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/;
  1212. return {
  1213. restrict: 'A',
  1214. transclude: 'element',
  1215. priority: 1000,
  1216. terminal: true,
  1217. require: ['^nyaBsSelect', '^ngModel'],
  1218. compile: function nyaBsOptionCompile (tElement, tAttrs) {
  1219. var expression = tAttrs.nyaBsOption;
  1220. var nyaBsOptionEndComment = document.createComment(' end nyaBsOption: ' + expression + ' ');
  1221. var match = expression.match(BS_OPTION_REGEX);
  1222. if(!match) {
  1223. throw new Error('invalid expression');
  1224. }
  1225. // we want to keep our expression comprehensible so we don't use 'select as label for value in collection' expression.
  1226. var valueExp = tAttrs.value,
  1227. valueExpGetter = valueExp ? $parse(valueExp) : null;
  1228. var valueIdentifier = match[3] || match[1],
  1229. keyIdentifier = match[2],
  1230. collectionExp = match[4],
  1231. groupByExpGetter = match[5] ? $parse(match[5]) : null,
  1232. trackByExp = match[6];
  1233. var trackByIdArrayFn,
  1234. trackByIdObjFn,
  1235. trackByIdExpFn,
  1236. trackByExpGetter;
  1237. var hashFnLocals = {$id: hashKey};
  1238. var groupByFn, locals = {};
  1239. if(trackByExp) {
  1240. trackByExpGetter = $parse(trackByExp);
  1241. } else {
  1242. trackByIdArrayFn = function(key, value) {
  1243. return hashKey(value);
  1244. };
  1245. trackByIdObjFn = function(key) {
  1246. return key;
  1247. };
  1248. }
  1249. return function nyaBsOptionLink($scope, $element, $attr, ctrls, $transclude) {
  1250. var nyaBsSelectCtrl = ctrls[0],
  1251. ngCtrl = ctrls[1],
  1252. valueExpFn,
  1253. deepWatched,
  1254. valueExpLocals = {};
  1255. if(trackByExpGetter) {
  1256. trackByIdExpFn = function(key, value, index) {
  1257. // assign key, value, and $index to the locals so that they can be used in hash functions
  1258. if (keyIdentifier) {
  1259. hashFnLocals[keyIdentifier] = key;
  1260. }
  1261. hashFnLocals[valueIdentifier] = value;
  1262. hashFnLocals.$index = index;
  1263. return trackByExpGetter($scope, hashFnLocals);
  1264. };
  1265. }
  1266. if(groupByExpGetter) {
  1267. groupByFn = function(key, value) {
  1268. if(keyIdentifier) {
  1269. locals[keyIdentifier] = key;
  1270. }
  1271. locals[valueIdentifier] = value;
  1272. return groupByExpGetter($scope, locals);
  1273. }
  1274. }
  1275. // set keyIdentifier and valueIdentifier property of nyaBsSelectCtrl
  1276. if(keyIdentifier) {
  1277. nyaBsSelectCtrl.keyIdentifier = keyIdentifier;
  1278. }
  1279. if(valueIdentifier) {
  1280. nyaBsSelectCtrl.valueIdentifier = valueIdentifier;
  1281. }
  1282. if(valueExpGetter) {
  1283. nyaBsSelectCtrl.valueExp = valueExp;
  1284. valueExpFn = function(key, value) {
  1285. if(keyIdentifier) {
  1286. valueExpLocals[keyIdentifier] = key;
  1287. }
  1288. valueExpLocals[valueIdentifier] = value;
  1289. return valueExpGetter($scope, valueExpLocals);
  1290. }
  1291. }
  1292. // Store a list of elements from previous run. This is a hash where key is the item from the
  1293. // iterator, and the value is objects with following properties.
  1294. // - scope: bound scope
  1295. // - element: previous element.
  1296. // - index: position
  1297. //
  1298. // We are using no-proto object so that we don't need to guard against inherited props via
  1299. // hasOwnProperty.
  1300. var lastBlockMap = createMap();
  1301. // deepWatch will impact performance. use with caution.
  1302. if($attr.deepWatch === 'true') {
  1303. deepWatched = true;
  1304. $scope.$watch(collectionExp, nyaBsOptionAction, true);
  1305. } else {
  1306. deepWatched = false;
  1307. $scope.$watchCollection(collectionExp, nyaBsOptionAction);
  1308. }
  1309. function nyaBsOptionAction(collection) {
  1310. var index,
  1311. previousNode = $element[0], // node that cloned nodes should be inserted after
  1312. // initialized to the comment node anchor
  1313. key, value,
  1314. trackById,
  1315. trackByIdFn,
  1316. collectionKeys,
  1317. collectionLength,
  1318. // Same as lastBlockMap but it has the current state. It will become the
  1319. // lastBlockMap on the next iteration.
  1320. nextBlockMap = createMap(),
  1321. nextBlockOrder,
  1322. block,
  1323. groupName,
  1324. nextNode,
  1325. group,
  1326. lastGroup,
  1327. removedClone, // removed clone node, should also remove isolateScope data as well
  1328. values = [],
  1329. valueObj; // the collection value
  1330. if(groupByFn) {
  1331. group = [];
  1332. }
  1333. if(isArrayLike(collection)) {
  1334. collectionKeys = collection;
  1335. trackByIdFn = trackByIdExpFn || trackByIdArrayFn;
  1336. } else {
  1337. trackByIdFn = trackByIdExpFn || trackByIdObjFn;
  1338. // if object, extract keys, sort them and use to determine order of iteration over obj props
  1339. collectionKeys = [];
  1340. for (var itemKey in collection) {
  1341. if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) != '$') {
  1342. collectionKeys.push(itemKey);
  1343. }
  1344. }
  1345. collectionKeys.sort();
  1346. }
  1347. collectionLength = collectionKeys.length;
  1348. nextBlockOrder = new Array(collectionLength);
  1349. for(index = 0; index < collectionLength; index++) {
  1350. key = (collection === collectionKeys) ? index : collectionKeys[index];
  1351. value = collection[key];
  1352. trackById = trackByIdFn(key, value, index);
  1353. // copy the value with scope like structure to notify the select directive.
  1354. valueObj = {};
  1355. if(keyIdentifier) {
  1356. valueObj[keyIdentifier] = key;
  1357. }
  1358. valueObj[valueIdentifier] = value;
  1359. values.push(valueObj);
  1360. if(groupByFn) {
  1361. groupName = groupByFn(key, value);
  1362. if(group.indexOf(groupName) === -1 && groupName) {
  1363. group.push(groupName);
  1364. }
  1365. }
  1366. if(lastBlockMap[trackById]) {
  1367. // found previously seen block
  1368. block = lastBlockMap[trackById];
  1369. delete lastBlockMap[trackById];
  1370. // must update block here because some data we stored may change.
  1371. if(groupByFn) {
  1372. block.group = groupName;
  1373. }
  1374. block.key = key;
  1375. block.value = value;
  1376. nextBlockMap[trackById] = block;
  1377. nextBlockOrder[index] = block;
  1378. } else if(nextBlockMap[trackById]) {
  1379. //if collision detected. restore lastBlockMap and throw an error
  1380. nextBlockOrder.forEach(function(block) {
  1381. if(block && block.scope) {
  1382. lastBlockMap[block.id] = block;
  1383. }
  1384. });
  1385. throw new Error("Duplicates in a select are not allowed. Use 'track by' expression to specify unique keys.");
  1386. } else {
  1387. // new never before seen block
  1388. nextBlockOrder[index] = {id: trackById, scope: undefined, clone: undefined, key: key, value: value};
  1389. nextBlockMap[trackById] = true;
  1390. if(groupName) {
  1391. nextBlockOrder[index].group = groupName;
  1392. }
  1393. }
  1394. }
  1395. // only resort nextBlockOrder when group found
  1396. if(group && group.length > 0) {
  1397. nextBlockOrder = sortByGroup(nextBlockOrder, group, 'group');
  1398. }
  1399. // remove DOM nodes
  1400. for( var blockKey in lastBlockMap) {
  1401. block = lastBlockMap[blockKey];
  1402. removedClone = getBlockNodes(block.clone);
  1403. // remove the isolateScope data to detach scope from this clone
  1404. removedClone.removeData('isolateScope');
  1405. removedClone.remove();
  1406. block.scope.$destroy();
  1407. }
  1408. for(index = 0; index < collectionLength; index++) {
  1409. block = nextBlockOrder[index];
  1410. if(block.scope) {
  1411. // if we have already seen this object, then we need to reuse the
  1412. // associated scope/element
  1413. nextNode = previousNode;
  1414. if(getBlockStart(block) != nextNode) {
  1415. jqLite(previousNode).after(block.clone);
  1416. }
  1417. previousNode = getBlockEnd(block);
  1418. updateScope(block.scope, index, valueIdentifier, block.value, keyIdentifier, block.key, collectionLength, block.group);
  1419. } else {
  1420. $transclude(function nyaBsOptionTransclude(clone, scope) {
  1421. // in case of the debugInfoEnable is set to false, we have to bind the scope to the clone node.
  1422. setElementIsolateScope(clone, scope);
  1423. block.scope = scope;
  1424. var endNode = nyaBsOptionEndComment.cloneNode(false);
  1425. clone[clone.length++] = endNode;
  1426. jqLite(previousNode).after(clone);
  1427. // add nya-bs-option class
  1428. clone.addClass('nya-bs-option');
  1429. // for newly created item we need to ensure its selected status from the model value.
  1430. if(valueExpFn) {
  1431. value = valueExpFn(block.key, block.value);
  1432. } else {
  1433. value = block.value || block.key;
  1434. }
  1435. if(nyaBsSelectCtrl.isMultiple) {
  1436. if(Array.isArray(ngCtrl.$modelValue) && contains(ngCtrl.$modelValue, value)) {
  1437. clone.addClass('selected');
  1438. }
  1439. } else {
  1440. if(deepEquals(value, ngCtrl.$modelValue)) {
  1441. clone.addClass('selected');
  1442. }
  1443. }
  1444. previousNode = endNode;
  1445. // Note: We only need the first/last node of the cloned nodes.
  1446. // However, we need to keep the reference to the jqlite wrapper as it might be changed later
  1447. // by a directive with templateUrl when its template arrives.
  1448. block.clone = clone;
  1449. nextBlockMap[block.id] = block;
  1450. updateScope(block.scope, index, valueIdentifier, block.value, keyIdentifier, block.key, collectionLength, block.group);
  1451. });
  1452. }
  1453. // we need to mark the first item of a group
  1454. if(group) {
  1455. if(!lastGroup || lastGroup !== block.group) {
  1456. block.clone.addClass('first-in-group');
  1457. } else {
  1458. block.clone.removeClass('first-in-group');
  1459. }
  1460. lastGroup = block.group;
  1461. // add special class for indent
  1462. block.clone.addClass('group-item');
  1463. }
  1464. }
  1465. lastBlockMap = nextBlockMap;
  1466. nyaBsSelectCtrl.onCollectionChange(values, deepWatched);
  1467. }
  1468. };
  1469. }
  1470. }
  1471. }]);
  1472. })();