dropdown.js 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955
  1. /*!
  2. * # Semantic UI 2.4.2 - Dropdown
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. 'use strict';
  12. window = (typeof window != 'undefined' && window.Math == Math)
  13. ? window
  14. : (typeof self != 'undefined' && self.Math == Math)
  15. ? self
  16. : Function('return this')()
  17. ;
  18. $.fn.dropdown = function(parameters) {
  19. var
  20. $allModules = $(this),
  21. $document = $(document),
  22. moduleSelector = $allModules.selector || '',
  23. hasTouch = ('ontouchstart' in document.documentElement),
  24. time = new Date().getTime(),
  25. performance = [],
  26. query = arguments[0],
  27. methodInvoked = (typeof query == 'string'),
  28. queryArguments = [].slice.call(arguments, 1),
  29. returnedValue
  30. ;
  31. $allModules
  32. .each(function(elementIndex) {
  33. var
  34. settings = ( $.isPlainObject(parameters) )
  35. ? $.extend(true, {}, $.fn.dropdown.settings, parameters)
  36. : $.extend({}, $.fn.dropdown.settings),
  37. className = settings.className,
  38. message = settings.message,
  39. fields = settings.fields,
  40. keys = settings.keys,
  41. metadata = settings.metadata,
  42. namespace = settings.namespace,
  43. regExp = settings.regExp,
  44. selector = settings.selector,
  45. error = settings.error,
  46. templates = settings.templates,
  47. eventNamespace = '.' + namespace,
  48. moduleNamespace = 'module-' + namespace,
  49. $module = $(this),
  50. $context = $(settings.context),
  51. $text = $module.find(selector.text),
  52. $search = $module.find(selector.search),
  53. $sizer = $module.find(selector.sizer),
  54. $input = $module.find(selector.input),
  55. $icon = $module.find(selector.icon),
  56. $combo = ($module.prev().find(selector.text).length > 0)
  57. ? $module.prev().find(selector.text)
  58. : $module.prev(),
  59. $menu = $module.children(selector.menu),
  60. $item = $menu.find(selector.item),
  61. activated = false,
  62. itemActivated = false,
  63. internalChange = false,
  64. element = this,
  65. instance = $module.data(moduleNamespace),
  66. initialLoad,
  67. pageLostFocus,
  68. willRefocus,
  69. elementNamespace,
  70. id,
  71. selectObserver,
  72. menuObserver,
  73. module
  74. ;
  75. module = {
  76. initialize: function() {
  77. module.debug('Initializing dropdown', settings);
  78. if( module.is.alreadySetup() ) {
  79. module.setup.reference();
  80. }
  81. else {
  82. module.setup.layout();
  83. if(settings.values) {
  84. module.change.values(settings.values);
  85. }
  86. module.refreshData();
  87. module.save.defaults();
  88. module.restore.selected();
  89. module.create.id();
  90. module.bind.events();
  91. module.observeChanges();
  92. module.instantiate();
  93. }
  94. },
  95. instantiate: function() {
  96. module.verbose('Storing instance of dropdown', module);
  97. instance = module;
  98. $module
  99. .data(moduleNamespace, module)
  100. ;
  101. },
  102. destroy: function() {
  103. module.verbose('Destroying previous dropdown', $module);
  104. module.remove.tabbable();
  105. $module
  106. .off(eventNamespace)
  107. .removeData(moduleNamespace)
  108. ;
  109. $menu
  110. .off(eventNamespace)
  111. ;
  112. $document
  113. .off(elementNamespace)
  114. ;
  115. module.disconnect.menuObserver();
  116. module.disconnect.selectObserver();
  117. },
  118. observeChanges: function() {
  119. if('MutationObserver' in window) {
  120. selectObserver = new MutationObserver(module.event.select.mutation);
  121. menuObserver = new MutationObserver(module.event.menu.mutation);
  122. module.debug('Setting up mutation observer', selectObserver, menuObserver);
  123. module.observe.select();
  124. module.observe.menu();
  125. }
  126. },
  127. disconnect: {
  128. menuObserver: function() {
  129. if(menuObserver) {
  130. menuObserver.disconnect();
  131. }
  132. },
  133. selectObserver: function() {
  134. if(selectObserver) {
  135. selectObserver.disconnect();
  136. }
  137. }
  138. },
  139. observe: {
  140. select: function() {
  141. if(module.has.input()) {
  142. selectObserver.observe($module[0], {
  143. childList : true,
  144. subtree : true
  145. });
  146. }
  147. },
  148. menu: function() {
  149. if(module.has.menu()) {
  150. menuObserver.observe($menu[0], {
  151. childList : true,
  152. subtree : true
  153. });
  154. }
  155. }
  156. },
  157. create: {
  158. id: function() {
  159. id = (Math.random().toString(16) + '000000000').substr(2, 8);
  160. elementNamespace = '.' + id;
  161. module.verbose('Creating unique id for element', id);
  162. },
  163. userChoice: function(values) {
  164. var
  165. $userChoices,
  166. $userChoice,
  167. isUserValue,
  168. html
  169. ;
  170. values = values || module.get.userValues();
  171. if(!values) {
  172. return false;
  173. }
  174. values = $.isArray(values)
  175. ? values
  176. : [values]
  177. ;
  178. $.each(values, function(index, value) {
  179. if(module.get.item(value) === false) {
  180. html = settings.templates.addition( module.add.variables(message.addResult, value) );
  181. $userChoice = $('<div />')
  182. .html(html)
  183. .attr('data-' + metadata.value, value)
  184. .attr('data-' + metadata.text, value)
  185. .addClass(className.addition)
  186. .addClass(className.item)
  187. ;
  188. if(settings.hideAdditions) {
  189. $userChoice.addClass(className.hidden);
  190. }
  191. $userChoices = ($userChoices === undefined)
  192. ? $userChoice
  193. : $userChoices.add($userChoice)
  194. ;
  195. module.verbose('Creating user choices for value', value, $userChoice);
  196. }
  197. });
  198. return $userChoices;
  199. },
  200. userLabels: function(value) {
  201. var
  202. userValues = module.get.userValues()
  203. ;
  204. if(userValues) {
  205. module.debug('Adding user labels', userValues);
  206. $.each(userValues, function(index, value) {
  207. module.verbose('Adding custom user value');
  208. module.add.label(value, value);
  209. });
  210. }
  211. },
  212. menu: function() {
  213. $menu = $('<div />')
  214. .addClass(className.menu)
  215. .appendTo($module)
  216. ;
  217. },
  218. sizer: function() {
  219. $sizer = $('<span />')
  220. .addClass(className.sizer)
  221. .insertAfter($search)
  222. ;
  223. }
  224. },
  225. search: function(query) {
  226. query = (query !== undefined)
  227. ? query
  228. : module.get.query()
  229. ;
  230. module.verbose('Searching for query', query);
  231. if(module.has.minCharacters(query)) {
  232. module.filter(query);
  233. }
  234. else {
  235. module.hide();
  236. }
  237. },
  238. select: {
  239. firstUnfiltered: function() {
  240. module.verbose('Selecting first non-filtered element');
  241. module.remove.selectedItem();
  242. $item
  243. .not(selector.unselectable)
  244. .not(selector.addition + selector.hidden)
  245. .eq(0)
  246. .addClass(className.selected)
  247. ;
  248. },
  249. nextAvailable: function($selected) {
  250. $selected = $selected.eq(0);
  251. var
  252. $nextAvailable = $selected.nextAll(selector.item).not(selector.unselectable).eq(0),
  253. $prevAvailable = $selected.prevAll(selector.item).not(selector.unselectable).eq(0),
  254. hasNext = ($nextAvailable.length > 0)
  255. ;
  256. if(hasNext) {
  257. module.verbose('Moving selection to', $nextAvailable);
  258. $nextAvailable.addClass(className.selected);
  259. }
  260. else {
  261. module.verbose('Moving selection to', $prevAvailable);
  262. $prevAvailable.addClass(className.selected);
  263. }
  264. }
  265. },
  266. setup: {
  267. api: function() {
  268. var
  269. apiSettings = {
  270. debug : settings.debug,
  271. urlData : {
  272. value : module.get.value(),
  273. query : module.get.query()
  274. },
  275. on : false
  276. }
  277. ;
  278. module.verbose('First request, initializing API');
  279. $module
  280. .api(apiSettings)
  281. ;
  282. },
  283. layout: function() {
  284. if( $module.is('select') ) {
  285. module.setup.select();
  286. module.setup.returnedObject();
  287. }
  288. if( !module.has.menu() ) {
  289. module.create.menu();
  290. }
  291. if( module.is.search() && !module.has.search() ) {
  292. module.verbose('Adding search input');
  293. $search = $('<input />')
  294. .addClass(className.search)
  295. .prop('autocomplete', 'off')
  296. .insertBefore($text)
  297. ;
  298. }
  299. if( module.is.multiple() && module.is.searchSelection() && !module.has.sizer()) {
  300. module.create.sizer();
  301. }
  302. if(settings.allowTab) {
  303. module.set.tabbable();
  304. }
  305. },
  306. select: function() {
  307. var
  308. selectValues = module.get.selectValues()
  309. ;
  310. module.debug('Dropdown initialized on a select', selectValues);
  311. if( $module.is('select') ) {
  312. $input = $module;
  313. }
  314. // see if select is placed correctly already
  315. if($input.parent(selector.dropdown).length > 0) {
  316. module.debug('UI dropdown already exists. Creating dropdown menu only');
  317. $module = $input.closest(selector.dropdown);
  318. if( !module.has.menu() ) {
  319. module.create.menu();
  320. }
  321. $menu = $module.children(selector.menu);
  322. module.setup.menu(selectValues);
  323. }
  324. else {
  325. module.debug('Creating entire dropdown from select');
  326. $module = $('<div />')
  327. .attr('class', $input.attr('class') )
  328. .addClass(className.selection)
  329. .addClass(className.dropdown)
  330. .html( templates.dropdown(selectValues) )
  331. .insertBefore($input)
  332. ;
  333. if($input.hasClass(className.multiple) && $input.prop('multiple') === false) {
  334. module.error(error.missingMultiple);
  335. $input.prop('multiple', true);
  336. }
  337. if($input.is('[multiple]')) {
  338. module.set.multiple();
  339. }
  340. if ($input.prop('disabled')) {
  341. module.debug('Disabling dropdown');
  342. $module.addClass(className.disabled);
  343. }
  344. $input
  345. .removeAttr('class')
  346. .detach()
  347. .prependTo($module)
  348. ;
  349. }
  350. module.refresh();
  351. },
  352. menu: function(values) {
  353. $menu.html( templates.menu(values, fields));
  354. $item = $menu.find(selector.item);
  355. },
  356. reference: function() {
  357. module.debug('Dropdown behavior was called on select, replacing with closest dropdown');
  358. // replace module reference
  359. $module = $module.parent(selector.dropdown);
  360. instance = $module.data(moduleNamespace);
  361. element = $module.get(0);
  362. module.refresh();
  363. module.setup.returnedObject();
  364. },
  365. returnedObject: function() {
  366. var
  367. $firstModules = $allModules.slice(0, elementIndex),
  368. $lastModules = $allModules.slice(elementIndex + 1)
  369. ;
  370. // adjust all modules to use correct reference
  371. $allModules = $firstModules.add($module).add($lastModules);
  372. }
  373. },
  374. refresh: function() {
  375. module.refreshSelectors();
  376. module.refreshData();
  377. },
  378. refreshItems: function() {
  379. $item = $menu.find(selector.item);
  380. },
  381. refreshSelectors: function() {
  382. module.verbose('Refreshing selector cache');
  383. $text = $module.find(selector.text);
  384. $search = $module.find(selector.search);
  385. $input = $module.find(selector.input);
  386. $icon = $module.find(selector.icon);
  387. $combo = ($module.prev().find(selector.text).length > 0)
  388. ? $module.prev().find(selector.text)
  389. : $module.prev()
  390. ;
  391. $menu = $module.children(selector.menu);
  392. $item = $menu.find(selector.item);
  393. },
  394. refreshData: function() {
  395. module.verbose('Refreshing cached metadata');
  396. $item
  397. .removeData(metadata.text)
  398. .removeData(metadata.value)
  399. ;
  400. },
  401. clearData: function() {
  402. module.verbose('Clearing metadata');
  403. $item
  404. .removeData(metadata.text)
  405. .removeData(metadata.value)
  406. ;
  407. $module
  408. .removeData(metadata.defaultText)
  409. .removeData(metadata.defaultValue)
  410. .removeData(metadata.placeholderText)
  411. ;
  412. },
  413. toggle: function() {
  414. module.verbose('Toggling menu visibility');
  415. if( !module.is.active() ) {
  416. module.show();
  417. }
  418. else {
  419. module.hide();
  420. }
  421. },
  422. show: function(callback) {
  423. callback = $.isFunction(callback)
  424. ? callback
  425. : function(){}
  426. ;
  427. if(!module.can.show() && module.is.remote()) {
  428. module.debug('No API results retrieved, searching before show');
  429. module.queryRemote(module.get.query(), module.show);
  430. }
  431. if( module.can.show() && !module.is.active() ) {
  432. module.debug('Showing dropdown');
  433. if(module.has.message() && !(module.has.maxSelections() || module.has.allResultsFiltered()) ) {
  434. module.remove.message();
  435. }
  436. if(module.is.allFiltered()) {
  437. return true;
  438. }
  439. if(settings.onShow.call(element) !== false) {
  440. module.animate.show(function() {
  441. if( module.can.click() ) {
  442. module.bind.intent();
  443. }
  444. if(module.has.menuSearch()) {
  445. module.focusSearch();
  446. }
  447. module.set.visible();
  448. callback.call(element);
  449. });
  450. }
  451. }
  452. },
  453. hide: function(callback) {
  454. callback = $.isFunction(callback)
  455. ? callback
  456. : function(){}
  457. ;
  458. if( module.is.active() && !module.is.animatingOutward() ) {
  459. module.debug('Hiding dropdown');
  460. if(settings.onHide.call(element) !== false) {
  461. module.animate.hide(function() {
  462. module.remove.visible();
  463. callback.call(element);
  464. });
  465. }
  466. }
  467. },
  468. hideOthers: function() {
  469. module.verbose('Finding other dropdowns to hide');
  470. $allModules
  471. .not($module)
  472. .has(selector.menu + '.' + className.visible)
  473. .dropdown('hide')
  474. ;
  475. },
  476. hideMenu: function() {
  477. module.verbose('Hiding menu instantaneously');
  478. module.remove.active();
  479. module.remove.visible();
  480. $menu.transition('hide');
  481. },
  482. hideSubMenus: function() {
  483. var
  484. $subMenus = $menu.children(selector.item).find(selector.menu)
  485. ;
  486. module.verbose('Hiding sub menus', $subMenus);
  487. $subMenus.transition('hide');
  488. },
  489. bind: {
  490. events: function() {
  491. if(hasTouch) {
  492. module.bind.touchEvents();
  493. }
  494. module.bind.keyboardEvents();
  495. module.bind.inputEvents();
  496. module.bind.mouseEvents();
  497. },
  498. touchEvents: function() {
  499. module.debug('Touch device detected binding additional touch events');
  500. if( module.is.searchSelection() ) {
  501. // do nothing special yet
  502. }
  503. else if( module.is.single() ) {
  504. $module
  505. .on('touchstart' + eventNamespace, module.event.test.toggle)
  506. ;
  507. }
  508. $menu
  509. .on('touchstart' + eventNamespace, selector.item, module.event.item.mouseenter)
  510. ;
  511. },
  512. keyboardEvents: function() {
  513. module.verbose('Binding keyboard events');
  514. $module
  515. .on('keydown' + eventNamespace, module.event.keydown)
  516. ;
  517. if( module.has.search() ) {
  518. $module
  519. .on(module.get.inputEvent() + eventNamespace, selector.search, module.event.input)
  520. ;
  521. }
  522. if( module.is.multiple() ) {
  523. $document
  524. .on('keydown' + elementNamespace, module.event.document.keydown)
  525. ;
  526. }
  527. },
  528. inputEvents: function() {
  529. module.verbose('Binding input change events');
  530. $module
  531. .on('change' + eventNamespace, selector.input, module.event.change)
  532. ;
  533. },
  534. mouseEvents: function() {
  535. module.verbose('Binding mouse events');
  536. if(module.is.multiple()) {
  537. $module
  538. .on('click' + eventNamespace, selector.label, module.event.label.click)
  539. .on('click' + eventNamespace, selector.remove, module.event.remove.click)
  540. ;
  541. }
  542. if( module.is.searchSelection() ) {
  543. $module
  544. .on('mousedown' + eventNamespace, module.event.mousedown)
  545. .on('mouseup' + eventNamespace, module.event.mouseup)
  546. .on('mousedown' + eventNamespace, selector.menu, module.event.menu.mousedown)
  547. .on('mouseup' + eventNamespace, selector.menu, module.event.menu.mouseup)
  548. .on('click' + eventNamespace, selector.icon, module.event.icon.click)
  549. .on('focus' + eventNamespace, selector.search, module.event.search.focus)
  550. .on('click' + eventNamespace, selector.search, module.event.search.focus)
  551. .on('blur' + eventNamespace, selector.search, module.event.search.blur)
  552. .on('click' + eventNamespace, selector.text, module.event.text.focus)
  553. ;
  554. if(module.is.multiple()) {
  555. $module
  556. .on('click' + eventNamespace, module.event.click)
  557. ;
  558. }
  559. }
  560. else {
  561. if(settings.on == 'click') {
  562. $module
  563. .on('click' + eventNamespace, module.event.test.toggle)
  564. ;
  565. }
  566. else if(settings.on == 'hover') {
  567. $module
  568. .on('mouseenter' + eventNamespace, module.delay.show)
  569. .on('mouseleave' + eventNamespace, module.delay.hide)
  570. ;
  571. }
  572. else {
  573. $module
  574. .on(settings.on + eventNamespace, module.toggle)
  575. ;
  576. }
  577. $module
  578. .on('click' + eventNamespace, selector.icon, module.event.icon.click)
  579. .on('mousedown' + eventNamespace, module.event.mousedown)
  580. .on('mouseup' + eventNamespace, module.event.mouseup)
  581. .on('focus' + eventNamespace, module.event.focus)
  582. ;
  583. if(module.has.menuSearch() ) {
  584. $module
  585. .on('blur' + eventNamespace, selector.search, module.event.search.blur)
  586. ;
  587. }
  588. else {
  589. $module
  590. .on('blur' + eventNamespace, module.event.blur)
  591. ;
  592. }
  593. }
  594. $menu
  595. .on('mouseenter' + eventNamespace, selector.item, module.event.item.mouseenter)
  596. .on('mouseleave' + eventNamespace, selector.item, module.event.item.mouseleave)
  597. .on('click' + eventNamespace, selector.item, module.event.item.click)
  598. ;
  599. },
  600. intent: function() {
  601. module.verbose('Binding hide intent event to document');
  602. if(hasTouch) {
  603. $document
  604. .on('touchstart' + elementNamespace, module.event.test.touch)
  605. .on('touchmove' + elementNamespace, module.event.test.touch)
  606. ;
  607. }
  608. $document
  609. .on('click' + elementNamespace, module.event.test.hide)
  610. ;
  611. }
  612. },
  613. unbind: {
  614. intent: function() {
  615. module.verbose('Removing hide intent event from document');
  616. if(hasTouch) {
  617. $document
  618. .off('touchstart' + elementNamespace)
  619. .off('touchmove' + elementNamespace)
  620. ;
  621. }
  622. $document
  623. .off('click' + elementNamespace)
  624. ;
  625. }
  626. },
  627. filter: function(query) {
  628. var
  629. searchTerm = (query !== undefined)
  630. ? query
  631. : module.get.query(),
  632. afterFiltered = function() {
  633. if(module.is.multiple()) {
  634. module.filterActive();
  635. }
  636. if(query || (!query && module.get.activeItem().length == 0)) {
  637. module.select.firstUnfiltered();
  638. }
  639. if( module.has.allResultsFiltered() ) {
  640. if( settings.onNoResults.call(element, searchTerm) ) {
  641. if(settings.allowAdditions) {
  642. if(settings.hideAdditions) {
  643. module.verbose('User addition with no menu, setting empty style');
  644. module.set.empty();
  645. module.hideMenu();
  646. }
  647. }
  648. else {
  649. module.verbose('All items filtered, showing message', searchTerm);
  650. module.add.message(message.noResults);
  651. }
  652. }
  653. else {
  654. module.verbose('All items filtered, hiding dropdown', searchTerm);
  655. module.hideMenu();
  656. }
  657. }
  658. else {
  659. module.remove.empty();
  660. module.remove.message();
  661. }
  662. if(settings.allowAdditions) {
  663. module.add.userSuggestion(query);
  664. }
  665. if(module.is.searchSelection() && module.can.show() && module.is.focusedOnSearch() ) {
  666. module.show();
  667. }
  668. }
  669. ;
  670. if(settings.useLabels && module.has.maxSelections()) {
  671. return;
  672. }
  673. if(settings.apiSettings) {
  674. if( module.can.useAPI() ) {
  675. module.queryRemote(searchTerm, function() {
  676. if(settings.filterRemoteData) {
  677. module.filterItems(searchTerm);
  678. }
  679. afterFiltered();
  680. });
  681. }
  682. else {
  683. module.error(error.noAPI);
  684. }
  685. }
  686. else {
  687. module.filterItems(searchTerm);
  688. afterFiltered();
  689. }
  690. },
  691. queryRemote: function(query, callback) {
  692. var
  693. apiSettings = {
  694. errorDuration : false,
  695. cache : 'local',
  696. throttle : settings.throttle,
  697. urlData : {
  698. query: query
  699. },
  700. onError: function() {
  701. module.add.message(message.serverError);
  702. callback();
  703. },
  704. onFailure: function() {
  705. module.add.message(message.serverError);
  706. callback();
  707. },
  708. onSuccess : function(response) {
  709. var
  710. values = response[fields.remoteValues],
  711. hasRemoteValues = ($.isArray(values) && values.length > 0)
  712. ;
  713. if(hasRemoteValues) {
  714. module.remove.message();
  715. module.setup.menu({
  716. values: response[fields.remoteValues]
  717. });
  718. }
  719. else {
  720. module.add.message(message.noResults);
  721. }
  722. callback();
  723. }
  724. }
  725. ;
  726. if( !$module.api('get request') ) {
  727. module.setup.api();
  728. }
  729. apiSettings = $.extend(true, {}, apiSettings, settings.apiSettings);
  730. $module
  731. .api('setting', apiSettings)
  732. .api('query')
  733. ;
  734. },
  735. filterItems: function(query) {
  736. var
  737. searchTerm = (query !== undefined)
  738. ? query
  739. : module.get.query(),
  740. results = null,
  741. escapedTerm = module.escape.string(searchTerm),
  742. beginsWithRegExp = new RegExp('^' + escapedTerm, 'igm')
  743. ;
  744. // avoid loop if we're matching nothing
  745. if( module.has.query() ) {
  746. results = [];
  747. module.verbose('Searching for matching values', searchTerm);
  748. $item
  749. .each(function(){
  750. var
  751. $choice = $(this),
  752. text,
  753. value
  754. ;
  755. if(settings.match == 'both' || settings.match == 'text') {
  756. text = String(module.get.choiceText($choice, false));
  757. if(text.search(beginsWithRegExp) !== -1) {
  758. results.push(this);
  759. return true;
  760. }
  761. else if (settings.fullTextSearch === 'exact' && module.exactSearch(searchTerm, text)) {
  762. results.push(this);
  763. return true;
  764. }
  765. else if (settings.fullTextSearch === true && module.fuzzySearch(searchTerm, text)) {
  766. results.push(this);
  767. return true;
  768. }
  769. }
  770. if(settings.match == 'both' || settings.match == 'value') {
  771. value = String(module.get.choiceValue($choice, text));
  772. if(value.search(beginsWithRegExp) !== -1) {
  773. results.push(this);
  774. return true;
  775. }
  776. else if (settings.fullTextSearch === 'exact' && module.exactSearch(searchTerm, value)) {
  777. results.push(this);
  778. return true;
  779. }
  780. else if (settings.fullTextSearch === true && module.fuzzySearch(searchTerm, value)) {
  781. results.push(this);
  782. return true;
  783. }
  784. }
  785. })
  786. ;
  787. }
  788. module.debug('Showing only matched items', searchTerm);
  789. module.remove.filteredItem();
  790. if(results) {
  791. $item
  792. .not(results)
  793. .addClass(className.filtered)
  794. ;
  795. }
  796. },
  797. fuzzySearch: function(query, term) {
  798. var
  799. termLength = term.length,
  800. queryLength = query.length
  801. ;
  802. query = query.toLowerCase();
  803. term = term.toLowerCase();
  804. if(queryLength > termLength) {
  805. return false;
  806. }
  807. if(queryLength === termLength) {
  808. return (query === term);
  809. }
  810. search: for (var characterIndex = 0, nextCharacterIndex = 0; characterIndex < queryLength; characterIndex++) {
  811. var
  812. queryCharacter = query.charCodeAt(characterIndex)
  813. ;
  814. while(nextCharacterIndex < termLength) {
  815. if(term.charCodeAt(nextCharacterIndex++) === queryCharacter) {
  816. continue search;
  817. }
  818. }
  819. return false;
  820. }
  821. return true;
  822. },
  823. exactSearch: function (query, term) {
  824. query = query.toLowerCase();
  825. term = term.toLowerCase();
  826. if(term.indexOf(query) > -1) {
  827. return true;
  828. }
  829. return false;
  830. },
  831. filterActive: function() {
  832. if(settings.useLabels) {
  833. $item.filter('.' + className.active)
  834. .addClass(className.filtered)
  835. ;
  836. }
  837. },
  838. focusSearch: function(skipHandler) {
  839. if( module.has.search() && !module.is.focusedOnSearch() ) {
  840. if(skipHandler) {
  841. $module.off('focus' + eventNamespace, selector.search);
  842. $search.focus();
  843. $module.on('focus' + eventNamespace, selector.search, module.event.search.focus);
  844. }
  845. else {
  846. $search.focus();
  847. }
  848. }
  849. },
  850. forceSelection: function() {
  851. var
  852. $currentlySelected = $item.not(className.filtered).filter('.' + className.selected).eq(0),
  853. $activeItem = $item.not(className.filtered).filter('.' + className.active).eq(0),
  854. $selectedItem = ($currentlySelected.length > 0)
  855. ? $currentlySelected
  856. : $activeItem,
  857. hasSelected = ($selectedItem.length > 0)
  858. ;
  859. if(hasSelected && !module.is.multiple()) {
  860. module.debug('Forcing partial selection to selected item', $selectedItem);
  861. module.event.item.click.call($selectedItem, {}, true);
  862. return;
  863. }
  864. else {
  865. if(settings.allowAdditions) {
  866. module.set.selected(module.get.query());
  867. module.remove.searchTerm();
  868. }
  869. else {
  870. module.remove.searchTerm();
  871. }
  872. }
  873. },
  874. change: {
  875. values: function(values) {
  876. if(!settings.allowAdditions) {
  877. module.clear();
  878. }
  879. module.debug('Creating dropdown with specified values', values);
  880. module.setup.menu({values: values});
  881. $.each(values, function(index, item) {
  882. if(item.selected == true) {
  883. module.debug('Setting initial selection to', item.value);
  884. module.set.selected(item.value);
  885. return true;
  886. }
  887. });
  888. }
  889. },
  890. event: {
  891. change: function() {
  892. if(!internalChange) {
  893. module.debug('Input changed, updating selection');
  894. module.set.selected();
  895. }
  896. },
  897. focus: function() {
  898. if(settings.showOnFocus && !activated && module.is.hidden() && !pageLostFocus) {
  899. module.show();
  900. }
  901. },
  902. blur: function(event) {
  903. pageLostFocus = (document.activeElement === this);
  904. if(!activated && !pageLostFocus) {
  905. module.remove.activeLabel();
  906. module.hide();
  907. }
  908. },
  909. mousedown: function() {
  910. if(module.is.searchSelection()) {
  911. // prevent menu hiding on immediate re-focus
  912. willRefocus = true;
  913. }
  914. else {
  915. // prevents focus callback from occurring on mousedown
  916. activated = true;
  917. }
  918. },
  919. mouseup: function() {
  920. if(module.is.searchSelection()) {
  921. // prevent menu hiding on immediate re-focus
  922. willRefocus = false;
  923. }
  924. else {
  925. activated = false;
  926. }
  927. },
  928. click: function(event) {
  929. var
  930. $target = $(event.target)
  931. ;
  932. // focus search
  933. if($target.is($module)) {
  934. if(!module.is.focusedOnSearch()) {
  935. module.focusSearch();
  936. }
  937. else {
  938. module.show();
  939. }
  940. }
  941. },
  942. search: {
  943. focus: function() {
  944. activated = true;
  945. if(module.is.multiple()) {
  946. module.remove.activeLabel();
  947. }
  948. if(settings.showOnFocus) {
  949. module.search();
  950. }
  951. },
  952. blur: function(event) {
  953. pageLostFocus = (document.activeElement === this);
  954. if(module.is.searchSelection() && !willRefocus) {
  955. if(!itemActivated && !pageLostFocus) {
  956. if(settings.forceSelection) {
  957. module.forceSelection();
  958. }
  959. module.hide();
  960. }
  961. }
  962. willRefocus = false;
  963. }
  964. },
  965. icon: {
  966. click: function(event) {
  967. if($icon.hasClass(className.clear)) {
  968. module.clear();
  969. }
  970. else if (module.can.click()) {
  971. module.toggle();
  972. }
  973. }
  974. },
  975. text: {
  976. focus: function(event) {
  977. activated = true;
  978. module.focusSearch();
  979. }
  980. },
  981. input: function(event) {
  982. if(module.is.multiple() || module.is.searchSelection()) {
  983. module.set.filtered();
  984. }
  985. clearTimeout(module.timer);
  986. module.timer = setTimeout(module.search, settings.delay.search);
  987. },
  988. label: {
  989. click: function(event) {
  990. var
  991. $label = $(this),
  992. $labels = $module.find(selector.label),
  993. $activeLabels = $labels.filter('.' + className.active),
  994. $nextActive = $label.nextAll('.' + className.active),
  995. $prevActive = $label.prevAll('.' + className.active),
  996. $range = ($nextActive.length > 0)
  997. ? $label.nextUntil($nextActive).add($activeLabels).add($label)
  998. : $label.prevUntil($prevActive).add($activeLabels).add($label)
  999. ;
  1000. if(event.shiftKey) {
  1001. $activeLabels.removeClass(className.active);
  1002. $range.addClass(className.active);
  1003. }
  1004. else if(event.ctrlKey) {
  1005. $label.toggleClass(className.active);
  1006. }
  1007. else {
  1008. $activeLabels.removeClass(className.active);
  1009. $label.addClass(className.active);
  1010. }
  1011. settings.onLabelSelect.apply(this, $labels.filter('.' + className.active));
  1012. }
  1013. },
  1014. remove: {
  1015. click: function() {
  1016. var
  1017. $label = $(this).parent()
  1018. ;
  1019. if( $label.hasClass(className.active) ) {
  1020. // remove all selected labels
  1021. module.remove.activeLabels();
  1022. }
  1023. else {
  1024. // remove this label only
  1025. module.remove.activeLabels( $label );
  1026. }
  1027. }
  1028. },
  1029. test: {
  1030. toggle: function(event) {
  1031. var
  1032. toggleBehavior = (module.is.multiple())
  1033. ? module.show
  1034. : module.toggle
  1035. ;
  1036. if(module.is.bubbledLabelClick(event) || module.is.bubbledIconClick(event)) {
  1037. return;
  1038. }
  1039. if( module.determine.eventOnElement(event, toggleBehavior) ) {
  1040. event.preventDefault();
  1041. }
  1042. },
  1043. touch: function(event) {
  1044. module.determine.eventOnElement(event, function() {
  1045. if(event.type == 'touchstart') {
  1046. module.timer = setTimeout(function() {
  1047. module.hide();
  1048. }, settings.delay.touch);
  1049. }
  1050. else if(event.type == 'touchmove') {
  1051. clearTimeout(module.timer);
  1052. }
  1053. });
  1054. event.stopPropagation();
  1055. },
  1056. hide: function(event) {
  1057. module.determine.eventInModule(event, module.hide);
  1058. }
  1059. },
  1060. select: {
  1061. mutation: function(mutations) {
  1062. module.debug('<select> modified, recreating menu');
  1063. var
  1064. isSelectMutation = false
  1065. ;
  1066. $.each(mutations, function(index, mutation) {
  1067. if($(mutation.target).is('select') || $(mutation.addedNodes).is('select')) {
  1068. isSelectMutation = true;
  1069. return true;
  1070. }
  1071. });
  1072. if(isSelectMutation) {
  1073. module.disconnect.selectObserver();
  1074. module.refresh();
  1075. module.setup.select();
  1076. module.set.selected();
  1077. module.observe.select();
  1078. }
  1079. }
  1080. },
  1081. menu: {
  1082. mutation: function(mutations) {
  1083. var
  1084. mutation = mutations[0],
  1085. $addedNode = mutation.addedNodes
  1086. ? $(mutation.addedNodes[0])
  1087. : $(false),
  1088. $removedNode = mutation.removedNodes
  1089. ? $(mutation.removedNodes[0])
  1090. : $(false),
  1091. $changedNodes = $addedNode.add($removedNode),
  1092. isUserAddition = $changedNodes.is(selector.addition) || $changedNodes.closest(selector.addition).length > 0,
  1093. isMessage = $changedNodes.is(selector.message) || $changedNodes.closest(selector.message).length > 0
  1094. ;
  1095. if(isUserAddition || isMessage) {
  1096. module.debug('Updating item selector cache');
  1097. module.refreshItems();
  1098. }
  1099. else {
  1100. module.debug('Menu modified, updating selector cache');
  1101. module.refresh();
  1102. }
  1103. },
  1104. mousedown: function() {
  1105. itemActivated = true;
  1106. },
  1107. mouseup: function() {
  1108. itemActivated = false;
  1109. }
  1110. },
  1111. item: {
  1112. mouseenter: function(event) {
  1113. var
  1114. $target = $(event.target),
  1115. $item = $(this),
  1116. $subMenu = $item.children(selector.menu),
  1117. $otherMenus = $item.siblings(selector.item).children(selector.menu),
  1118. hasSubMenu = ($subMenu.length > 0),
  1119. isBubbledEvent = ($subMenu.find($target).length > 0)
  1120. ;
  1121. if( !isBubbledEvent && hasSubMenu ) {
  1122. clearTimeout(module.itemTimer);
  1123. module.itemTimer = setTimeout(function() {
  1124. module.verbose('Showing sub-menu', $subMenu);
  1125. $.each($otherMenus, function() {
  1126. module.animate.hide(false, $(this));
  1127. });
  1128. module.animate.show(false, $subMenu);
  1129. }, settings.delay.show);
  1130. event.preventDefault();
  1131. }
  1132. },
  1133. mouseleave: function(event) {
  1134. var
  1135. $subMenu = $(this).children(selector.menu)
  1136. ;
  1137. if($subMenu.length > 0) {
  1138. clearTimeout(module.itemTimer);
  1139. module.itemTimer = setTimeout(function() {
  1140. module.verbose('Hiding sub-menu', $subMenu);
  1141. module.animate.hide(false, $subMenu);
  1142. }, settings.delay.hide);
  1143. }
  1144. },
  1145. click: function (event, skipRefocus) {
  1146. var
  1147. $choice = $(this),
  1148. $target = (event)
  1149. ? $(event.target)
  1150. : $(''),
  1151. $subMenu = $choice.find(selector.menu),
  1152. text = module.get.choiceText($choice),
  1153. value = module.get.choiceValue($choice, text),
  1154. hasSubMenu = ($subMenu.length > 0),
  1155. isBubbledEvent = ($subMenu.find($target).length > 0)
  1156. ;
  1157. // prevents IE11 bug where menu receives focus even though `tabindex=-1`
  1158. if(module.has.menuSearch()) {
  1159. $(document.activeElement).blur();
  1160. }
  1161. if(!isBubbledEvent && (!hasSubMenu || settings.allowCategorySelection)) {
  1162. if(module.is.searchSelection()) {
  1163. if(settings.allowAdditions) {
  1164. module.remove.userAddition();
  1165. }
  1166. module.remove.searchTerm();
  1167. if(!module.is.focusedOnSearch() && !(skipRefocus == true)) {
  1168. module.focusSearch(true);
  1169. }
  1170. }
  1171. if(!settings.useLabels) {
  1172. module.remove.filteredItem();
  1173. module.set.scrollPosition($choice);
  1174. }
  1175. module.determine.selectAction.call(this, text, value);
  1176. }
  1177. }
  1178. },
  1179. document: {
  1180. // label selection should occur even when element has no focus
  1181. keydown: function(event) {
  1182. var
  1183. pressedKey = event.which,
  1184. isShortcutKey = module.is.inObject(pressedKey, keys)
  1185. ;
  1186. if(isShortcutKey) {
  1187. var
  1188. $label = $module.find(selector.label),
  1189. $activeLabel = $label.filter('.' + className.active),
  1190. activeValue = $activeLabel.data(metadata.value),
  1191. labelIndex = $label.index($activeLabel),
  1192. labelCount = $label.length,
  1193. hasActiveLabel = ($activeLabel.length > 0),
  1194. hasMultipleActive = ($activeLabel.length > 1),
  1195. isFirstLabel = (labelIndex === 0),
  1196. isLastLabel = (labelIndex + 1 == labelCount),
  1197. isSearch = module.is.searchSelection(),
  1198. isFocusedOnSearch = module.is.focusedOnSearch(),
  1199. isFocused = module.is.focused(),
  1200. caretAtStart = (isFocusedOnSearch && module.get.caretPosition() === 0),
  1201. $nextLabel
  1202. ;
  1203. if(isSearch && !hasActiveLabel && !isFocusedOnSearch) {
  1204. return;
  1205. }
  1206. if(pressedKey == keys.leftArrow) {
  1207. // activate previous label
  1208. if((isFocused || caretAtStart) && !hasActiveLabel) {
  1209. module.verbose('Selecting previous label');
  1210. $label.last().addClass(className.active);
  1211. }
  1212. else if(hasActiveLabel) {
  1213. if(!event.shiftKey) {
  1214. module.verbose('Selecting previous label');
  1215. $label.removeClass(className.active);
  1216. }
  1217. else {
  1218. module.verbose('Adding previous label to selection');
  1219. }
  1220. if(isFirstLabel && !hasMultipleActive) {
  1221. $activeLabel.addClass(className.active);
  1222. }
  1223. else {
  1224. $activeLabel.prev(selector.siblingLabel)
  1225. .addClass(className.active)
  1226. .end()
  1227. ;
  1228. }
  1229. event.preventDefault();
  1230. }
  1231. }
  1232. else if(pressedKey == keys.rightArrow) {
  1233. // activate first label
  1234. if(isFocused && !hasActiveLabel) {
  1235. $label.first().addClass(className.active);
  1236. }
  1237. // activate next label
  1238. if(hasActiveLabel) {
  1239. if(!event.shiftKey) {
  1240. module.verbose('Selecting next label');
  1241. $label.removeClass(className.active);
  1242. }
  1243. else {
  1244. module.verbose('Adding next label to selection');
  1245. }
  1246. if(isLastLabel) {
  1247. if(isSearch) {
  1248. if(!isFocusedOnSearch) {
  1249. module.focusSearch();
  1250. }
  1251. else {
  1252. $label.removeClass(className.active);
  1253. }
  1254. }
  1255. else if(hasMultipleActive) {
  1256. $activeLabel.next(selector.siblingLabel).addClass(className.active);
  1257. }
  1258. else {
  1259. $activeLabel.addClass(className.active);
  1260. }
  1261. }
  1262. else {
  1263. $activeLabel.next(selector.siblingLabel).addClass(className.active);
  1264. }
  1265. event.preventDefault();
  1266. }
  1267. }
  1268. else if(pressedKey == keys.deleteKey || pressedKey == keys.backspace) {
  1269. if(hasActiveLabel) {
  1270. module.verbose('Removing active labels');
  1271. if(isLastLabel) {
  1272. if(isSearch && !isFocusedOnSearch) {
  1273. module.focusSearch();
  1274. }
  1275. }
  1276. $activeLabel.last().next(selector.siblingLabel).addClass(className.active);
  1277. module.remove.activeLabels($activeLabel);
  1278. event.preventDefault();
  1279. }
  1280. else if(caretAtStart && !hasActiveLabel && pressedKey == keys.backspace) {
  1281. module.verbose('Removing last label on input backspace');
  1282. $activeLabel = $label.last().addClass(className.active);
  1283. module.remove.activeLabels($activeLabel);
  1284. }
  1285. }
  1286. else {
  1287. $activeLabel.removeClass(className.active);
  1288. }
  1289. }
  1290. }
  1291. },
  1292. keydown: function(event) {
  1293. var
  1294. pressedKey = event.which,
  1295. isShortcutKey = module.is.inObject(pressedKey, keys)
  1296. ;
  1297. if(isShortcutKey) {
  1298. var
  1299. $currentlySelected = $item.not(selector.unselectable).filter('.' + className.selected).eq(0),
  1300. $activeItem = $menu.children('.' + className.active).eq(0),
  1301. $selectedItem = ($currentlySelected.length > 0)
  1302. ? $currentlySelected
  1303. : $activeItem,
  1304. $visibleItems = ($selectedItem.length > 0)
  1305. ? $selectedItem.siblings(':not(.' + className.filtered +')').addBack()
  1306. : $menu.children(':not(.' + className.filtered +')'),
  1307. $subMenu = $selectedItem.children(selector.menu),
  1308. $parentMenu = $selectedItem.closest(selector.menu),
  1309. inVisibleMenu = ($parentMenu.hasClass(className.visible) || $parentMenu.hasClass(className.animating) || $parentMenu.parent(selector.menu).length > 0),
  1310. hasSubMenu = ($subMenu.length> 0),
  1311. hasSelectedItem = ($selectedItem.length > 0),
  1312. selectedIsSelectable = ($selectedItem.not(selector.unselectable).length > 0),
  1313. delimiterPressed = (pressedKey == keys.delimiter && settings.allowAdditions && module.is.multiple()),
  1314. isAdditionWithoutMenu = (settings.allowAdditions && settings.hideAdditions && (pressedKey == keys.enter || delimiterPressed) && selectedIsSelectable),
  1315. $nextItem,
  1316. isSubMenuItem,
  1317. newIndex
  1318. ;
  1319. // allow selection with menu closed
  1320. if(isAdditionWithoutMenu) {
  1321. module.verbose('Selecting item from keyboard shortcut', $selectedItem);
  1322. module.event.item.click.call($selectedItem, event);
  1323. if(module.is.searchSelection()) {
  1324. module.remove.searchTerm();
  1325. }
  1326. }
  1327. // visible menu keyboard shortcuts
  1328. if( module.is.visible() ) {
  1329. // enter (select or open sub-menu)
  1330. if(pressedKey == keys.enter || delimiterPressed) {
  1331. if(pressedKey == keys.enter && hasSelectedItem && hasSubMenu && !settings.allowCategorySelection) {
  1332. module.verbose('Pressed enter on unselectable category, opening sub menu');
  1333. pressedKey = keys.rightArrow;
  1334. }
  1335. else if(selectedIsSelectable) {
  1336. module.verbose('Selecting item from keyboard shortcut', $selectedItem);
  1337. module.event.item.click.call($selectedItem, event);
  1338. if(module.is.searchSelection()) {
  1339. module.remove.searchTerm();
  1340. }
  1341. }
  1342. event.preventDefault();
  1343. }
  1344. // sub-menu actions
  1345. if(hasSelectedItem) {
  1346. if(pressedKey == keys.leftArrow) {
  1347. isSubMenuItem = ($parentMenu[0] !== $menu[0]);
  1348. if(isSubMenuItem) {
  1349. module.verbose('Left key pressed, closing sub-menu');
  1350. module.animate.hide(false, $parentMenu);
  1351. $selectedItem
  1352. .removeClass(className.selected)
  1353. ;
  1354. $parentMenu
  1355. .closest(selector.item)
  1356. .addClass(className.selected)
  1357. ;
  1358. event.preventDefault();
  1359. }
  1360. }
  1361. // right arrow (show sub-menu)
  1362. if(pressedKey == keys.rightArrow) {
  1363. if(hasSubMenu) {
  1364. module.verbose('Right key pressed, opening sub-menu');
  1365. module.animate.show(false, $subMenu);
  1366. $selectedItem
  1367. .removeClass(className.selected)
  1368. ;
  1369. $subMenu
  1370. .find(selector.item).eq(0)
  1371. .addClass(className.selected)
  1372. ;
  1373. event.preventDefault();
  1374. }
  1375. }
  1376. }
  1377. // up arrow (traverse menu up)
  1378. if(pressedKey == keys.upArrow) {
  1379. $nextItem = (hasSelectedItem && inVisibleMenu)
  1380. ? $selectedItem.prevAll(selector.item + ':not(' + selector.unselectable + ')').eq(0)
  1381. : $item.eq(0)
  1382. ;
  1383. if($visibleItems.index( $nextItem ) < 0) {
  1384. module.verbose('Up key pressed but reached top of current menu');
  1385. event.preventDefault();
  1386. return;
  1387. }
  1388. else {
  1389. module.verbose('Up key pressed, changing active item');
  1390. $selectedItem
  1391. .removeClass(className.selected)
  1392. ;
  1393. $nextItem
  1394. .addClass(className.selected)
  1395. ;
  1396. module.set.scrollPosition($nextItem);
  1397. if(settings.selectOnKeydown && module.is.single()) {
  1398. module.set.selectedItem($nextItem);
  1399. }
  1400. }
  1401. event.preventDefault();
  1402. }
  1403. // down arrow (traverse menu down)
  1404. if(pressedKey == keys.downArrow) {
  1405. $nextItem = (hasSelectedItem && inVisibleMenu)
  1406. ? $nextItem = $selectedItem.nextAll(selector.item + ':not(' + selector.unselectable + ')').eq(0)
  1407. : $item.eq(0)
  1408. ;
  1409. if($nextItem.length === 0) {
  1410. module.verbose('Down key pressed but reached bottom of current menu');
  1411. event.preventDefault();
  1412. return;
  1413. }
  1414. else {
  1415. module.verbose('Down key pressed, changing active item');
  1416. $item
  1417. .removeClass(className.selected)
  1418. ;
  1419. $nextItem
  1420. .addClass(className.selected)
  1421. ;
  1422. module.set.scrollPosition($nextItem);
  1423. if(settings.selectOnKeydown && module.is.single()) {
  1424. module.set.selectedItem($nextItem);
  1425. }
  1426. }
  1427. event.preventDefault();
  1428. }
  1429. // page down (show next page)
  1430. if(pressedKey == keys.pageUp) {
  1431. module.scrollPage('up');
  1432. event.preventDefault();
  1433. }
  1434. if(pressedKey == keys.pageDown) {
  1435. module.scrollPage('down');
  1436. event.preventDefault();
  1437. }
  1438. // escape (close menu)
  1439. if(pressedKey == keys.escape) {
  1440. module.verbose('Escape key pressed, closing dropdown');
  1441. module.hide();
  1442. }
  1443. }
  1444. else {
  1445. // delimiter key
  1446. if(delimiterPressed) {
  1447. event.preventDefault();
  1448. }
  1449. // down arrow (open menu)
  1450. if(pressedKey == keys.downArrow && !module.is.visible()) {
  1451. module.verbose('Down key pressed, showing dropdown');
  1452. module.show();
  1453. event.preventDefault();
  1454. }
  1455. }
  1456. }
  1457. else {
  1458. if( !module.has.search() ) {
  1459. module.set.selectedLetter( String.fromCharCode(pressedKey) );
  1460. }
  1461. }
  1462. }
  1463. },
  1464. trigger: {
  1465. change: function() {
  1466. var
  1467. events = document.createEvent('HTMLEvents'),
  1468. inputElement = $input[0]
  1469. ;
  1470. if(inputElement) {
  1471. module.verbose('Triggering native change event');
  1472. events.initEvent('change', true, false);
  1473. inputElement.dispatchEvent(events);
  1474. }
  1475. }
  1476. },
  1477. determine: {
  1478. selectAction: function(text, value) {
  1479. module.verbose('Determining action', settings.action);
  1480. if( $.isFunction( module.action[settings.action] ) ) {
  1481. module.verbose('Triggering preset action', settings.action, text, value);
  1482. module.action[ settings.action ].call(element, text, value, this);
  1483. }
  1484. else if( $.isFunction(settings.action) ) {
  1485. module.verbose('Triggering user action', settings.action, text, value);
  1486. settings.action.call(element, text, value, this);
  1487. }
  1488. else {
  1489. module.error(error.action, settings.action);
  1490. }
  1491. },
  1492. eventInModule: function(event, callback) {
  1493. var
  1494. $target = $(event.target),
  1495. inDocument = ($target.closest(document.documentElement).length > 0),
  1496. inModule = ($target.closest($module).length > 0)
  1497. ;
  1498. callback = $.isFunction(callback)
  1499. ? callback
  1500. : function(){}
  1501. ;
  1502. if(inDocument && !inModule) {
  1503. module.verbose('Triggering event', callback);
  1504. callback();
  1505. return true;
  1506. }
  1507. else {
  1508. module.verbose('Event occurred in dropdown, canceling callback');
  1509. return false;
  1510. }
  1511. },
  1512. eventOnElement: function(event, callback) {
  1513. var
  1514. $target = $(event.target),
  1515. $label = $target.closest(selector.siblingLabel),
  1516. inVisibleDOM = document.body.contains(event.target),
  1517. notOnLabel = ($module.find($label).length === 0),
  1518. notInMenu = ($target.closest($menu).length === 0)
  1519. ;
  1520. callback = $.isFunction(callback)
  1521. ? callback
  1522. : function(){}
  1523. ;
  1524. if(inVisibleDOM && notOnLabel && notInMenu) {
  1525. module.verbose('Triggering event', callback);
  1526. callback();
  1527. return true;
  1528. }
  1529. else {
  1530. module.verbose('Event occurred in dropdown menu, canceling callback');
  1531. return false;
  1532. }
  1533. }
  1534. },
  1535. action: {
  1536. nothing: function() {},
  1537. activate: function(text, value, element) {
  1538. value = (value !== undefined)
  1539. ? value
  1540. : text
  1541. ;
  1542. if( module.can.activate( $(element) ) ) {
  1543. module.set.selected(value, $(element));
  1544. if(module.is.multiple() && !module.is.allFiltered()) {
  1545. return;
  1546. }
  1547. else {
  1548. module.hideAndClear();
  1549. }
  1550. }
  1551. },
  1552. select: function(text, value, element) {
  1553. value = (value !== undefined)
  1554. ? value
  1555. : text
  1556. ;
  1557. if( module.can.activate( $(element) ) ) {
  1558. module.set.value(value, text, $(element));
  1559. if(module.is.multiple() && !module.is.allFiltered()) {
  1560. return;
  1561. }
  1562. else {
  1563. module.hideAndClear();
  1564. }
  1565. }
  1566. },
  1567. combo: function(text, value, element) {
  1568. value = (value !== undefined)
  1569. ? value
  1570. : text
  1571. ;
  1572. module.set.selected(value, $(element));
  1573. module.hideAndClear();
  1574. },
  1575. hide: function(text, value, element) {
  1576. module.set.value(value, text, $(element));
  1577. module.hideAndClear();
  1578. }
  1579. },
  1580. get: {
  1581. id: function() {
  1582. return id;
  1583. },
  1584. defaultText: function() {
  1585. return $module.data(metadata.defaultText);
  1586. },
  1587. defaultValue: function() {
  1588. return $module.data(metadata.defaultValue);
  1589. },
  1590. placeholderText: function() {
  1591. if(settings.placeholder != 'auto' && typeof settings.placeholder == 'string') {
  1592. return settings.placeholder;
  1593. }
  1594. return $module.data(metadata.placeholderText) || '';
  1595. },
  1596. text: function() {
  1597. return $text.text();
  1598. },
  1599. query: function() {
  1600. return $.trim($search.val());
  1601. },
  1602. searchWidth: function(value) {
  1603. value = (value !== undefined)
  1604. ? value
  1605. : $search.val()
  1606. ;
  1607. $sizer.text(value);
  1608. // prevent rounding issues
  1609. return Math.ceil( $sizer.width() + 1);
  1610. },
  1611. selectionCount: function() {
  1612. var
  1613. values = module.get.values(),
  1614. count
  1615. ;
  1616. count = ( module.is.multiple() )
  1617. ? $.isArray(values)
  1618. ? values.length
  1619. : 0
  1620. : (module.get.value() !== '')
  1621. ? 1
  1622. : 0
  1623. ;
  1624. return count;
  1625. },
  1626. transition: function($subMenu) {
  1627. return (settings.transition == 'auto')
  1628. ? module.is.upward($subMenu)
  1629. ? 'slide up'
  1630. : 'slide down'
  1631. : settings.transition
  1632. ;
  1633. },
  1634. userValues: function() {
  1635. var
  1636. values = module.get.values()
  1637. ;
  1638. if(!values) {
  1639. return false;
  1640. }
  1641. values = $.isArray(values)
  1642. ? values
  1643. : [values]
  1644. ;
  1645. return $.grep(values, function(value) {
  1646. return (module.get.item(value) === false);
  1647. });
  1648. },
  1649. uniqueArray: function(array) {
  1650. return $.grep(array, function (value, index) {
  1651. return $.inArray(value, array) === index;
  1652. });
  1653. },
  1654. caretPosition: function() {
  1655. var
  1656. input = $search.get(0),
  1657. range,
  1658. rangeLength
  1659. ;
  1660. if('selectionStart' in input) {
  1661. return input.selectionStart;
  1662. }
  1663. else if (document.selection) {
  1664. input.focus();
  1665. range = document.selection.createRange();
  1666. rangeLength = range.text.length;
  1667. range.moveStart('character', -input.value.length);
  1668. return range.text.length - rangeLength;
  1669. }
  1670. },
  1671. value: function() {
  1672. var
  1673. value = ($input.length > 0)
  1674. ? $input.val()
  1675. : $module.data(metadata.value),
  1676. isEmptyMultiselect = ($.isArray(value) && value.length === 1 && value[0] === '')
  1677. ;
  1678. // prevents placeholder element from being selected when multiple
  1679. return (value === undefined || isEmptyMultiselect)
  1680. ? ''
  1681. : value
  1682. ;
  1683. },
  1684. values: function() {
  1685. var
  1686. value = module.get.value()
  1687. ;
  1688. if(value === '') {
  1689. return '';
  1690. }
  1691. return ( !module.has.selectInput() && module.is.multiple() )
  1692. ? (typeof value == 'string') // delimited string
  1693. ? value.split(settings.delimiter)
  1694. : ''
  1695. : value
  1696. ;
  1697. },
  1698. remoteValues: function() {
  1699. var
  1700. values = module.get.values(),
  1701. remoteValues = false
  1702. ;
  1703. if(values) {
  1704. if(typeof values == 'string') {
  1705. values = [values];
  1706. }
  1707. $.each(values, function(index, value) {
  1708. var
  1709. name = module.read.remoteData(value)
  1710. ;
  1711. module.verbose('Restoring value from session data', name, value);
  1712. if(name) {
  1713. if(!remoteValues) {
  1714. remoteValues = {};
  1715. }
  1716. remoteValues[value] = name;
  1717. }
  1718. });
  1719. }
  1720. return remoteValues;
  1721. },
  1722. choiceText: function($choice, preserveHTML) {
  1723. preserveHTML = (preserveHTML !== undefined)
  1724. ? preserveHTML
  1725. : settings.preserveHTML
  1726. ;
  1727. if($choice) {
  1728. if($choice.find(selector.menu).length > 0) {
  1729. module.verbose('Retrieving text of element with sub-menu');
  1730. $choice = $choice.clone();
  1731. $choice.find(selector.menu).remove();
  1732. $choice.find(selector.menuIcon).remove();
  1733. }
  1734. return ($choice.data(metadata.text) !== undefined)
  1735. ? $choice.data(metadata.text)
  1736. : (preserveHTML)
  1737. ? $.trim($choice.html())
  1738. : $.trim($choice.text())
  1739. ;
  1740. }
  1741. },
  1742. choiceValue: function($choice, choiceText) {
  1743. choiceText = choiceText || module.get.choiceText($choice);
  1744. if(!$choice) {
  1745. return false;
  1746. }
  1747. return ($choice.data(metadata.value) !== undefined)
  1748. ? String( $choice.data(metadata.value) )
  1749. : (typeof choiceText === 'string')
  1750. ? $.trim(choiceText.toLowerCase())
  1751. : String(choiceText)
  1752. ;
  1753. },
  1754. inputEvent: function() {
  1755. var
  1756. input = $search[0]
  1757. ;
  1758. if(input) {
  1759. return (input.oninput !== undefined)
  1760. ? 'input'
  1761. : (input.onpropertychange !== undefined)
  1762. ? 'propertychange'
  1763. : 'keyup'
  1764. ;
  1765. }
  1766. return false;
  1767. },
  1768. selectValues: function() {
  1769. var
  1770. select = {}
  1771. ;
  1772. select.values = [];
  1773. $module
  1774. .find('option')
  1775. .each(function() {
  1776. var
  1777. $option = $(this),
  1778. name = $option.html(),
  1779. disabled = $option.attr('disabled'),
  1780. value = ( $option.attr('value') !== undefined )
  1781. ? $option.attr('value')
  1782. : name
  1783. ;
  1784. if(settings.placeholder === 'auto' && value === '') {
  1785. select.placeholder = name;
  1786. }
  1787. else {
  1788. select.values.push({
  1789. name : name,
  1790. value : value,
  1791. disabled : disabled
  1792. });
  1793. }
  1794. })
  1795. ;
  1796. if(settings.placeholder && settings.placeholder !== 'auto') {
  1797. module.debug('Setting placeholder value to', settings.placeholder);
  1798. select.placeholder = settings.placeholder;
  1799. }
  1800. if(settings.sortSelect) {
  1801. select.values.sort(function(a, b) {
  1802. return (a.name > b.name)
  1803. ? 1
  1804. : -1
  1805. ;
  1806. });
  1807. module.debug('Retrieved and sorted values from select', select);
  1808. }
  1809. else {
  1810. module.debug('Retrieved values from select', select);
  1811. }
  1812. return select;
  1813. },
  1814. activeItem: function() {
  1815. return $item.filter('.' + className.active);
  1816. },
  1817. selectedItem: function() {
  1818. var
  1819. $selectedItem = $item.not(selector.unselectable).filter('.' + className.selected)
  1820. ;
  1821. return ($selectedItem.length > 0)
  1822. ? $selectedItem
  1823. : $item.eq(0)
  1824. ;
  1825. },
  1826. itemWithAdditions: function(value) {
  1827. var
  1828. $items = module.get.item(value),
  1829. $userItems = module.create.userChoice(value),
  1830. hasUserItems = ($userItems && $userItems.length > 0)
  1831. ;
  1832. if(hasUserItems) {
  1833. $items = ($items.length > 0)
  1834. ? $items.add($userItems)
  1835. : $userItems
  1836. ;
  1837. }
  1838. return $items;
  1839. },
  1840. item: function(value, strict) {
  1841. var
  1842. $selectedItem = false,
  1843. shouldSearch,
  1844. isMultiple
  1845. ;
  1846. value = (value !== undefined)
  1847. ? value
  1848. : ( module.get.values() !== undefined)
  1849. ? module.get.values()
  1850. : module.get.text()
  1851. ;
  1852. shouldSearch = (isMultiple)
  1853. ? (value.length > 0)
  1854. : (value !== undefined && value !== null)
  1855. ;
  1856. isMultiple = (module.is.multiple() && $.isArray(value));
  1857. strict = (value === '' || value === 0)
  1858. ? true
  1859. : strict || false
  1860. ;
  1861. if(shouldSearch) {
  1862. $item
  1863. .each(function() {
  1864. var
  1865. $choice = $(this),
  1866. optionText = module.get.choiceText($choice),
  1867. optionValue = module.get.choiceValue($choice, optionText)
  1868. ;
  1869. // safe early exit
  1870. if(optionValue === null || optionValue === undefined) {
  1871. return;
  1872. }
  1873. if(isMultiple) {
  1874. if($.inArray( String(optionValue), value) !== -1 || $.inArray(optionText, value) !== -1) {
  1875. $selectedItem = ($selectedItem)
  1876. ? $selectedItem.add($choice)
  1877. : $choice
  1878. ;
  1879. }
  1880. }
  1881. else if(strict) {
  1882. module.verbose('Ambiguous dropdown value using strict type check', $choice, value);
  1883. if( optionValue === value || optionText === value) {
  1884. $selectedItem = $choice;
  1885. return true;
  1886. }
  1887. }
  1888. else {
  1889. if( String(optionValue) == String(value) || optionText == value) {
  1890. module.verbose('Found select item by value', optionValue, value);
  1891. $selectedItem = $choice;
  1892. return true;
  1893. }
  1894. }
  1895. })
  1896. ;
  1897. }
  1898. return $selectedItem;
  1899. }
  1900. },
  1901. check: {
  1902. maxSelections: function(selectionCount) {
  1903. if(settings.maxSelections) {
  1904. selectionCount = (selectionCount !== undefined)
  1905. ? selectionCount
  1906. : module.get.selectionCount()
  1907. ;
  1908. if(selectionCount >= settings.maxSelections) {
  1909. module.debug('Maximum selection count reached');
  1910. if(settings.useLabels) {
  1911. $item.addClass(className.filtered);
  1912. module.add.message(message.maxSelections);
  1913. }
  1914. return true;
  1915. }
  1916. else {
  1917. module.verbose('No longer at maximum selection count');
  1918. module.remove.message();
  1919. module.remove.filteredItem();
  1920. if(module.is.searchSelection()) {
  1921. module.filterItems();
  1922. }
  1923. return false;
  1924. }
  1925. }
  1926. return true;
  1927. }
  1928. },
  1929. restore: {
  1930. defaults: function() {
  1931. module.clear();
  1932. module.restore.defaultText();
  1933. module.restore.defaultValue();
  1934. },
  1935. defaultText: function() {
  1936. var
  1937. defaultText = module.get.defaultText(),
  1938. placeholderText = module.get.placeholderText
  1939. ;
  1940. if(defaultText === placeholderText) {
  1941. module.debug('Restoring default placeholder text', defaultText);
  1942. module.set.placeholderText(defaultText);
  1943. }
  1944. else {
  1945. module.debug('Restoring default text', defaultText);
  1946. module.set.text(defaultText);
  1947. }
  1948. },
  1949. placeholderText: function() {
  1950. module.set.placeholderText();
  1951. },
  1952. defaultValue: function() {
  1953. var
  1954. defaultValue = module.get.defaultValue()
  1955. ;
  1956. if(defaultValue !== undefined) {
  1957. module.debug('Restoring default value', defaultValue);
  1958. if(defaultValue !== '') {
  1959. module.set.value(defaultValue);
  1960. module.set.selected();
  1961. }
  1962. else {
  1963. module.remove.activeItem();
  1964. module.remove.selectedItem();
  1965. }
  1966. }
  1967. },
  1968. labels: function() {
  1969. if(settings.allowAdditions) {
  1970. if(!settings.useLabels) {
  1971. module.error(error.labels);
  1972. settings.useLabels = true;
  1973. }
  1974. module.debug('Restoring selected values');
  1975. module.create.userLabels();
  1976. }
  1977. module.check.maxSelections();
  1978. },
  1979. selected: function() {
  1980. module.restore.values();
  1981. if(module.is.multiple()) {
  1982. module.debug('Restoring previously selected values and labels');
  1983. module.restore.labels();
  1984. }
  1985. else {
  1986. module.debug('Restoring previously selected values');
  1987. }
  1988. },
  1989. values: function() {
  1990. // prevents callbacks from occurring on initial load
  1991. module.set.initialLoad();
  1992. if(settings.apiSettings && settings.saveRemoteData && module.get.remoteValues()) {
  1993. module.restore.remoteValues();
  1994. }
  1995. else {
  1996. module.set.selected();
  1997. }
  1998. module.remove.initialLoad();
  1999. },
  2000. remoteValues: function() {
  2001. var
  2002. values = module.get.remoteValues()
  2003. ;
  2004. module.debug('Recreating selected from session data', values);
  2005. if(values) {
  2006. if( module.is.single() ) {
  2007. $.each(values, function(value, name) {
  2008. module.set.text(name);
  2009. });
  2010. }
  2011. else {
  2012. $.each(values, function(value, name) {
  2013. module.add.label(value, name);
  2014. });
  2015. }
  2016. }
  2017. }
  2018. },
  2019. read: {
  2020. remoteData: function(value) {
  2021. var
  2022. name
  2023. ;
  2024. if(window.Storage === undefined) {
  2025. module.error(error.noStorage);
  2026. return;
  2027. }
  2028. name = sessionStorage.getItem(value);
  2029. return (name !== undefined)
  2030. ? name
  2031. : false
  2032. ;
  2033. }
  2034. },
  2035. save: {
  2036. defaults: function() {
  2037. module.save.defaultText();
  2038. module.save.placeholderText();
  2039. module.save.defaultValue();
  2040. },
  2041. defaultValue: function() {
  2042. var
  2043. value = module.get.value()
  2044. ;
  2045. module.verbose('Saving default value as', value);
  2046. $module.data(metadata.defaultValue, value);
  2047. },
  2048. defaultText: function() {
  2049. var
  2050. text = module.get.text()
  2051. ;
  2052. module.verbose('Saving default text as', text);
  2053. $module.data(metadata.defaultText, text);
  2054. },
  2055. placeholderText: function() {
  2056. var
  2057. text
  2058. ;
  2059. if(settings.placeholder !== false && $text.hasClass(className.placeholder)) {
  2060. text = module.get.text();
  2061. module.verbose('Saving placeholder text as', text);
  2062. $module.data(metadata.placeholderText, text);
  2063. }
  2064. },
  2065. remoteData: function(name, value) {
  2066. if(window.Storage === undefined) {
  2067. module.error(error.noStorage);
  2068. return;
  2069. }
  2070. module.verbose('Saving remote data to session storage', value, name);
  2071. sessionStorage.setItem(value, name);
  2072. }
  2073. },
  2074. clear: function() {
  2075. if(module.is.multiple() && settings.useLabels) {
  2076. module.remove.labels();
  2077. }
  2078. else {
  2079. module.remove.activeItem();
  2080. module.remove.selectedItem();
  2081. }
  2082. module.set.placeholderText();
  2083. module.clearValue();
  2084. },
  2085. clearValue: function() {
  2086. module.set.value('');
  2087. },
  2088. scrollPage: function(direction, $selectedItem) {
  2089. var
  2090. $currentItem = $selectedItem || module.get.selectedItem(),
  2091. $menu = $currentItem.closest(selector.menu),
  2092. menuHeight = $menu.outerHeight(),
  2093. currentScroll = $menu.scrollTop(),
  2094. itemHeight = $item.eq(0).outerHeight(),
  2095. itemsPerPage = Math.floor(menuHeight / itemHeight),
  2096. maxScroll = $menu.prop('scrollHeight'),
  2097. newScroll = (direction == 'up')
  2098. ? currentScroll - (itemHeight * itemsPerPage)
  2099. : currentScroll + (itemHeight * itemsPerPage),
  2100. $selectableItem = $item.not(selector.unselectable),
  2101. isWithinRange,
  2102. $nextSelectedItem,
  2103. elementIndex
  2104. ;
  2105. elementIndex = (direction == 'up')
  2106. ? $selectableItem.index($currentItem) - itemsPerPage
  2107. : $selectableItem.index($currentItem) + itemsPerPage
  2108. ;
  2109. isWithinRange = (direction == 'up')
  2110. ? (elementIndex >= 0)
  2111. : (elementIndex < $selectableItem.length)
  2112. ;
  2113. $nextSelectedItem = (isWithinRange)
  2114. ? $selectableItem.eq(elementIndex)
  2115. : (direction == 'up')
  2116. ? $selectableItem.first()
  2117. : $selectableItem.last()
  2118. ;
  2119. if($nextSelectedItem.length > 0) {
  2120. module.debug('Scrolling page', direction, $nextSelectedItem);
  2121. $currentItem
  2122. .removeClass(className.selected)
  2123. ;
  2124. $nextSelectedItem
  2125. .addClass(className.selected)
  2126. ;
  2127. if(settings.selectOnKeydown && module.is.single()) {
  2128. module.set.selectedItem($nextSelectedItem);
  2129. }
  2130. $menu
  2131. .scrollTop(newScroll)
  2132. ;
  2133. }
  2134. },
  2135. set: {
  2136. filtered: function() {
  2137. var
  2138. isMultiple = module.is.multiple(),
  2139. isSearch = module.is.searchSelection(),
  2140. isSearchMultiple = (isMultiple && isSearch),
  2141. searchValue = (isSearch)
  2142. ? module.get.query()
  2143. : '',
  2144. hasSearchValue = (typeof searchValue === 'string' && searchValue.length > 0),
  2145. searchWidth = module.get.searchWidth(),
  2146. valueIsSet = searchValue !== ''
  2147. ;
  2148. if(isMultiple && hasSearchValue) {
  2149. module.verbose('Adjusting input width', searchWidth, settings.glyphWidth);
  2150. $search.css('width', searchWidth);
  2151. }
  2152. if(hasSearchValue || (isSearchMultiple && valueIsSet)) {
  2153. module.verbose('Hiding placeholder text');
  2154. $text.addClass(className.filtered);
  2155. }
  2156. else if(!isMultiple || (isSearchMultiple && !valueIsSet)) {
  2157. module.verbose('Showing placeholder text');
  2158. $text.removeClass(className.filtered);
  2159. }
  2160. },
  2161. empty: function() {
  2162. $module.addClass(className.empty);
  2163. },
  2164. loading: function() {
  2165. $module.addClass(className.loading);
  2166. },
  2167. placeholderText: function(text) {
  2168. text = text || module.get.placeholderText();
  2169. module.debug('Setting placeholder text', text);
  2170. module.set.text(text);
  2171. $text.addClass(className.placeholder);
  2172. },
  2173. tabbable: function() {
  2174. if( module.is.searchSelection() ) {
  2175. module.debug('Added tabindex to searchable dropdown');
  2176. $search
  2177. .val('')
  2178. .attr('tabindex', 0)
  2179. ;
  2180. $menu
  2181. .attr('tabindex', -1)
  2182. ;
  2183. }
  2184. else {
  2185. module.debug('Added tabindex to dropdown');
  2186. if( $module.attr('tabindex') === undefined) {
  2187. $module
  2188. .attr('tabindex', 0)
  2189. ;
  2190. $menu
  2191. .attr('tabindex', -1)
  2192. ;
  2193. }
  2194. }
  2195. },
  2196. initialLoad: function() {
  2197. module.verbose('Setting initial load');
  2198. initialLoad = true;
  2199. },
  2200. activeItem: function($item) {
  2201. if( settings.allowAdditions && $item.filter(selector.addition).length > 0 ) {
  2202. $item.addClass(className.filtered);
  2203. }
  2204. else {
  2205. $item.addClass(className.active);
  2206. }
  2207. },
  2208. partialSearch: function(text) {
  2209. var
  2210. length = module.get.query().length
  2211. ;
  2212. $search.val( text.substr(0, length));
  2213. },
  2214. scrollPosition: function($item, forceScroll) {
  2215. var
  2216. edgeTolerance = 5,
  2217. $menu,
  2218. hasActive,
  2219. offset,
  2220. itemHeight,
  2221. itemOffset,
  2222. menuOffset,
  2223. menuScroll,
  2224. menuHeight,
  2225. abovePage,
  2226. belowPage
  2227. ;
  2228. $item = $item || module.get.selectedItem();
  2229. $menu = $item.closest(selector.menu);
  2230. hasActive = ($item && $item.length > 0);
  2231. forceScroll = (forceScroll !== undefined)
  2232. ? forceScroll
  2233. : false
  2234. ;
  2235. if($item && $menu.length > 0 && hasActive) {
  2236. itemOffset = $item.position().top;
  2237. $menu.addClass(className.loading);
  2238. menuScroll = $menu.scrollTop();
  2239. menuOffset = $menu.offset().top;
  2240. itemOffset = $item.offset().top;
  2241. offset = menuScroll - menuOffset + itemOffset;
  2242. if(!forceScroll) {
  2243. menuHeight = $menu.height();
  2244. belowPage = menuScroll + menuHeight < (offset + edgeTolerance);
  2245. abovePage = ((offset - edgeTolerance) < menuScroll);
  2246. }
  2247. module.debug('Scrolling to active item', offset);
  2248. if(forceScroll || abovePage || belowPage) {
  2249. $menu.scrollTop(offset);
  2250. }
  2251. $menu.removeClass(className.loading);
  2252. }
  2253. },
  2254. text: function(text) {
  2255. if(settings.action !== 'select') {
  2256. if(settings.action == 'combo') {
  2257. module.debug('Changing combo button text', text, $combo);
  2258. if(settings.preserveHTML) {
  2259. $combo.html(text);
  2260. }
  2261. else {
  2262. $combo.text(text);
  2263. }
  2264. }
  2265. else {
  2266. if(text !== module.get.placeholderText()) {
  2267. $text.removeClass(className.placeholder);
  2268. }
  2269. module.debug('Changing text', text, $text);
  2270. $text
  2271. .removeClass(className.filtered)
  2272. ;
  2273. if(settings.preserveHTML) {
  2274. $text.html(text);
  2275. }
  2276. else {
  2277. $text.text(text);
  2278. }
  2279. }
  2280. }
  2281. },
  2282. selectedItem: function($item) {
  2283. var
  2284. value = module.get.choiceValue($item),
  2285. searchText = module.get.choiceText($item, false),
  2286. text = module.get.choiceText($item, true)
  2287. ;
  2288. module.debug('Setting user selection to item', $item);
  2289. module.remove.activeItem();
  2290. module.set.partialSearch(searchText);
  2291. module.set.activeItem($item);
  2292. module.set.selected(value, $item);
  2293. module.set.text(text);
  2294. },
  2295. selectedLetter: function(letter) {
  2296. var
  2297. $selectedItem = $item.filter('.' + className.selected),
  2298. alreadySelectedLetter = $selectedItem.length > 0 && module.has.firstLetter($selectedItem, letter),
  2299. $nextValue = false,
  2300. $nextItem
  2301. ;
  2302. // check next of same letter
  2303. if(alreadySelectedLetter) {
  2304. $nextItem = $selectedItem.nextAll($item).eq(0);
  2305. if( module.has.firstLetter($nextItem, letter) ) {
  2306. $nextValue = $nextItem;
  2307. }
  2308. }
  2309. // check all values
  2310. if(!$nextValue) {
  2311. $item
  2312. .each(function(){
  2313. if(module.has.firstLetter($(this), letter)) {
  2314. $nextValue = $(this);
  2315. return false;
  2316. }
  2317. })
  2318. ;
  2319. }
  2320. // set next value
  2321. if($nextValue) {
  2322. module.verbose('Scrolling to next value with letter', letter);
  2323. module.set.scrollPosition($nextValue);
  2324. $selectedItem.removeClass(className.selected);
  2325. $nextValue.addClass(className.selected);
  2326. if(settings.selectOnKeydown && module.is.single()) {
  2327. module.set.selectedItem($nextValue);
  2328. }
  2329. }
  2330. },
  2331. direction: function($menu) {
  2332. if(settings.direction == 'auto') {
  2333. // reset position
  2334. module.remove.upward();
  2335. if(module.can.openDownward($menu)) {
  2336. module.remove.upward($menu);
  2337. }
  2338. else {
  2339. module.set.upward($menu);
  2340. }
  2341. if(!module.is.leftward($menu) && !module.can.openRightward($menu)) {
  2342. module.set.leftward($menu);
  2343. }
  2344. }
  2345. else if(settings.direction == 'upward') {
  2346. module.set.upward($menu);
  2347. }
  2348. },
  2349. upward: function($currentMenu) {
  2350. var $element = $currentMenu || $module;
  2351. $element.addClass(className.upward);
  2352. },
  2353. leftward: function($currentMenu) {
  2354. var $element = $currentMenu || $menu;
  2355. $element.addClass(className.leftward);
  2356. },
  2357. value: function(value, text, $selected) {
  2358. var
  2359. escapedValue = module.escape.value(value),
  2360. hasInput = ($input.length > 0),
  2361. currentValue = module.get.values(),
  2362. stringValue = (value !== undefined)
  2363. ? String(value)
  2364. : value,
  2365. newValue
  2366. ;
  2367. if(hasInput) {
  2368. if(!settings.allowReselection && stringValue == currentValue) {
  2369. module.verbose('Skipping value update already same value', value, currentValue);
  2370. if(!module.is.initialLoad()) {
  2371. return;
  2372. }
  2373. }
  2374. if( module.is.single() && module.has.selectInput() && module.can.extendSelect() ) {
  2375. module.debug('Adding user option', value);
  2376. module.add.optionValue(value);
  2377. }
  2378. module.debug('Updating input value', escapedValue, currentValue);
  2379. internalChange = true;
  2380. $input
  2381. .val(escapedValue)
  2382. ;
  2383. if(settings.fireOnInit === false && module.is.initialLoad()) {
  2384. module.debug('Input native change event ignored on initial load');
  2385. }
  2386. else {
  2387. module.trigger.change();
  2388. }
  2389. internalChange = false;
  2390. }
  2391. else {
  2392. module.verbose('Storing value in metadata', escapedValue, $input);
  2393. if(escapedValue !== currentValue) {
  2394. $module.data(metadata.value, stringValue);
  2395. }
  2396. }
  2397. if(module.is.single() && settings.clearable) {
  2398. // treat undefined or '' as empty
  2399. if(!escapedValue) {
  2400. module.remove.clearable();
  2401. }
  2402. else {
  2403. module.set.clearable();
  2404. }
  2405. }
  2406. if(settings.fireOnInit === false && module.is.initialLoad()) {
  2407. module.verbose('No callback on initial load', settings.onChange);
  2408. }
  2409. else {
  2410. settings.onChange.call(element, value, text, $selected);
  2411. }
  2412. },
  2413. active: function() {
  2414. $module
  2415. .addClass(className.active)
  2416. ;
  2417. },
  2418. multiple: function() {
  2419. $module.addClass(className.multiple);
  2420. },
  2421. visible: function() {
  2422. $module.addClass(className.visible);
  2423. },
  2424. exactly: function(value, $selectedItem) {
  2425. module.debug('Setting selected to exact values');
  2426. module.clear();
  2427. module.set.selected(value, $selectedItem);
  2428. },
  2429. selected: function(value, $selectedItem) {
  2430. var
  2431. isMultiple = module.is.multiple(),
  2432. $userSelectedItem
  2433. ;
  2434. $selectedItem = (settings.allowAdditions)
  2435. ? $selectedItem || module.get.itemWithAdditions(value)
  2436. : $selectedItem || module.get.item(value)
  2437. ;
  2438. if(!$selectedItem) {
  2439. return;
  2440. }
  2441. module.debug('Setting selected menu item to', $selectedItem);
  2442. if(module.is.multiple()) {
  2443. module.remove.searchWidth();
  2444. }
  2445. if(module.is.single()) {
  2446. module.remove.activeItem();
  2447. module.remove.selectedItem();
  2448. }
  2449. else if(settings.useLabels) {
  2450. module.remove.selectedItem();
  2451. }
  2452. // select each item
  2453. $selectedItem
  2454. .each(function() {
  2455. var
  2456. $selected = $(this),
  2457. selectedText = module.get.choiceText($selected),
  2458. selectedValue = module.get.choiceValue($selected, selectedText),
  2459. isFiltered = $selected.hasClass(className.filtered),
  2460. isActive = $selected.hasClass(className.active),
  2461. isUserValue = $selected.hasClass(className.addition),
  2462. shouldAnimate = (isMultiple && $selectedItem.length == 1)
  2463. ;
  2464. if(isMultiple) {
  2465. if(!isActive || isUserValue) {
  2466. if(settings.apiSettings && settings.saveRemoteData) {
  2467. module.save.remoteData(selectedText, selectedValue);
  2468. }
  2469. if(settings.useLabels) {
  2470. module.add.label(selectedValue, selectedText, shouldAnimate);
  2471. module.add.value(selectedValue, selectedText, $selected);
  2472. module.set.activeItem($selected);
  2473. module.filterActive();
  2474. module.select.nextAvailable($selectedItem);
  2475. }
  2476. else {
  2477. module.add.value(selectedValue, selectedText, $selected);
  2478. module.set.text(module.add.variables(message.count));
  2479. module.set.activeItem($selected);
  2480. }
  2481. }
  2482. else if(!isFiltered) {
  2483. module.debug('Selected active value, removing label');
  2484. module.remove.selected(selectedValue);
  2485. }
  2486. }
  2487. else {
  2488. if(settings.apiSettings && settings.saveRemoteData) {
  2489. module.save.remoteData(selectedText, selectedValue);
  2490. }
  2491. module.set.text(selectedText);
  2492. module.set.value(selectedValue, selectedText, $selected);
  2493. $selected
  2494. .addClass(className.active)
  2495. .addClass(className.selected)
  2496. ;
  2497. }
  2498. })
  2499. ;
  2500. },
  2501. clearable: function() {
  2502. $icon.addClass(className.clear);
  2503. },
  2504. },
  2505. add: {
  2506. label: function(value, text, shouldAnimate) {
  2507. var
  2508. $next = module.is.searchSelection()
  2509. ? $search
  2510. : $text,
  2511. escapedValue = module.escape.value(value),
  2512. $label
  2513. ;
  2514. if(settings.ignoreCase) {
  2515. escapedValue = escapedValue.toLowerCase();
  2516. }
  2517. $label = $('<a />')
  2518. .addClass(className.label)
  2519. .attr('data-' + metadata.value, escapedValue)
  2520. .html(templates.label(escapedValue, text))
  2521. ;
  2522. $label = settings.onLabelCreate.call($label, escapedValue, text);
  2523. if(module.has.label(value)) {
  2524. module.debug('User selection already exists, skipping', escapedValue);
  2525. return;
  2526. }
  2527. if(settings.label.variation) {
  2528. $label.addClass(settings.label.variation);
  2529. }
  2530. if(shouldAnimate === true) {
  2531. module.debug('Animating in label', $label);
  2532. $label
  2533. .addClass(className.hidden)
  2534. .insertBefore($next)
  2535. .transition(settings.label.transition, settings.label.duration)
  2536. ;
  2537. }
  2538. else {
  2539. module.debug('Adding selection label', $label);
  2540. $label
  2541. .insertBefore($next)
  2542. ;
  2543. }
  2544. },
  2545. message: function(message) {
  2546. var
  2547. $message = $menu.children(selector.message),
  2548. html = settings.templates.message(module.add.variables(message))
  2549. ;
  2550. if($message.length > 0) {
  2551. $message
  2552. .html(html)
  2553. ;
  2554. }
  2555. else {
  2556. $message = $('<div/>')
  2557. .html(html)
  2558. .addClass(className.message)
  2559. .appendTo($menu)
  2560. ;
  2561. }
  2562. },
  2563. optionValue: function(value) {
  2564. var
  2565. escapedValue = module.escape.value(value),
  2566. $option = $input.find('option[value="' + module.escape.string(escapedValue) + '"]'),
  2567. hasOption = ($option.length > 0)
  2568. ;
  2569. if(hasOption) {
  2570. return;
  2571. }
  2572. // temporarily disconnect observer
  2573. module.disconnect.selectObserver();
  2574. if( module.is.single() ) {
  2575. module.verbose('Removing previous user addition');
  2576. $input.find('option.' + className.addition).remove();
  2577. }
  2578. $('<option/>')
  2579. .prop('value', escapedValue)
  2580. .addClass(className.addition)
  2581. .html(value)
  2582. .appendTo($input)
  2583. ;
  2584. module.verbose('Adding user addition as an <option>', value);
  2585. module.observe.select();
  2586. },
  2587. userSuggestion: function(value) {
  2588. var
  2589. $addition = $menu.children(selector.addition),
  2590. $existingItem = module.get.item(value),
  2591. alreadyHasValue = $existingItem && $existingItem.not(selector.addition).length,
  2592. hasUserSuggestion = $addition.length > 0,
  2593. html
  2594. ;
  2595. if(settings.useLabels && module.has.maxSelections()) {
  2596. return;
  2597. }
  2598. if(value === '' || alreadyHasValue) {
  2599. $addition.remove();
  2600. return;
  2601. }
  2602. if(hasUserSuggestion) {
  2603. $addition
  2604. .data(metadata.value, value)
  2605. .data(metadata.text, value)
  2606. .attr('data-' + metadata.value, value)
  2607. .attr('data-' + metadata.text, value)
  2608. .removeClass(className.filtered)
  2609. ;
  2610. if(!settings.hideAdditions) {
  2611. html = settings.templates.addition( module.add.variables(message.addResult, value) );
  2612. $addition
  2613. .html(html)
  2614. ;
  2615. }
  2616. module.verbose('Replacing user suggestion with new value', $addition);
  2617. }
  2618. else {
  2619. $addition = module.create.userChoice(value);
  2620. $addition
  2621. .prependTo($menu)
  2622. ;
  2623. module.verbose('Adding item choice to menu corresponding with user choice addition', $addition);
  2624. }
  2625. if(!settings.hideAdditions || module.is.allFiltered()) {
  2626. $addition
  2627. .addClass(className.selected)
  2628. .siblings()
  2629. .removeClass(className.selected)
  2630. ;
  2631. }
  2632. module.refreshItems();
  2633. },
  2634. variables: function(message, term) {
  2635. var
  2636. hasCount = (message.search('{count}') !== -1),
  2637. hasMaxCount = (message.search('{maxCount}') !== -1),
  2638. hasTerm = (message.search('{term}') !== -1),
  2639. values,
  2640. count,
  2641. query
  2642. ;
  2643. module.verbose('Adding templated variables to message', message);
  2644. if(hasCount) {
  2645. count = module.get.selectionCount();
  2646. message = message.replace('{count}', count);
  2647. }
  2648. if(hasMaxCount) {
  2649. count = module.get.selectionCount();
  2650. message = message.replace('{maxCount}', settings.maxSelections);
  2651. }
  2652. if(hasTerm) {
  2653. query = term || module.get.query();
  2654. message = message.replace('{term}', query);
  2655. }
  2656. return message;
  2657. },
  2658. value: function(addedValue, addedText, $selectedItem) {
  2659. var
  2660. currentValue = module.get.values(),
  2661. newValue
  2662. ;
  2663. if(module.has.value(addedValue)) {
  2664. module.debug('Value already selected');
  2665. return;
  2666. }
  2667. if(addedValue === '') {
  2668. module.debug('Cannot select blank values from multiselect');
  2669. return;
  2670. }
  2671. // extend current array
  2672. if($.isArray(currentValue)) {
  2673. newValue = currentValue.concat([addedValue]);
  2674. newValue = module.get.uniqueArray(newValue);
  2675. }
  2676. else {
  2677. newValue = [addedValue];
  2678. }
  2679. // add values
  2680. if( module.has.selectInput() ) {
  2681. if(module.can.extendSelect()) {
  2682. module.debug('Adding value to select', addedValue, newValue, $input);
  2683. module.add.optionValue(addedValue);
  2684. }
  2685. }
  2686. else {
  2687. newValue = newValue.join(settings.delimiter);
  2688. module.debug('Setting hidden input to delimited value', newValue, $input);
  2689. }
  2690. if(settings.fireOnInit === false && module.is.initialLoad()) {
  2691. module.verbose('Skipping onadd callback on initial load', settings.onAdd);
  2692. }
  2693. else {
  2694. settings.onAdd.call(element, addedValue, addedText, $selectedItem);
  2695. }
  2696. module.set.value(newValue, addedValue, addedText, $selectedItem);
  2697. module.check.maxSelections();
  2698. },
  2699. },
  2700. remove: {
  2701. active: function() {
  2702. $module.removeClass(className.active);
  2703. },
  2704. activeLabel: function() {
  2705. $module.find(selector.label).removeClass(className.active);
  2706. },
  2707. empty: function() {
  2708. $module.removeClass(className.empty);
  2709. },
  2710. loading: function() {
  2711. $module.removeClass(className.loading);
  2712. },
  2713. initialLoad: function() {
  2714. initialLoad = false;
  2715. },
  2716. upward: function($currentMenu) {
  2717. var $element = $currentMenu || $module;
  2718. $element.removeClass(className.upward);
  2719. },
  2720. leftward: function($currentMenu) {
  2721. var $element = $currentMenu || $menu;
  2722. $element.removeClass(className.leftward);
  2723. },
  2724. visible: function() {
  2725. $module.removeClass(className.visible);
  2726. },
  2727. activeItem: function() {
  2728. $item.removeClass(className.active);
  2729. },
  2730. filteredItem: function() {
  2731. if(settings.useLabels && module.has.maxSelections() ) {
  2732. return;
  2733. }
  2734. if(settings.useLabels && module.is.multiple()) {
  2735. $item.not('.' + className.active).removeClass(className.filtered);
  2736. }
  2737. else {
  2738. $item.removeClass(className.filtered);
  2739. }
  2740. module.remove.empty();
  2741. },
  2742. optionValue: function(value) {
  2743. var
  2744. escapedValue = module.escape.value(value),
  2745. $option = $input.find('option[value="' + module.escape.string(escapedValue) + '"]'),
  2746. hasOption = ($option.length > 0)
  2747. ;
  2748. if(!hasOption || !$option.hasClass(className.addition)) {
  2749. return;
  2750. }
  2751. // temporarily disconnect observer
  2752. if(selectObserver) {
  2753. selectObserver.disconnect();
  2754. module.verbose('Temporarily disconnecting mutation observer');
  2755. }
  2756. $option.remove();
  2757. module.verbose('Removing user addition as an <option>', escapedValue);
  2758. if(selectObserver) {
  2759. selectObserver.observe($input[0], {
  2760. childList : true,
  2761. subtree : true
  2762. });
  2763. }
  2764. },
  2765. message: function() {
  2766. $menu.children(selector.message).remove();
  2767. },
  2768. searchWidth: function() {
  2769. $search.css('width', '');
  2770. },
  2771. searchTerm: function() {
  2772. module.verbose('Cleared search term');
  2773. $search.val('');
  2774. module.set.filtered();
  2775. },
  2776. userAddition: function() {
  2777. $item.filter(selector.addition).remove();
  2778. },
  2779. selected: function(value, $selectedItem) {
  2780. $selectedItem = (settings.allowAdditions)
  2781. ? $selectedItem || module.get.itemWithAdditions(value)
  2782. : $selectedItem || module.get.item(value)
  2783. ;
  2784. if(!$selectedItem) {
  2785. return false;
  2786. }
  2787. $selectedItem
  2788. .each(function() {
  2789. var
  2790. $selected = $(this),
  2791. selectedText = module.get.choiceText($selected),
  2792. selectedValue = module.get.choiceValue($selected, selectedText)
  2793. ;
  2794. if(module.is.multiple()) {
  2795. if(settings.useLabels) {
  2796. module.remove.value(selectedValue, selectedText, $selected);
  2797. module.remove.label(selectedValue);
  2798. }
  2799. else {
  2800. module.remove.value(selectedValue, selectedText, $selected);
  2801. if(module.get.selectionCount() === 0) {
  2802. module.set.placeholderText();
  2803. }
  2804. else {
  2805. module.set.text(module.add.variables(message.count));
  2806. }
  2807. }
  2808. }
  2809. else {
  2810. module.remove.value(selectedValue, selectedText, $selected);
  2811. }
  2812. $selected
  2813. .removeClass(className.filtered)
  2814. .removeClass(className.active)
  2815. ;
  2816. if(settings.useLabels) {
  2817. $selected.removeClass(className.selected);
  2818. }
  2819. })
  2820. ;
  2821. },
  2822. selectedItem: function() {
  2823. $item.removeClass(className.selected);
  2824. },
  2825. value: function(removedValue, removedText, $removedItem) {
  2826. var
  2827. values = module.get.values(),
  2828. newValue
  2829. ;
  2830. if( module.has.selectInput() ) {
  2831. module.verbose('Input is <select> removing selected option', removedValue);
  2832. newValue = module.remove.arrayValue(removedValue, values);
  2833. module.remove.optionValue(removedValue);
  2834. }
  2835. else {
  2836. module.verbose('Removing from delimited values', removedValue);
  2837. newValue = module.remove.arrayValue(removedValue, values);
  2838. newValue = newValue.join(settings.delimiter);
  2839. }
  2840. if(settings.fireOnInit === false && module.is.initialLoad()) {
  2841. module.verbose('No callback on initial load', settings.onRemove);
  2842. }
  2843. else {
  2844. settings.onRemove.call(element, removedValue, removedText, $removedItem);
  2845. }
  2846. module.set.value(newValue, removedText, $removedItem);
  2847. module.check.maxSelections();
  2848. },
  2849. arrayValue: function(removedValue, values) {
  2850. if( !$.isArray(values) ) {
  2851. values = [values];
  2852. }
  2853. values = $.grep(values, function(value){
  2854. return (removedValue != value);
  2855. });
  2856. module.verbose('Removed value from delimited string', removedValue, values);
  2857. return values;
  2858. },
  2859. label: function(value, shouldAnimate) {
  2860. var
  2861. $labels = $module.find(selector.label),
  2862. $removedLabel = $labels.filter('[data-' + metadata.value + '="' + module.escape.string(value) +'"]')
  2863. ;
  2864. module.verbose('Removing label', $removedLabel);
  2865. $removedLabel.remove();
  2866. },
  2867. activeLabels: function($activeLabels) {
  2868. $activeLabels = $activeLabels || $module.find(selector.label).filter('.' + className.active);
  2869. module.verbose('Removing active label selections', $activeLabels);
  2870. module.remove.labels($activeLabels);
  2871. },
  2872. labels: function($labels) {
  2873. $labels = $labels || $module.find(selector.label);
  2874. module.verbose('Removing labels', $labels);
  2875. $labels
  2876. .each(function(){
  2877. var
  2878. $label = $(this),
  2879. value = $label.data(metadata.value),
  2880. stringValue = (value !== undefined)
  2881. ? String(value)
  2882. : value,
  2883. isUserValue = module.is.userValue(stringValue)
  2884. ;
  2885. if(settings.onLabelRemove.call($label, value) === false) {
  2886. module.debug('Label remove callback cancelled removal');
  2887. return;
  2888. }
  2889. module.remove.message();
  2890. if(isUserValue) {
  2891. module.remove.value(stringValue);
  2892. module.remove.label(stringValue);
  2893. }
  2894. else {
  2895. // selected will also remove label
  2896. module.remove.selected(stringValue);
  2897. }
  2898. })
  2899. ;
  2900. },
  2901. tabbable: function() {
  2902. if( module.is.searchSelection() ) {
  2903. module.debug('Searchable dropdown initialized');
  2904. $search
  2905. .removeAttr('tabindex')
  2906. ;
  2907. $menu
  2908. .removeAttr('tabindex')
  2909. ;
  2910. }
  2911. else {
  2912. module.debug('Simple selection dropdown initialized');
  2913. $module
  2914. .removeAttr('tabindex')
  2915. ;
  2916. $menu
  2917. .removeAttr('tabindex')
  2918. ;
  2919. }
  2920. },
  2921. clearable: function() {
  2922. $icon.removeClass(className.clear);
  2923. }
  2924. },
  2925. has: {
  2926. menuSearch: function() {
  2927. return (module.has.search() && $search.closest($menu).length > 0);
  2928. },
  2929. search: function() {
  2930. return ($search.length > 0);
  2931. },
  2932. sizer: function() {
  2933. return ($sizer.length > 0);
  2934. },
  2935. selectInput: function() {
  2936. return ( $input.is('select') );
  2937. },
  2938. minCharacters: function(searchTerm) {
  2939. if(settings.minCharacters) {
  2940. searchTerm = (searchTerm !== undefined)
  2941. ? String(searchTerm)
  2942. : String(module.get.query())
  2943. ;
  2944. return (searchTerm.length >= settings.minCharacters);
  2945. }
  2946. return true;
  2947. },
  2948. firstLetter: function($item, letter) {
  2949. var
  2950. text,
  2951. firstLetter
  2952. ;
  2953. if(!$item || $item.length === 0 || typeof letter !== 'string') {
  2954. return false;
  2955. }
  2956. text = module.get.choiceText($item, false);
  2957. letter = letter.toLowerCase();
  2958. firstLetter = String(text).charAt(0).toLowerCase();
  2959. return (letter == firstLetter);
  2960. },
  2961. input: function() {
  2962. return ($input.length > 0);
  2963. },
  2964. items: function() {
  2965. return ($item.length > 0);
  2966. },
  2967. menu: function() {
  2968. return ($menu.length > 0);
  2969. },
  2970. message: function() {
  2971. return ($menu.children(selector.message).length !== 0);
  2972. },
  2973. label: function(value) {
  2974. var
  2975. escapedValue = module.escape.value(value),
  2976. $labels = $module.find(selector.label)
  2977. ;
  2978. if(settings.ignoreCase) {
  2979. escapedValue = escapedValue.toLowerCase();
  2980. }
  2981. return ($labels.filter('[data-' + metadata.value + '="' + module.escape.string(escapedValue) +'"]').length > 0);
  2982. },
  2983. maxSelections: function() {
  2984. return (settings.maxSelections && module.get.selectionCount() >= settings.maxSelections);
  2985. },
  2986. allResultsFiltered: function() {
  2987. var
  2988. $normalResults = $item.not(selector.addition)
  2989. ;
  2990. return ($normalResults.filter(selector.unselectable).length === $normalResults.length);
  2991. },
  2992. userSuggestion: function() {
  2993. return ($menu.children(selector.addition).length > 0);
  2994. },
  2995. query: function() {
  2996. return (module.get.query() !== '');
  2997. },
  2998. value: function(value) {
  2999. return (settings.ignoreCase)
  3000. ? module.has.valueIgnoringCase(value)
  3001. : module.has.valueMatchingCase(value)
  3002. ;
  3003. },
  3004. valueMatchingCase: function(value) {
  3005. var
  3006. values = module.get.values(),
  3007. hasValue = $.isArray(values)
  3008. ? values && ($.inArray(value, values) !== -1)
  3009. : (values == value)
  3010. ;
  3011. return (hasValue)
  3012. ? true
  3013. : false
  3014. ;
  3015. },
  3016. valueIgnoringCase: function(value) {
  3017. var
  3018. values = module.get.values(),
  3019. hasValue = false
  3020. ;
  3021. if(!$.isArray(values)) {
  3022. values = [values];
  3023. }
  3024. $.each(values, function(index, existingValue) {
  3025. if(String(value).toLowerCase() == String(existingValue).toLowerCase()) {
  3026. hasValue = true;
  3027. return false;
  3028. }
  3029. });
  3030. return hasValue;
  3031. }
  3032. },
  3033. is: {
  3034. active: function() {
  3035. return $module.hasClass(className.active);
  3036. },
  3037. animatingInward: function() {
  3038. return $menu.transition('is inward');
  3039. },
  3040. animatingOutward: function() {
  3041. return $menu.transition('is outward');
  3042. },
  3043. bubbledLabelClick: function(event) {
  3044. return $(event.target).is('select, input') && $module.closest('label').length > 0;
  3045. },
  3046. bubbledIconClick: function(event) {
  3047. return $(event.target).closest($icon).length > 0;
  3048. },
  3049. alreadySetup: function() {
  3050. return ($module.is('select') && $module.parent(selector.dropdown).data(moduleNamespace) !== undefined && $module.prev().length === 0);
  3051. },
  3052. animating: function($subMenu) {
  3053. return ($subMenu)
  3054. ? $subMenu.transition && $subMenu.transition('is animating')
  3055. : $menu.transition && $menu.transition('is animating')
  3056. ;
  3057. },
  3058. leftward: function($subMenu) {
  3059. var $selectedMenu = $subMenu || $menu;
  3060. return $selectedMenu.hasClass(className.leftward);
  3061. },
  3062. disabled: function() {
  3063. return $module.hasClass(className.disabled);
  3064. },
  3065. focused: function() {
  3066. return (document.activeElement === $module[0]);
  3067. },
  3068. focusedOnSearch: function() {
  3069. return (document.activeElement === $search[0]);
  3070. },
  3071. allFiltered: function() {
  3072. return( (module.is.multiple() || module.has.search()) && !(settings.hideAdditions == false && module.has.userSuggestion()) && !module.has.message() && module.has.allResultsFiltered() );
  3073. },
  3074. hidden: function($subMenu) {
  3075. return !module.is.visible($subMenu);
  3076. },
  3077. initialLoad: function() {
  3078. return initialLoad;
  3079. },
  3080. inObject: function(needle, object) {
  3081. var
  3082. found = false
  3083. ;
  3084. $.each(object, function(index, property) {
  3085. if(property == needle) {
  3086. found = true;
  3087. return true;
  3088. }
  3089. });
  3090. return found;
  3091. },
  3092. multiple: function() {
  3093. return $module.hasClass(className.multiple);
  3094. },
  3095. remote: function() {
  3096. return settings.apiSettings && module.can.useAPI();
  3097. },
  3098. single: function() {
  3099. return !module.is.multiple();
  3100. },
  3101. selectMutation: function(mutations) {
  3102. var
  3103. selectChanged = false
  3104. ;
  3105. $.each(mutations, function(index, mutation) {
  3106. if(mutation.target && $(mutation.target).is('select')) {
  3107. selectChanged = true;
  3108. return true;
  3109. }
  3110. });
  3111. return selectChanged;
  3112. },
  3113. search: function() {
  3114. return $module.hasClass(className.search);
  3115. },
  3116. searchSelection: function() {
  3117. return ( module.has.search() && $search.parent(selector.dropdown).length === 1 );
  3118. },
  3119. selection: function() {
  3120. return $module.hasClass(className.selection);
  3121. },
  3122. userValue: function(value) {
  3123. return ($.inArray(value, module.get.userValues()) !== -1);
  3124. },
  3125. upward: function($menu) {
  3126. var $element = $menu || $module;
  3127. return $element.hasClass(className.upward);
  3128. },
  3129. visible: function($subMenu) {
  3130. return ($subMenu)
  3131. ? $subMenu.hasClass(className.visible)
  3132. : $menu.hasClass(className.visible)
  3133. ;
  3134. },
  3135. verticallyScrollableContext: function() {
  3136. var
  3137. overflowY = ($context.get(0) !== window)
  3138. ? $context.css('overflow-y')
  3139. : false
  3140. ;
  3141. return (overflowY == 'auto' || overflowY == 'scroll');
  3142. },
  3143. horizontallyScrollableContext: function() {
  3144. var
  3145. overflowX = ($context.get(0) !== window)
  3146. ? $context.css('overflow-X')
  3147. : false
  3148. ;
  3149. return (overflowX == 'auto' || overflowX == 'scroll');
  3150. }
  3151. },
  3152. can: {
  3153. activate: function($item) {
  3154. if(settings.useLabels) {
  3155. return true;
  3156. }
  3157. if(!module.has.maxSelections()) {
  3158. return true;
  3159. }
  3160. if(module.has.maxSelections() && $item.hasClass(className.active)) {
  3161. return true;
  3162. }
  3163. return false;
  3164. },
  3165. openDownward: function($subMenu) {
  3166. var
  3167. $currentMenu = $subMenu || $menu,
  3168. canOpenDownward = true,
  3169. onScreen = {},
  3170. calculations
  3171. ;
  3172. $currentMenu
  3173. .addClass(className.loading)
  3174. ;
  3175. calculations = {
  3176. context: {
  3177. offset : ($context.get(0) === window)
  3178. ? { top: 0, left: 0}
  3179. : $context.offset(),
  3180. scrollTop : $context.scrollTop(),
  3181. height : $context.outerHeight()
  3182. },
  3183. menu : {
  3184. offset: $currentMenu.offset(),
  3185. height: $currentMenu.outerHeight()
  3186. }
  3187. };
  3188. if(module.is.verticallyScrollableContext()) {
  3189. calculations.menu.offset.top += calculations.context.scrollTop;
  3190. }
  3191. onScreen = {
  3192. above : (calculations.context.scrollTop) <= calculations.menu.offset.top - calculations.context.offset.top - calculations.menu.height,
  3193. below : (calculations.context.scrollTop + calculations.context.height) >= calculations.menu.offset.top - calculations.context.offset.top + calculations.menu.height
  3194. };
  3195. if(onScreen.below) {
  3196. module.verbose('Dropdown can fit in context downward', onScreen);
  3197. canOpenDownward = true;
  3198. }
  3199. else if(!onScreen.below && !onScreen.above) {
  3200. module.verbose('Dropdown cannot fit in either direction, favoring downward', onScreen);
  3201. canOpenDownward = true;
  3202. }
  3203. else {
  3204. module.verbose('Dropdown cannot fit below, opening upward', onScreen);
  3205. canOpenDownward = false;
  3206. }
  3207. $currentMenu.removeClass(className.loading);
  3208. return canOpenDownward;
  3209. },
  3210. openRightward: function($subMenu) {
  3211. var
  3212. $currentMenu = $subMenu || $menu,
  3213. canOpenRightward = true,
  3214. isOffscreenRight = false,
  3215. calculations
  3216. ;
  3217. $currentMenu
  3218. .addClass(className.loading)
  3219. ;
  3220. calculations = {
  3221. context: {
  3222. offset : ($context.get(0) === window)
  3223. ? { top: 0, left: 0}
  3224. : $context.offset(),
  3225. scrollLeft : $context.scrollLeft(),
  3226. width : $context.outerWidth()
  3227. },
  3228. menu: {
  3229. offset : $currentMenu.offset(),
  3230. width : $currentMenu.outerWidth()
  3231. }
  3232. };
  3233. if(module.is.horizontallyScrollableContext()) {
  3234. calculations.menu.offset.left += calculations.context.scrollLeft;
  3235. }
  3236. isOffscreenRight = (calculations.menu.offset.left - calculations.context.offset.left + calculations.menu.width >= calculations.context.scrollLeft + calculations.context.width);
  3237. if(isOffscreenRight) {
  3238. module.verbose('Dropdown cannot fit in context rightward', isOffscreenRight);
  3239. canOpenRightward = false;
  3240. }
  3241. $currentMenu.removeClass(className.loading);
  3242. return canOpenRightward;
  3243. },
  3244. click: function() {
  3245. return (hasTouch || settings.on == 'click');
  3246. },
  3247. extendSelect: function() {
  3248. return settings.allowAdditions || settings.apiSettings;
  3249. },
  3250. show: function() {
  3251. return !module.is.disabled() && (module.has.items() || module.has.message());
  3252. },
  3253. useAPI: function() {
  3254. return $.fn.api !== undefined;
  3255. }
  3256. },
  3257. animate: {
  3258. show: function(callback, $subMenu) {
  3259. var
  3260. $currentMenu = $subMenu || $menu,
  3261. start = ($subMenu)
  3262. ? function() {}
  3263. : function() {
  3264. module.hideSubMenus();
  3265. module.hideOthers();
  3266. module.set.active();
  3267. },
  3268. transition
  3269. ;
  3270. callback = $.isFunction(callback)
  3271. ? callback
  3272. : function(){}
  3273. ;
  3274. module.verbose('Doing menu show animation', $currentMenu);
  3275. module.set.direction($subMenu);
  3276. transition = module.get.transition($subMenu);
  3277. if( module.is.selection() ) {
  3278. module.set.scrollPosition(module.get.selectedItem(), true);
  3279. }
  3280. if( module.is.hidden($currentMenu) || module.is.animating($currentMenu) ) {
  3281. if(transition == 'none') {
  3282. start();
  3283. $currentMenu.transition('show');
  3284. callback.call(element);
  3285. }
  3286. else if($.fn.transition !== undefined && $module.transition('is supported')) {
  3287. $currentMenu
  3288. .transition({
  3289. animation : transition + ' in',
  3290. debug : settings.debug,
  3291. verbose : settings.verbose,
  3292. duration : settings.duration,
  3293. queue : true,
  3294. onStart : start,
  3295. onComplete : function() {
  3296. callback.call(element);
  3297. }
  3298. })
  3299. ;
  3300. }
  3301. else {
  3302. module.error(error.noTransition, transition);
  3303. }
  3304. }
  3305. },
  3306. hide: function(callback, $subMenu) {
  3307. var
  3308. $currentMenu = $subMenu || $menu,
  3309. duration = ($subMenu)
  3310. ? (settings.duration * 0.9)
  3311. : settings.duration,
  3312. start = ($subMenu)
  3313. ? function() {}
  3314. : function() {
  3315. if( module.can.click() ) {
  3316. module.unbind.intent();
  3317. }
  3318. module.remove.active();
  3319. },
  3320. transition = module.get.transition($subMenu)
  3321. ;
  3322. callback = $.isFunction(callback)
  3323. ? callback
  3324. : function(){}
  3325. ;
  3326. if( module.is.visible($currentMenu) || module.is.animating($currentMenu) ) {
  3327. module.verbose('Doing menu hide animation', $currentMenu);
  3328. if(transition == 'none') {
  3329. start();
  3330. $currentMenu.transition('hide');
  3331. callback.call(element);
  3332. }
  3333. else if($.fn.transition !== undefined && $module.transition('is supported')) {
  3334. $currentMenu
  3335. .transition({
  3336. animation : transition + ' out',
  3337. duration : settings.duration,
  3338. debug : settings.debug,
  3339. verbose : settings.verbose,
  3340. queue : false,
  3341. onStart : start,
  3342. onComplete : function() {
  3343. callback.call(element);
  3344. }
  3345. })
  3346. ;
  3347. }
  3348. else {
  3349. module.error(error.transition);
  3350. }
  3351. }
  3352. }
  3353. },
  3354. hideAndClear: function() {
  3355. module.remove.searchTerm();
  3356. if( module.has.maxSelections() ) {
  3357. return;
  3358. }
  3359. if(module.has.search()) {
  3360. module.hide(function() {
  3361. module.remove.filteredItem();
  3362. });
  3363. }
  3364. else {
  3365. module.hide();
  3366. }
  3367. },
  3368. delay: {
  3369. show: function() {
  3370. module.verbose('Delaying show event to ensure user intent');
  3371. clearTimeout(module.timer);
  3372. module.timer = setTimeout(module.show, settings.delay.show);
  3373. },
  3374. hide: function() {
  3375. module.verbose('Delaying hide event to ensure user intent');
  3376. clearTimeout(module.timer);
  3377. module.timer = setTimeout(module.hide, settings.delay.hide);
  3378. }
  3379. },
  3380. escape: {
  3381. value: function(value) {
  3382. var
  3383. multipleValues = $.isArray(value),
  3384. stringValue = (typeof value === 'string'),
  3385. isUnparsable = (!stringValue && !multipleValues),
  3386. hasQuotes = (stringValue && value.search(regExp.quote) !== -1),
  3387. values = []
  3388. ;
  3389. if(isUnparsable || !hasQuotes) {
  3390. return value;
  3391. }
  3392. module.debug('Encoding quote values for use in select', value);
  3393. if(multipleValues) {
  3394. $.each(value, function(index, value){
  3395. values.push(value.replace(regExp.quote, '&quot;'));
  3396. });
  3397. return values;
  3398. }
  3399. return value.replace(regExp.quote, '&quot;');
  3400. },
  3401. string: function(text) {
  3402. text = String(text);
  3403. return text.replace(regExp.escape, '\\$&');
  3404. }
  3405. },
  3406. setting: function(name, value) {
  3407. module.debug('Changing setting', name, value);
  3408. if( $.isPlainObject(name) ) {
  3409. $.extend(true, settings, name);
  3410. }
  3411. else if(value !== undefined) {
  3412. if($.isPlainObject(settings[name])) {
  3413. $.extend(true, settings[name], value);
  3414. }
  3415. else {
  3416. settings[name] = value;
  3417. }
  3418. }
  3419. else {
  3420. return settings[name];
  3421. }
  3422. },
  3423. internal: function(name, value) {
  3424. if( $.isPlainObject(name) ) {
  3425. $.extend(true, module, name);
  3426. }
  3427. else if(value !== undefined) {
  3428. module[name] = value;
  3429. }
  3430. else {
  3431. return module[name];
  3432. }
  3433. },
  3434. debug: function() {
  3435. if(!settings.silent && settings.debug) {
  3436. if(settings.performance) {
  3437. module.performance.log(arguments);
  3438. }
  3439. else {
  3440. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  3441. module.debug.apply(console, arguments);
  3442. }
  3443. }
  3444. },
  3445. verbose: function() {
  3446. if(!settings.silent && settings.verbose && settings.debug) {
  3447. if(settings.performance) {
  3448. module.performance.log(arguments);
  3449. }
  3450. else {
  3451. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  3452. module.verbose.apply(console, arguments);
  3453. }
  3454. }
  3455. },
  3456. error: function() {
  3457. if(!settings.silent) {
  3458. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  3459. module.error.apply(console, arguments);
  3460. }
  3461. },
  3462. performance: {
  3463. log: function(message) {
  3464. var
  3465. currentTime,
  3466. executionTime,
  3467. previousTime
  3468. ;
  3469. if(settings.performance) {
  3470. currentTime = new Date().getTime();
  3471. previousTime = time || currentTime;
  3472. executionTime = currentTime - previousTime;
  3473. time = currentTime;
  3474. performance.push({
  3475. 'Name' : message[0],
  3476. 'Arguments' : [].slice.call(message, 1) || '',
  3477. 'Element' : element,
  3478. 'Execution Time' : executionTime
  3479. });
  3480. }
  3481. clearTimeout(module.performance.timer);
  3482. module.performance.timer = setTimeout(module.performance.display, 500);
  3483. },
  3484. display: function() {
  3485. var
  3486. title = settings.name + ':',
  3487. totalTime = 0
  3488. ;
  3489. time = false;
  3490. clearTimeout(module.performance.timer);
  3491. $.each(performance, function(index, data) {
  3492. totalTime += data['Execution Time'];
  3493. });
  3494. title += ' ' + totalTime + 'ms';
  3495. if(moduleSelector) {
  3496. title += ' \'' + moduleSelector + '\'';
  3497. }
  3498. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  3499. console.groupCollapsed(title);
  3500. if(console.table) {
  3501. console.table(performance);
  3502. }
  3503. else {
  3504. $.each(performance, function(index, data) {
  3505. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  3506. });
  3507. }
  3508. console.groupEnd();
  3509. }
  3510. performance = [];
  3511. }
  3512. },
  3513. invoke: function(query, passedArguments, context) {
  3514. var
  3515. object = instance,
  3516. maxDepth,
  3517. found,
  3518. response
  3519. ;
  3520. passedArguments = passedArguments || queryArguments;
  3521. context = element || context;
  3522. if(typeof query == 'string' && object !== undefined) {
  3523. query = query.split(/[\. ]/);
  3524. maxDepth = query.length - 1;
  3525. $.each(query, function(depth, value) {
  3526. var camelCaseValue = (depth != maxDepth)
  3527. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  3528. : query
  3529. ;
  3530. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  3531. object = object[camelCaseValue];
  3532. }
  3533. else if( object[camelCaseValue] !== undefined ) {
  3534. found = object[camelCaseValue];
  3535. return false;
  3536. }
  3537. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  3538. object = object[value];
  3539. }
  3540. else if( object[value] !== undefined ) {
  3541. found = object[value];
  3542. return false;
  3543. }
  3544. else {
  3545. module.error(error.method, query);
  3546. return false;
  3547. }
  3548. });
  3549. }
  3550. if ( $.isFunction( found ) ) {
  3551. response = found.apply(context, passedArguments);
  3552. }
  3553. else if(found !== undefined) {
  3554. response = found;
  3555. }
  3556. if($.isArray(returnedValue)) {
  3557. returnedValue.push(response);
  3558. }
  3559. else if(returnedValue !== undefined) {
  3560. returnedValue = [returnedValue, response];
  3561. }
  3562. else if(response !== undefined) {
  3563. returnedValue = response;
  3564. }
  3565. return found;
  3566. }
  3567. };
  3568. if(methodInvoked) {
  3569. if(instance === undefined) {
  3570. module.initialize();
  3571. }
  3572. module.invoke(query);
  3573. }
  3574. else {
  3575. if(instance !== undefined) {
  3576. instance.invoke('destroy');
  3577. }
  3578. module.initialize();
  3579. }
  3580. })
  3581. ;
  3582. return (returnedValue !== undefined)
  3583. ? returnedValue
  3584. : $allModules
  3585. ;
  3586. };
  3587. $.fn.dropdown.settings = {
  3588. silent : false,
  3589. debug : false,
  3590. verbose : false,
  3591. performance : true,
  3592. on : 'click', // what event should show menu action on item selection
  3593. action : 'activate', // action on item selection (nothing, activate, select, combo, hide, function(){})
  3594. values : false, // specify values to use for dropdown
  3595. clearable : false, // whether the value of the dropdown can be cleared
  3596. apiSettings : false,
  3597. selectOnKeydown : true, // Whether selection should occur automatically when keyboard shortcuts used
  3598. minCharacters : 0, // Minimum characters required to trigger API call
  3599. filterRemoteData : false, // Whether API results should be filtered after being returned for query term
  3600. saveRemoteData : true, // Whether remote name/value pairs should be stored in sessionStorage to allow remote data to be restored on page refresh
  3601. throttle : 200, // How long to wait after last user input to search remotely
  3602. context : window, // Context to use when determining if on screen
  3603. direction : 'auto', // Whether dropdown should always open in one direction
  3604. keepOnScreen : true, // Whether dropdown should check whether it is on screen before showing
  3605. match : 'both', // what to match against with search selection (both, text, or label)
  3606. fullTextSearch : false, // search anywhere in value (set to 'exact' to require exact matches)
  3607. placeholder : 'auto', // whether to convert blank <select> values to placeholder text
  3608. preserveHTML : true, // preserve html when selecting value
  3609. sortSelect : false, // sort selection on init
  3610. forceSelection : true, // force a choice on blur with search selection
  3611. allowAdditions : false, // whether multiple select should allow user added values
  3612. ignoreCase : false, // whether to consider values not matching in case to be the same
  3613. hideAdditions : true, // whether or not to hide special message prompting a user they can enter a value
  3614. maxSelections : false, // When set to a number limits the number of selections to this count
  3615. useLabels : true, // whether multiple select should filter currently active selections from choices
  3616. delimiter : ',', // when multiselect uses normal <input> the values will be delimited with this character
  3617. showOnFocus : true, // show menu on focus
  3618. allowReselection : false, // whether current value should trigger callbacks when reselected
  3619. allowTab : true, // add tabindex to element
  3620. allowCategorySelection : false, // allow elements with sub-menus to be selected
  3621. fireOnInit : false, // Whether callbacks should fire when initializing dropdown values
  3622. transition : 'auto', // auto transition will slide down or up based on direction
  3623. duration : 200, // duration of transition
  3624. glyphWidth : 1.037, // widest glyph width in em (W is 1.037 em) used to calculate multiselect input width
  3625. // label settings on multi-select
  3626. label: {
  3627. transition : 'scale',
  3628. duration : 200,
  3629. variation : false
  3630. },
  3631. // delay before event
  3632. delay : {
  3633. hide : 300,
  3634. show : 200,
  3635. search : 20,
  3636. touch : 50
  3637. },
  3638. /* Callbacks */
  3639. onChange : function(value, text, $selected){},
  3640. onAdd : function(value, text, $selected){},
  3641. onRemove : function(value, text, $selected){},
  3642. onLabelSelect : function($selectedLabels){},
  3643. onLabelCreate : function(value, text) { return $(this); },
  3644. onLabelRemove : function(value) { return true; },
  3645. onNoResults : function(searchTerm) { return true; },
  3646. onShow : function(){},
  3647. onHide : function(){},
  3648. /* Component */
  3649. name : 'Dropdown',
  3650. namespace : 'dropdown',
  3651. message: {
  3652. addResult : 'Add <b>{term}</b>',
  3653. count : '{count} selected',
  3654. maxSelections : 'Max {maxCount} selections',
  3655. noResults : 'No results found.',
  3656. serverError : 'There was an error contacting the server'
  3657. },
  3658. error : {
  3659. action : 'You called a dropdown action that was not defined',
  3660. alreadySetup : 'Once a select has been initialized behaviors must be called on the created ui dropdown',
  3661. labels : 'Allowing user additions currently requires the use of labels.',
  3662. missingMultiple : '<select> requires multiple property to be set to correctly preserve multiple values',
  3663. method : 'The method you called is not defined.',
  3664. noAPI : 'The API module is required to load resources remotely',
  3665. noStorage : 'Saving remote data requires session storage',
  3666. noTransition : 'This module requires ui transitions <https://github.com/Semantic-Org/UI-Transition>'
  3667. },
  3668. regExp : {
  3669. escape : /[-[\]{}()*+?.,\\^$|#\s]/g,
  3670. quote : /"/g
  3671. },
  3672. metadata : {
  3673. defaultText : 'defaultText',
  3674. defaultValue : 'defaultValue',
  3675. placeholderText : 'placeholder',
  3676. text : 'text',
  3677. value : 'value'
  3678. },
  3679. // property names for remote query
  3680. fields: {
  3681. remoteValues : 'results', // grouping for api results
  3682. values : 'values', // grouping for all dropdown values
  3683. disabled : 'disabled', // whether value should be disabled
  3684. name : 'name', // displayed dropdown text
  3685. value : 'value', // actual dropdown value
  3686. text : 'text' // displayed text when selected
  3687. },
  3688. keys : {
  3689. backspace : 8,
  3690. delimiter : 188, // comma
  3691. deleteKey : 46,
  3692. enter : 13,
  3693. escape : 27,
  3694. pageUp : 33,
  3695. pageDown : 34,
  3696. leftArrow : 37,
  3697. upArrow : 38,
  3698. rightArrow : 39,
  3699. downArrow : 40
  3700. },
  3701. selector : {
  3702. addition : '.addition',
  3703. dropdown : '.ui.dropdown',
  3704. hidden : '.hidden',
  3705. icon : '> .dropdown.icon',
  3706. input : '> input[type="hidden"], > select',
  3707. item : '.item',
  3708. label : '> .label',
  3709. remove : '> .label > .delete.icon',
  3710. siblingLabel : '.label',
  3711. menu : '.menu',
  3712. message : '.message',
  3713. menuIcon : '.dropdown.icon',
  3714. search : 'input.search, .menu > .search > input, .menu input.search',
  3715. sizer : '> input.sizer',
  3716. text : '> .text:not(.icon)',
  3717. unselectable : '.disabled, .filtered'
  3718. },
  3719. className : {
  3720. active : 'active',
  3721. addition : 'addition',
  3722. animating : 'animating',
  3723. clear : 'clear',
  3724. disabled : 'disabled',
  3725. empty : 'empty',
  3726. dropdown : 'ui dropdown',
  3727. filtered : 'filtered',
  3728. hidden : 'hidden transition',
  3729. item : 'item',
  3730. label : 'ui label',
  3731. loading : 'loading',
  3732. menu : 'menu',
  3733. message : 'message',
  3734. multiple : 'multiple',
  3735. placeholder : 'default',
  3736. sizer : 'sizer',
  3737. search : 'search',
  3738. selected : 'selected',
  3739. selection : 'selection',
  3740. upward : 'upward',
  3741. leftward : 'left',
  3742. visible : 'visible'
  3743. }
  3744. };
  3745. /* Templates */
  3746. $.fn.dropdown.settings.templates = {
  3747. // generates dropdown from select values
  3748. dropdown: function(select) {
  3749. var
  3750. placeholder = select.placeholder || false,
  3751. values = select.values || {},
  3752. html = ''
  3753. ;
  3754. html += '<i class="dropdown icon"></i>';
  3755. if(select.placeholder) {
  3756. html += '<div class="default text">' + placeholder + '</div>';
  3757. }
  3758. else {
  3759. html += '<div class="text"></div>';
  3760. }
  3761. html += '<div class="menu">';
  3762. $.each(select.values, function(index, option) {
  3763. html += (option.disabled)
  3764. ? '<div class="disabled item" data-value="' + option.value + '">' + option.name + '</div>'
  3765. : '<div class="item" data-value="' + option.value + '">' + option.name + '</div>'
  3766. ;
  3767. });
  3768. html += '</div>';
  3769. return html;
  3770. },
  3771. // generates just menu from select
  3772. menu: function(response, fields) {
  3773. var
  3774. values = response[fields.values] || {},
  3775. html = ''
  3776. ;
  3777. $.each(values, function(index, option) {
  3778. var
  3779. maybeText = (option[fields.text])
  3780. ? 'data-text="' + option[fields.text] + '"'
  3781. : '',
  3782. maybeDisabled = (option[fields.disabled])
  3783. ? 'disabled '
  3784. : ''
  3785. ;
  3786. html += '<div class="'+ maybeDisabled +'item" data-value="' + option[fields.value] + '"' + maybeText + '>';
  3787. html += option[fields.name];
  3788. html += '</div>';
  3789. });
  3790. return html;
  3791. },
  3792. // generates label for multiselect
  3793. label: function(value, text) {
  3794. return text + '<i class="delete icon"></i>';
  3795. },
  3796. // generates messages like "No results"
  3797. message: function(message) {
  3798. return message;
  3799. },
  3800. // generates user addition to selection menu
  3801. addition: function(choice) {
  3802. return choice;
  3803. }
  3804. };
  3805. })( jQuery, window, document );