1// MSDropDown - jquery.dd.js 2// author: Marghoob Suleman - http://www.marghoobsuleman.com/ 3// Date: 10 Nov, 2012 4// Version: 3.2 5// Revision: 21 6// web: www.marghoobsuleman.com 7/* 8// msDropDown is free jQuery Plugin: you can redistribute it and/or modify 9// it under the terms of the either the MIT License or the Gnu General Public License (GPL) Version 2 10*/ 11var msBeautify = msBeautify || {}; 12(function ($) { 13msBeautify = { 14 version: {msDropdown:'3.2'}, 15 author: "Marghoob Suleman", 16 counter: 20, 17 debug: function (v) { 18 if (v !== false) { 19 $(".ddOutOfVision").css({height: 'auto', position: 'relative'}); 20 } else { 21 //$(".ddOutOfVision").css({height: '0px', position: 'absolute'}); 22 $(".ddOutOfVision").css({height: '0px', position: 'relative'}); 23 } 24 }, 25 oldDiv: '', 26 create: function (id, settings, type) { 27 type = type || "dropdown"; 28 var data; 29 switch (type.toLowerCase()) { 30 case "dropdown": 31 case "select": 32 data = $(id).msDropdown(settings).data("dd"); 33 break; 34 } 35 return data; 36 } 37}; 38 39$.msDropDown = {}; 40$.msDropdown = {}; 41$.extend(true, $.msDropDown, msBeautify); 42$.extend(true, $.msDropdown, msBeautify); 43// make compatibiliy with old and new jquery 44if ($.fn.prop === undefined) {$.fn.prop = $.fn.attr;} 45if ($.fn.on === undefined) {$.fn.on = $.fn.bind;$.fn.off = $.fn.unbind;} 46if (typeof $.expr.createPseudo === 'function') { 47 //jQuery 1.8 or greater 48 $.expr[':'].Contains = $.expr.createPseudo(function (arg) {return function (elem) { return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0; }; }); 49} else { 50 //lower version 51 $.expr[':'].Contains = function (a, i, m) {return $(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; }; 52} 53//dropdown class 54function dd(element, settings) { 55 var _settings = $.extend(true,{byJson: {data: null, selectedIndex: 0, name: null, size: 0, multiple: false, width: 250}, 56 mainCSS: 'dd', 57 height: 120, //not using currently 58 visibleRows: 7, 59 rowHeight: 0, 60 showIcon: true, 61 zIndex: 9999, 62 useSprite: false, 63 animStyle: '', 64 event:'click', 65 openDirection: 'auto', //auto || alwaysUp 66 jsonTitle: false, 67 style: '', 68 disabledOpacity: 0.7, 69 disabledOptionEvents: true, 70 childWidth:0, 71 enableCheckbox:false, //this needs to multiple or it will set element to multiple 72 checkboxNameSuffix:'_mscheck', 73 append:'', 74 prepend:'', 75 on: {create: null,open: null,close: null,add: null,remove: null,change: null,blur: null,click: null,dblclick: null,mousemove: null,mouseover: null,mouseout: null,focus: null,mousedown: null,mouseup: null} 76 }, settings); 77 var _this = this; //this class 78 var _holderId = {postElementHolder: '_msddHolder', postID: '_msdd', postTitleID: '_title',postTitleTextID: '_titleText', postChildID: '_child'}; 79 var _styles = {dd:_settings.mainCSS, ddTitle: 'ddTitle', arrow: 'arrow arrowoff', ddChild: 'ddChild', ddTitleText: 'ddTitleText',disabled: 'disabled', enabled: 'enabled', ddOutOfVision: 'ddOutOfVision', borderTop: 'borderTop', noBorderTop: 'noBorderTop', selected: 'selected', divider: 'divider', optgroup: "optgroup", optgroupTitle: "optgroupTitle", description: "description", label: "ddlabel",hover: 'hover',disabledAll: 'disabledAll'}; 80 var _styles_i = {li: '_msddli_',borderRadiusTp: 'borderRadiusTp',ddChildMore: 'border shadow',fnone: "fnone"}; 81 var _isList = false, _isMultiple=false,_isDisabled=false, _cacheElement = {}, _element, _orginial, _isOpen=false; 82 var DOWN_ARROW = 40, UP_ARROW = 38, LEFT_ARROW=37, RIGHT_ARROW=39, ESCAPE = 27, ENTER = 13, ALPHABETS_START = 47, SHIFT=16 , CONTROL = 17; 83 var _shiftHolded=false, _controlHolded=false,_lastTarget=null,_forcedTrigger=false, _oldSelected, _isCreated = false; 84 var _doc = document; 85 86 var _checkDataSetting = function() { 87 _settings.mainCSS = $("#"+_element).data("maincss") || _settings.mainCSS; 88 _settings.visibleRows = $("#"+_element).data("visiblerows") || _settings.visibleRows; 89 if($("#"+_element).data("showicon")==false) {_settings.showIcon = $("#"+_element).data("showicon");}; 90 _settings.useSprite = $("#"+_element).data("usesprite") || _settings.useSprite; 91 _settings.animStyle = $("#"+_element).data("animstyle") || _settings.animStyle; 92 _settings.event = $("#"+_element).data("event") || _settings.event; 93 _settings.openDirection = $("#"+_element).data("opendirection") || _settings.openDirection; 94 _settings.jsonTitle = $("#"+_element).data("jsontitle") || _settings.jsonTitle; 95 _settings.disabledOpacity = $("#"+_element).data("disabledopacity") || _settings.disabledOpacity; 96 _settings.childWidth = $("#"+_element).data("childwidth") || _settings.childWidth; 97 _settings.enableCheckbox = $("#"+_element).data("enablecheckbox") || _settings.enableCheckbox; 98 _settings.checkboxNameSuffix = $("#"+_element).data("checkboxnamesuffix") || _settings.checkboxNameSuffix; 99 _settings.append = $("#"+_element).data("append") || _settings.append; 100 _settings.prepend = $("#"+_element).data("prepend") || _settings.prepend; 101 }; 102 var getElement = function(ele) { 103 if (_cacheElement[ele] === undefined) { 104 _cacheElement[ele] = _doc.getElementById(ele); 105 } 106 return _cacheElement[ele]; 107 }; 108 var _getIndex = function(opt) { 109 var childid = _getPostID("postChildID"); 110 return $("#"+childid + " li."+_styles_i.li).index(opt); 111 }; 112 var _createByJson = function() { 113 if (_settings.byJson.data) {
114 var validData = ["description","image", "title"]; 115 try { 116 if (!element.id) { 117 element.id = "dropdown"+msBeautify.counter; 118 }; 119 _settings.byJson.data = eval(_settings.byJson.data); 120 //change element 121 var id = "msdropdown"+(msBeautify.counter++); 122 var obj = {}; 123 obj.id = id; 124 obj.name = _settings.byJson.name || element.id; //its name 125 if (_settings.byJson.size>0) { 126 obj.size = _settings.byJson.size; 127 }; 128 obj.multiple = _settings.byJson.multiple; 129 var oSelect = _createElement("select", obj); 130 for(var i=0;i<_settings.byJson.data.length;i++) { 131 var current = _settings.byJson.data[i]; 132 var opt = new Option(current.text, current.value); 133 for(var p in current) { 134 if (p.toLowerCase() != 'text') { 135 var key = ($.inArray(p.toLowerCase(), validData)!=-1) ? "data-" : ""; 136 opt.setAttribute(key+p, current[p]); 137 }; 138 }; 139 oSelect.options[i] = opt; 140 }; 141 getElement(element.id).appendChild(oSelect); 142 oSelect.selectedIndex = _settings.byJson.selectedIndex; 143 $(oSelect).css({width: _settings.byJson.width+'px'}); 144 //now change element for access other things 145 element = oSelect; 146 } catch(e) { 147 throw "There is an error in json data."; 148 }; 149 }; 150 }; 151 var _construct = function() { 152 //set properties 153 _createByJson(); 154 if (!element.id) { 155 element.id = "msdrpdd"+(msBeautify.counter++); 156 }; 157 _element = element.id; 158 _this._element = _element; 159 _checkDataSetting(); 160 _isDisabled = getElement(_element).disabled; 161 var useCheckbox = _settings.enableCheckbox; 162 if(Boolean(useCheckbox)===true) { 163 getElement(_element).multiple = true; 164 _settings.enableCheckbox = true; 165 }; 166 _isList = (getElement(_element).size>1 || getElement(_element).multiple==true) ? true : false; 167 //trace("_isList "+_isList); 168 if (_isList) {_isMultiple = getElement(_element).multiple;}; 169 _mergeAllProp(); 170 //create layout 171 _createLayout(); 172 //set ui prop 173 _updateProp("uiData", _getDataAndUI()); 174 _updateProp("selectedOptions", $("#"+_element +" option:selected")); 175 var childid = _getPostID("postChildID"); 176 _oldSelected = $("#" + childid + " li." + _styles.selected); 177 }; 178 /********************************************************************************************/ 179 var _getPostID = function (id) { 180 return _element+_holderId[id]; 181 }; 182 var _getInternalStyle = function(ele) { 183 var s = (ele.style === undefined) ? "" : ele.style.cssText; 184 return s; 185 }; 186 var _parseOption = function(opt) { 187 var imagePath = '', title ='', description='', value=-1, text='', className='', imagecss = ''; 188 if (opt !== undefined) { 189 var attrTitle = opt.title || ""; 190 //data-title 191 if (attrTitle!="") { 192 var reg = /^\{.*\}$/; 193 var isJson = reg.test(attrTitle); 194 if (isJson && _settings.jsonTitle) { 195 var obj = eval("["+attrTitle+"]"); 196 }; 197 title = (isJson && _settings.jsonTitle) ? obj[0].title : title; 198 description = (isJson && _settings.jsonTitle) ? obj[0].description : description; 199 imagePath = (isJson && _settings.jsonTitle) ? obj[0].image : attrTitle; 200 imagecss = (isJson && _settings.jsonTitle) ? obj[0].imagecss : imagecss; 201 }; 202 203 text = opt.text || ''; 204 value = opt.value || ''; 205 className = opt.className || ""; 206 //ignore title attribute if playing with data tags 207 title = $(opt).prop("data-title") || $(opt).data("title") || (title || ""); 208 description = $(opt).prop("data-description") || $(opt).data("description") || (description || ""); 209 imagePath = $(opt).prop("data-image") || $(opt).data("image") || (imagePath || ""); 210 imagecss = $(opt).prop("data-imagecss") || $(opt).data("imagecss") || (imagecss || ""); 211 212 }; 213 var o = {image: imagePath, title: title, description: description, value: value, text: text, className: className, imagecss:imagecss}; 214 return o; 215 }; 216 var _createElement = function(nm, attr, html) { 217 var tag = _doc.createElement(nm); 218 if (attr) { 219 for(var i in attr) { 220 switch(i) { 221 case "style": 222 tag.style.cssText = attr[i]; 223 break; 224 default: 225 tag[i] = attr[i]; 226 break; 227 }; 228 }; 229 }; 230 if (html) { 231 tag.innerHTML = html; 232 }; 233 return tag; 234 }; 235 /********************************************************************************************/ 236 /*********************** <layout> *************************************/ 237 var _hideOriginal = function() { 238 var hidid = _getPostID("postElementHolder"); 239 if ($("#"+hidid).length==0) { 240 //var obj = {style: 'height: 0px;overflow: hidden;position: absolute;',className: _styles.ddOutOfVision}; 241 var obj = {style: 'height: 0px;overflow: hidden;position: relative;',className: _styles.ddOutOfVision}; 242 obj.id = hidid; 243 var oDiv = _createElement("div", obj); 244 $("#"+_element).after(oDiv); 245 $("#"+_element).appendTo($("#"+hidid)); 246 } else { 247 //$("#"+hidid).css({height: 0,overflow: 'hidden',position: 'absolute'}); 248 $("#"+hidid).css({height: 0,overflow: 'hidden',position: 'relative'}); 249 }; 250 }; 251 var _createWrapper = function () { 252 var obj = { 253 className: _styles.dd + " ddcommon borderRadius" 254 }; 255 var styles = _getInternalStyle(getElement(_element)); 256 var w = $("#" + _element).outerWidth(); 257 //obj.style = "width: " + w + "px;"; 258 if (styles.length > 0) { 259 obj.style = obj.style + "" + styles; 260 }; 261 obj.id = _getPostID("postID"); 262 var oDiv = _createElement("div", obj); 263 return oDiv; 264 }; 265 var _createTitle = function () { 266 var selectedOption; 267 if(getElement(_element).selectedIndex>=0) { 268 selectedOption = getElement(_element).options[getElement(_element).selectedIndex]; 269 } else { 270 selectedOption = {value:'', text:''}; 271 } 272 var spriteClass = "", selectedClass = ""; 273 //check sprite 274 var useSprite = $("#"+_element).data("usesprite"); 275 if(useSprite) { _settings.useSprite = useSprite; }; 276 if (_settings.useSprite != false) { 277 spriteClass = " " + _settings.useSprite; 278 selectedClass = " " + selectedOption.className; 279 }; 280 var oTitle = _createElement("div", {className: _styles.ddTitle + spriteClass + " " + _styles_i.borderRadiusTp}); 281 //divider 282 var oDivider = _createElement("span", {className: _styles.divider}); 283 //arrow 284 var oArrow = _createElement("span", {className: _styles.arrow}); 285 //title Text 286 var titleid = _getPostID("postTitleID"); 287 var oTitleText = _createElement("span", {className: _styles.ddTitleText + selectedClass, id: titleid}); 288 289 var parsed = _parseOption(selectedOption); 290 var arrowPath = parsed.image; 291 var sText = parsed.text || ""; 292 if (arrowPath != "" && _settings.showIcon) { 293 var oIcon = _createElement("img"); 294 var oIconParent = _createElement("span", {className: "ddImage"}); 295 oIcon.src = arrowPath; 296 if(parsed.imagecss!="") { 297 oIcon.className = parsed.imagecss+" "; 298 }; 299 }; 300 var oTitleText_in = _createElement("span", {className: _styles.label}, sText); 301 oTitle.appendChild(oDivider); 302 oTitle.appendChild(oArrow); 303 if (oIcon) { 304 oIconParent.appendChild(oIcon); 305 oTitleText.appendChild(oIconParent); 306 }; 307 oTitleText.appendChild(oTitleText_in); 308 oTitle.appendChild(oTitleText); 309 var oDescription = _createElement("span", {className: _styles.description}, parsed.description); 310 311 oTitleText_in.appendChild(oDescription); 312 313 return oTitle; 314 }; 315 var _createFilterBox = function () { 316 var tid = _getPostID("postTitleTextID"); 317 var sText = _createElement("input", {id: tid, type: 'text', value: '', autocomplete: 'off', className: 'text shadow borderRadius', style: 'display: none'}); 318 return sText; 319 }; 320 var _createChild = function (opt) { 321 var obj = {}; 322 var styles = _getInternalStyle(opt); 323 if (styles.length > 0) {obj.style = styles; }; 324 var css = (opt.disabled) ? _styles.disabled : _styles.enabled; 325 css = (opt.selected) ? (css + " " + _styles.selected) : css; 326 css = css + " " + _styles_i.li; 327 obj.className = css; 328 if (_settings.useSprite != false) { 329 obj.className = css + " " + opt.className; 330 }; 331 var li = _createElement("li", obj); 332 var parsed = _parseOption(opt); 333 if (parsed.title != "") { 334 li.title = parsed.title; 335 }; 336 var arrowPath = parsed.image; 337 if (arrowPath != "" && _settings.showIcon) { 338 var oIcon = _createElement("img"); 339 oIcon.src = arrowPath; 340 var oIconParent = _createElement("span", {className: "ddImage"}); 341 if(parsed.imagecss!="") { 342 oIcon.className = parsed.imagecss+" "; 343 }; 344 }; 345 if (parsed.description != "") { 346 var oDescription = _createElement("span", { 347 className: _styles.description 348 }, parsed.description); 349 }; 350 var sText = opt.text || ""; 351 var oTitleText = _createElement("span", { 352 className: _styles.label 353 }, sText); 354 //checkbox 355 if(_settings.enableCheckbox===true) { 356 var chkbox = _createElement("input", { 357 type: 'checkbox', name:_element+_settings.checkboxNameSuffix+'[]', value:opt.value||""}); //this can be used for future 358 li.appendChild(chkbox); 359 if(_settings.enableCheckbox===true) { 360 chkbox.checked = (opt.selected) ? true : false; 361 }; 362 }; 363 if (oIcon) { 364 oIconParent.appendChild(oIcon); 365 li.appendChild(oIconParent); 366 }; 367 li.appendChild(oTitleText); 368 if (oDescription) { 369 oTitleText.appendChild(oDescription); 370 } else { 371 if (oIcon) { 372 oIcon.className = oIcon.className+_styles_i.fnone; 373 }; 374 }; 375 var oClear = _createElement("div", {className: 'clear'}); 376 li.appendChild(oClear); 377 return li; 378 }; 379 var _createChildren = function () { 380 var childid = _getPostID("postChildID"); 381 var obj = {className: _styles.ddChild + " ddchild_ " + _styles_i.ddChildMore, id: childid}; 382 if (_isList == false) { 383 obj.style = "z-index: " + _settings.zIndex; 384 } else { 385 obj.style = "z-index:1"; 386 }; 387 var childWidth = $("#"+_element).data("childwidth") || _settings.childWidth; 388 if(childWidth) { 389 obj.style = (obj.style || "") + ";width:"+childWidth; 390 }; 391 var oDiv = _createElement("div", obj); 392 var ul = _createElement("ul"); 393 if (_settings.useSprite != false) { 394 ul.className = _settings.useSprite; 395 }; 396 var allOptions = getElement(_element).children; 397 for (var i = 0; i < allOptions.length; i++) { 398 var current = allOptions[i]; 399 var li; 400 if (current.nodeName.toLowerCase() == "optgroup") { 401 //create ul 402 li = _createElement("li", {className: _styles.optgroup}); 403 var span = _createElement("span", {className: _styles.optgroupTitle}, current.label); 404 li.appendChild(span); 405 var optChildren = current.children; 406 var optul = _createElement("ul"); 407 for (var j = 0; j < optChildren.length; j++) { 408 var opt_li = _createChild(optChildren[j]); 409 optul.appendChild(opt_li); 410 }; 411 li.appendChild(optul); 412 } else { 413 li = _createChild(current); 414 }; 415 ul.appendChild(li); 416 }; 417 oDiv.appendChild(ul); 418 return oDiv; 419 }; 420 var _childHeight = function (val) { 421 var childid = _getPostID("postChildID"); 422 if (val) { 423 if (val == -1) { //auto 424 $("#"+childid).css({height: "auto", overflow: "auto"}); 425 } else { 426 $("#"+childid).css("height", val+"px"); 427 }; 428 return false; 429 }; 430 //else return height 431 var iHeight; 432 if (getElement(_element).options.length > _settings.visibleRows) { 433 var margin = parseInt($("#" + childid + " li:first").css("padding-bottom")) + parseInt($("#" + childid + " li:first").css("padding-top")); 434 if(_settings.rowHeight===0) { 435 $("#" + childid).css({visibility:'hidden',display:'block'}); //hack for first child 436 _settings.rowHeight = Math.round($("#" + childid + " li:first").height()); 437 $("#" + childid).css({visibility:'visible'}); 438 if(!_isList || _settings.enableCheckbox===true) { 439 $("#" + childid).css({display:'none'}); 440 }; 441 }; 442 iHeight = ((_settings.rowHeight + margin) * _settings.visibleRows); 443 } else if (_isList) { 444 iHeight = $("#" + _element).height(); //get height from original element 445 }; 446 return iHeight; 447 }; 448 var _applyChildEvents = function () { 449 var childid = _getPostID("postChildID"); 450 $("#" + childid).on("click", function (e) { 451 if (_isDisabled === true) return false; 452 //prevent body click 453 e.preventDefault(); 454 e.stopPropagation(); 455 if (_isList) { 456 _bind_on_events(); 457 }; 458 }); 459 $("#" + childid + " li." + _styles.enabled).on("click", function (e) { 460 if(e.target.nodeName.toLowerCase() !== "input") { 461 _close(this); 462 }; 463 }); 464 $("#" + childid + " li." + _styles.enabled).on("mousedown", function (e) { 465 if (_isDisabled === true) return false; 466 _oldSelected = $("#" + childid + " li." + _styles.selected); 467 _lastTarget = this; 468 e.preventDefault(); 469 e.stopPropagation(); 470 //select current input 471 if(_settings.enableCheckbox===true) { 472 if(e.target.nodeName.toLowerCase() === "input") { 473 _controlHolded = true; 474 }; 475 }; 476 if (_isList === true) { 477 if (_isMultiple) { 478 if (_shiftHolded === true) { 479 $(this).addClass(_styles.selected); 480 var selected = $("#" + childid + " li." + _styles.selected); 481 var lastIndex = _getIndex(this); 482 if (selected.length > 1) { 483 var items = $("#" + childid + " li." + _styles_i.li); 484 var ind1 = _getIndex(selected[0]); 485 var ind2 = _getIndex(selected[1]); 486 if (lastIndex > ind2) { 487 ind1 = (lastIndex); 488 ind2 = ind2 + 1; 489 }; 490 for (var i = Math.min(ind1, ind2); i <= Math.max(ind1, ind2); i++) { 491 var current = items[i]; 492 if ($(current).hasClass(_styles.enabled)) { 493 $(current).addClass(_styles.selected); 494 }; 495 }; 496 }; 497 } else if (_controlHolded === true) { 498 $(this).toggleClass(_styles.selected); //toggle 499 if(_settings.enableCheckbox===true) { 500 var checkbox = this.childNodes[0]; 501 checkbox.checked = !checkbox.checked; //toggle 502 }; 503 } else { 504 $("#" + childid + " li." + _styles.selected).removeClass(_styles.selected); 505 $("#" + childid + " input:checkbox").prop("checked", false); 506 $(this).addClass(_styles.selected); 507 if(_settings.enableCheckbox===true) { 508 this.childNodes[0].checked = true; 509 }; 510 }; 511 } else { 512 $("#" + childid + " li." + _styles.selected).removeClass(_styles.selected); 513 $(this).addClass(_styles.selected); 514 }; 515 //fire event on mouseup 516 } else { 517 $("#" + childid + " li." + _styles.selected).removeClass(_styles.selected); 518 $(this).addClass(_styles.selected); 519 }; 520 }); 521 $("#" + childid + " li." + _styles.enabled).on("mouseenter", function (e) { 522 if (_isDisabled === true) return false; 523 e.preventDefault(); 524 e.stopPropagation(); 525 if (_lastTarget != null) { 526 if (_isMultiple) { 527 $(this).addClass(_styles.selected); 528 if(_settings.enableCheckbox===true) { 529 this.childNodes[0].checked = true; 530 }; 531 }; 532 }; 533 }); 534 535 $("#" + childid + " li." + _styles.enabled).on("mouseover", function (e) { 536 if (_isDisabled === true) return false; 537 $(this).addClass(_styles.hover); 538 }); 539 $("#" + childid + " li." + _styles.enabled).on("mouseout", function (e) { 540 if (_isDisabled === true) return false; 541 $("#" + childid + " li." + _styles.hover).removeClass(_styles.hover); 542 }); 543 544 $("#" + childid + " li." + _styles.enabled).on("mouseup", function (e) { 545 if (_isDisabled === true) return false; 546 e.preventDefault(); 547 e.stopPropagation(); 548 if(_settings.enableCheckbox===true) { 549 _controlHolded = false; 550 }; 551 var selected = $("#" + childid + " li." + _styles.selected).length; 552 _forcedTrigger = (_oldSelected.length != selected || selected == 0) ? true : false; 553 _fireAfterItemClicked(); 554 _unbind_on_events(); //remove old one 555 _bind_on_events(); 556 _lastTarget = null; 557 }); 558 559 /* options events */ 560 if (_settings.disabledOptionEvents == false) { 561 $("#" + childid + " li." + _styles_i.li).on("click", function (e) { 562 if (_isDisabled === true) return false; 563 fireOptionEventIfExist(this, "click"); 564 }); 565 $("#" + childid + " li." + _styles_i.li).on("mouseenter", function (e) { 566 if (_i
566sDisabled === true) return false; 567 fireOptionEventIfExist(this, "mouseenter"); 568 }); 569 $("#" + childid + " li." + _styles_i.li).on("mouseover", function (e) { 570 if (_isDisabled === true) return false; 571 fireOptionEventIfExist(this, "mouseover"); 572 }); 573 $("#" + childid + " li." + _styles_i.li).on("mouseout", function (e) { 574 if (_isDisabled === true) return false; 575 fireOptionEventIfExist(this, "mouseout"); 576 }); 577 $("#" + childid + " li." + _styles_i.li).on("mousedown", function (e) { 578 if (_isDisabled === true) return false; 579 fireOptionEventIfExist(this, "mousedown"); 580 }); 581 $("#" + childid + " li." + _styles_i.li).on("mouseup", function (e) { 582 if (_isDisabled === true) return false; 583 fireOptionEventIfExist(this, "mouseup"); 584 }); 585 }; 586 }; 587 var _removeChildEvents = function () { 588 var childid = _getPostID("postChildID"); 589 $("#" + childid).off("click"); 590 $("#" + childid + " li." + _styles.enabled).off("mouseenter"); 591 $("#" + childid + " li." + _styles.enabled).off("click"); 592 $("#" + childid + " li." + _styles.enabled).off("mouseover"); 593 $("#" + childid + " li." + _styles.enabled).off("mouseout"); 594 $("#" + childid + " li." + _styles.enabled).off("mousedown"); 595 $("#" + childid + " li." + _styles.enabled).off("mouseup"); 596 }; 597 var _applyEvents = function () { 598 var id = _getPostID("postID"); 599 var childid = _getPostID("postChildID"); 600 $("#" + id).on(_settings.event, function (e) { 601 if (_isDisabled === true) return false; 602 fireEventIfExist("click"); 603 //prevent body click 604 e.preventDefault(); 605 e.stopPropagation(); 606 _open(e); 607 }); 608 _applyChildEvents(); 609 $("#" + id).on("dblclick", on_dblclick); 610 $("#" + id).on("mousemove", on_mousemove); 611 $("#" + id).on("mouseenter", on_mouseover); 612 $("#" + id).on("mouseleave", on_mouseout); 613 $("#" + id).on("mousedown", on_mousedown); 614 $("#" + id).on("mouseup", on_mouseup); 615 }; 616 //after create 617 var _fixedForList = function () { 618 var id = _getPostID("postID"); 619 var childid = _getPostID("postChildID"); 620 if (_isList === true && _settings.enableCheckbox===false) { 621 $("#" + id + " ." + _styles.ddTitle).hide(); 622 $("#" + childid).css({display: 'block', position: 'relative'}); 623 //_open(); 624 } else { 625 if(_settings.enableCheckbox===false) { 626 _isMultiple = false; //set multiple off if this is not a list 627 }; 628 $("#" + id + " ." + _styles.ddTitle).show(); 629 //$("#" + childid).css({display: 'none', position: 'absolute'}); 630 $("#" + childid).css({display: 'none', position: 'relative'}); 631 //set value 632 var first = $("#" + childid + " li." + _styles.selected)[0]; 633 $("#" + childid + " li." + _styles.selected).removeClass(_styles.selected); 634 var index = _getIndex($(first).addClass(_styles.selected)); 635 _setValue(index); 636 }; 637 _childHeight(_childHeight()); //get and set height 638 }; 639 var _fixedForDisabled = function () { 640 var id = _getPostID("postID"); 641 var opc = (_isDisabled == true) ? _settings.disabledOpacity : 1; 642 if (_isDisabled === true) { 643 $("#" + id).addClass(_styles.disabledAll); 644 } else { 645 $("#" + id).removeClass(_styles.disabledAll); 646 }; 647 }; 648 var _fixedSomeUI = function () { 649 //auto filter 650 var tid = _getPostID("postTitleTextID"); 651 $("#" + tid).on("keyup", _applyFilters); 652 //if is list 653 _fixedForList(); 654 _fixedForDisabled(); 655 }; 656 var _createLayout = function () { 657 var oDiv = _createWrapper(); 658 var oTitle = _createTitle(); 659 oDiv.appendChild(oTitle); 660 //auto filter box 661 var oFilterBox = _createFilterBox(); 662 oDiv.appendChild(oFilterBox); 663 664 var oChildren = _createChildren(); 665 oDiv.appendChild(oChildren); 666 $("#" + _element).after(oDiv); 667 _hideOriginal(); //hideOriginal 668 _fixedSomeUI(); 669 _applyEvents(); 670 671 var childid = _getPostID("postChildID"); 672 //append 673 if(_settings.append!='') { 674 $("#" + childid).append(_settings.append); 675 }; 676 //prepend 677 if(_settings.prepend!='') { 678 $("#" + childid).prepend(_settings.prepend); 679 }; 680 if (typeof _settings.on.create == "function") { 681 _settings.on.create.apply(_this, arguments); 682 }; 683 }; 684 var _selectMutipleOptions = function (bySelected) { 685 var childid = _getPostID("postChildID"); 686 var selected = bySelected || $("#" + childid + " li." + _styles.selected); //bySelected or by argument 687 for (var i = 0; i < selected.length; i++) { 688 var ind = _getIndex(selected[i]); 689 getElement(_element).options[ind].selected = "selected"; 690 }; 691 _setValue(selected); 692 }; 693 var _fireAfterItemClicked = function () { 694 //console.log("_fireAfterItemClicked") 695 var childid = _getPostID("postChildID"); 696 var selected = $("#" + childid + " li." + _styles.selected); 697 if (_isMultiple && (_shiftHolded || _controlHolded) || _forcedTrigger) { 698 getElement(_element).selectedIndex = -1; //reset old 699 }; 700 var index; 701 if (selected.length == 0) { 702 index = -1; 703 } else if (selected.length > 1) { 704 //selected multiple 705 _selectMutipleOptions(selected); 706 //index = $("#" + childid + " li." + _styles.selected); 707 708 } else { 709 //if one selected 710 index = _getIndex($("#" + childid + " li." + _styles.selected)); 711 }; 712 if ((getElement(_element).selectedIndex != index || _forcedTrigger) && selected.length<=1) { 713 _forcedTrigger = false; 714 var evt = has_handler("change"); 715 getElement(_element).selectedIndex = index; 716 _setValue(index); 717 //local 718 if (typeof _settings.on.change == "function") { 719 var d = _getDataAndUI(); 720 _settings.on.change(d.data, d.ui); 721 }; 722 $("#" + _element).trigger("change"); 723 }; 724 }; 725 var _setValue = function (index, byvalue) { 726 if (index !== undefined) { 727 var selectedIndex, value, selectedText; 728 if (index == -1) { 729 selectedIndex = -1; 730 value = ""; 731 selectedText = ""; 732 _updateTitleUI(-1); 733 } else { 734 //by index or byvalue 735 if (typeof index != "object") { 736 var opt = getElement(_element).options[index]; 737 getElement(_element).selectedIndex = index; 738 selectedIndex = index; 739 value = _parseOption(opt); 740 selectedText = (index >= 0) ? getElement(_element).options[index].text : ""; 741 _updateTitleUI(undefined, value); 742 value = value.value; //for bottom 743 } else { 744 //this is multiple or by option 745 selectedIndex = (byvalue && byvalue.index) || getElement(_element).selectedIndex; 746 value = (byvalue && byvalue.value) || getElement(_element).value; 747 selectedText = (byvalue && byvalue.text) || getElement(_element).options[getElement(_element).selectedIndex].text || ""; 748 _updateTitleUI(selectedIndex); 749 }; 750 }; 751 _updateProp("selectedIndex", selectedIndex); 752 _updateProp("value", value); 753 _updateProp("selectedText", selectedText); 754 _updateProp("children", getElement(_element).children); 755 _updateProp("uiData", _getDataAndUI()); 756 _updateProp("selectedOptions", $("#" + _element + " option:selected")); 757 }; 758 }; 759 var has_handler = function (name) { 760 //True if a handler has been added in the html. 761 var evt = {byElement: false, byJQuery: false, hasEvent: false}; 762 var obj = $("#" + _element); 763 //console.log(name) 764 try { 765 //console.log(obj.prop("on" + name) + " "+name); 766 if (obj.prop("on" + name) !== null) { 767 evt.hasEvent = true; 768 evt.byElement = true; 769 }; 770 } catch(e) { 771 //console.log(e.message); 772 } 773 // True if a handler has been added using jQuery. 774 var evs; 775 if (typeof $._data == "function") { //1.8 776 evs = $._data(obj[0], "events"); 777 } else { 778 evs = obj.data("events"); 779 }; 780 if (evs && evs[name]) { 781 evt.hasEvent = true; 782 evt.byJQuery = true; 783 }; 784 return evt; 785 }; 786 var _bind_on_events = function () { 787 _unbind_on_events(); 788 $("body").on("click", _close); 789 //bind more events 790 $(document).on("keydown", on_keydown); 791 $(document).on("keyup", on_keyup); 792 //focus will work on this 793 }; 794 var _unbind_on_events = function () {
795 $("body").off("click", _close); 796 //bind more events 797 $(document).off("keydown", on_keydown); 798 $(document).off("keyup", on_keyup); 799 }; 800 var _applyFilters = function () { 801 var childid = _getPostID("postChildID"); 802 var tid = _getPostID("postTitleTextID"); 803 var sText = getElement(tid).value; 804 if (sText.length == 0) { 805 $("#" + childid + " li:hidden").show(); //show if hidden 806 _childHeight(_childHeight()); 807 } else { 808 $("#" + childid + " li").hide(); 809 $("#" + childid + " li:Contains('" + sText + "')").show(); 810 if ($("#" + childid + " li:visible").length <= _settings.visibleRows) { 811 _childHeight(-1); //set autoheight 812 }; 813 }; 814 }; 815 var _showFilterBox = function () { 816 var tid = _getPostID("postTitleTextID"); 817 if ($("#" + tid + ":hidden").length > 0 && _controlHolded == false) { 818 $("#" + tid + ":hidden").show().val(""); 819 getElement(tid).focus(); 820 }; 821 }; 822 var _hideFilterBox = function () { 823 var tid = _getPostID("postTitleTextID"); 824 if ($("#" + tid + ":visible").length > 0) { 825 $("#" + tid + ":visible").hide(); 826 getElement(tid).blur(); 827 }; 828 }; 829 var on_keydown = function (evt) { 830 var tid = _getPostID("postTitleTextID"); 831 var childid = _getPostID("postChildID"); 832 switch (evt.keyCode) { 833 case DOWN_ARROW: 834 case RIGHT_ARROW: 835 evt.preventDefault(); 836 evt.stopPropagation(); 837 //_hideFilterBox(); 838 _next(); 839 break; 840 case UP_ARROW: 841 case LEFT_ARROW: 842 evt.preventDefault(); 843 evt.stopPropagation(); 844 //_hideFilterBox(); 845 _previous(); 846 break; 847 case ESCAPE: 848 case ENTER: 849 evt.preventDefault(); 850 evt.stopPropagation(); 851 _close(); 852 var selected = $("#" + childid + " li." + _styles.selected).length; 853 _forcedTrigger = (_oldSelected.length != selected || selected == 0) ? true : false; 854 _fireAfterItemClicked(); 855 _unbind_on_events(); //remove old one 856 _lastTarget = null; 857 break; 858 case SHIFT: 859 _shiftHolded = true; 860 break; 861 case CONTROL: 862 _controlHolded = true; 863 break; 864 default: 865 if (evt.keyCode >= ALPHABETS_START && _isList === false) { 866 //_showFilterBox(); 867 }; 868 break; 869 }; 870 if (_isDisabled === true) return false; 871 fireEventIfExist("keydown"); 872 }; 873 var on_keyup = function (evt) { 874 switch (evt.keyCode) { 875 case SHIFT: 876 _shiftHolded = false; 877 break; 878 case CONTROL: 879 _controlHolded = false; 880 break; 881 }; 882 if (_isDisabled === true) return false; 883 fireEventIfExist("keyup"); 884 }; 885 var on_dblclick = function (evt) { 886 if (_isDisabled === true) return false; 887 fireEventIfExist("dblclick"); 888 }; 889 var on_mousemove = function (evt) { 890 if (_isDisabled === true) return false; 891 fireEventIfExist("mousemove"); 892 }; 893 894 var on_mouseover = function (evt) { 895 if (_isDisabled === true) return false; 896 evt.preventDefault(); 897 fireEventIfExist("mouseover"); 898 }; 899 var on_mouseout = function (evt) { 900 if (_isDisabled === true) return false; 901 evt.preventDefault(); 902 fireEventIfExist("mouseout"); 903 }; 904 var on_mousedown = function (evt) { 905 if (_isDisabled === true) return false; 906 fireEventIfExist("mousedown"); 907 }; 908 var on_mouseup = function (evt) { 909 if (_isDisabled === true) return false; 910 fireEventIfExist("mouseup"); 911 }; 912 var option_has_handler = function (opt, name) { 913 //True if a handler has been added in the html. 914 var evt = {byElement: false, byJQuery: false, hasEvent: false}; 915 if ($(opt).prop("on" + name) != undefined) { 916 evt.hasEvent = true; 917 evt.byElement = true; 918 }; 919 // True if a handler has been added using jQuery. 920 var evs = $(opt).data("events"); 921 if (evs && evs[name]) { 922 evt.hasEvent = true; 923 evt.byJQuery = true; 924 }; 925 return evt; 926 }; 927 var fireOptionEventIfExist = function (li, evt_n) { 928 if (_settings.disabledOptionEvents == false) { 929 var opt = getElement(_element).options[_getIndex(li)]; 930 //check if original has some 931 if (option_has_handler(opt, evt_n).hasEvent === true) { 932 if (option_has_handler(opt, evt_n).byElement === true) { 933 opt["on" + evt_n](); 934 }; 935 if (option_has_handler(opt, evt_n).byJQuery === true) { 936 switch (evt_n) { 937 case "keydown": 938 case "keyup": 939 //key down/up will check later 940 break; 941 default: 942 $(opt).trigger(evt_n); 943 break; 944 }; 945 }; 946 return false; 947 }; 948 }; 949 }; 950 var fireEventIfExist = function (evt_n) { 951 //local 952 if (typeof _settings.on[evt_n] == "function") { 953 _settings.on[evt_n].apply(this, arguments); 954 }; 955 //check if original has some 956 if (has_handler(evt_n).hasEvent === true) { 957 if (has_handler(evt_n).byElement === true) {
958 getElement(_element)["on" + evt_n](); 959 }; 960 if (has_handler(evt_n).byJQuery === true) { 961 switch (evt_n) { 962 case "keydown": 963 case "keyup": 964 //key down/up will check later 965 break; 966 default: 967 $("#" + _element).trigger(evt_n); 968 break; 969 }; 970 }; 971 return false; 972 }; 973 }; 974 /******************************* navigation **********************************************/ 975 var _scrollToIfNeeded = function (opt) { 976 var childid = _getPostID("postChildID"); 977 //if scroll is needed 978 opt = (opt !== undefined) ? opt : $("#" + childid + " li." + _styles.selected); 979 if (opt.length > 0) { 980 var pos = parseInt(($(opt).position().top)); 981 var ch = parseInt($("#" + childid).height()); 982 if (pos > ch) { 983 var top = pos + $("#" + childid).scrollTop() - (ch/2); 984 $("#" + childid); 985 }; 986 }; 987 }; 988 var _next = function () { 989 var childid = _getPostID("postChildID"); 990 var items = $("#" + childid + " li:visible." + _styles_i.li); 991 var selected = $("#" + childid + " li:visible." + _styles.selected); 992 selected = (selected.length==0) ? items[0] : selected; 993 var index = $("#" + childid + " li:visible." + _styles_i.li).index(selected); 994 if ((index < items.length - 1)) { 995 index = getNext(index); 996 if (index < items.length) { //check again - hack for last disabled 997 if (!_shiftHolded || !_isList || !_isMultiple) { 998 $("#" + childid + " ." + _styles.selected).removeClass(_styles.selected); 999 }; 1000 $(items[index]).addClass(_styles.selected); 1001 _updateTitleUI(index); 1002 if (_isList == true) { 1003 _fireAfterItemClicked(); 1004 }; 1005 _scrollToIfNeeded($(items[index])); 1006 }; 1007 if (!_isList) { 1008 _adjustOpen(); 1009 }; 1010 }; 1011 function getNext(ind) { 1012 ind = ind + 1; 1013 if (ind > items.length) { 1014 return ind; 1015 }; 1016 if ($(items[ind]).hasClass(_styles.enabled) === true) { 1017 return ind; 1018 }; 1019 return ind = getNext(ind); 1020 }; 1021 }; 1022 var _previous = function () { 1023 var childid = _getPostID("postChildID"); 1024 var selected = $("#" + childid + " li:visible." + _styles.selected); 1025 var items = $("#" + childid + " li:visible." + _styles_i.li); 1026 var index = $("#" + childid + " li:visible." + _styles_i.li).index(selected[0]); 1027 if (index >= 0) { 1028 index = getPrev(index); 1029 if (index >= 0) { //check again - hack for disabled 1030 if (!_shiftHolded || !_isList || !_isMultiple) { 1031 $("#" + childid + " ." + _styles.selected).removeClass(_styles.selected); 1032 }; 1033 $(items[index]).addClass(_styles.selected); 1034 _updateTitleUI(index); 1035 if (_isList == true) { 1036 _fireAfterItemClicked(); 1037 }; 1038 if (parseInt(($(items[index]).position().top + $(items[index]).height())) <= 0) { 1039 var top = ($("#" + childid).scrollTop() - $("#" + childid).height()) - $(items[index]).height(); 1040 $("#" + childid); 1041 }; 1042 }; 1043 if (!_isList) { 1044 _adjustOpen(); 1045 }; 1046 }; 1047 1048 function getPrev(ind) { 1049 ind = ind - 1; 1050 if (ind < 0) { 1051 return ind; 1052 }; 1053 if ($(items[ind]).hasClass(_styles.enabled) === true) { 1054 return ind; 1055 }; 1056 return ind = getPrev(ind); 1057 }; 1058 }; 1059 var _adjustOpen = function () { 1060 var id = _getPostID("postID"); 1061 var childid = _getPostID("postChildID"); 1062 var pos = $("#" + id).offset(); 1063 var mH = $("#" + id).height(); 1064 var wH = $(window).height(); 1065 var st = $(window).scrollTop(); 1066 var cH = $("#" + childid).height(); 1067 var top = $("#" + id).height(); //this close so its title height 1068 1069 var contBottom = $("#" + id).closest('.btQuoteBooking').offset().top + $("#" + id).closest('.btQuoteBooking').height(); 1070 1071 if (contBottom < Math.floor(cH + mH + pos.top) || _settings.openDirection.toLowerCase() == 'alwaysup') { 1072 top = cH; 1073 $("#" + childid).css({top: "-" + top + "px", display: 'block', zIndex: _settings.zIndex}); 1074 $("#" + id).removeClass("borderRadius borderRadiusTp").addClass("borderRadiusBtm"); 1075 var top = $("#" + childid).offset().top; 1076 if (top < -10) { 1077 $("#" + childid).css({top: (parseInt($("#" + childid).css("top")) - top + 20 + st) + "px", zIndex: _settings.zIndex}); 1078 $("#" + id).removeClass("borderRadiusBtm borderRadiusTp").addClass("borderRadius"); 1079 }; 1080 } else { 1081 $("#" + childid).css({top: top + "px", zIndex: _settings.zIndex}); 1082 $("#" + id).removeClass("borderRadius borderRadiusBtm").addClass("borderRadiusTp"); 1083 }; 1084 //hack for ie zindex 1085 //i hate ie :D 1086 /*if($.browser.msie) { 1087 if(parseInt($.browser.version)<=7) { 1088 $('div.ddcommon').css("zIndex", _settings.zIndex-10); 1089 $("#" + id).css("zIndex", _settings.zIndex+5); 1090 }; 1091 };*/ 1092 }; 1093 var _open = function (e) { 1094 if (_isDisabled === true) return false; 1095 var id = _getPostID("postID"); 1096 var childid = _getPostID("postChildID"); 1097 if (!_isOpen) { 1098 _isOpen = true; 1099 if (msBeautify.oldDiv != '') { 1100 $("#" + msBeautify.oldDiv).css({display: "none"}); //hide all 1101 }; 1102 msBeautify.oldDiv = childid; 1103 $("#" + childid + " li:hidden").show(); //show if hidden 1104 _adjustOpen(); 1105 var animStyle = _settings.animStyle; 1106 if(animStyle=="" || animStyle=="none") { 1107 $("#" + childid).css({display:"block"}); 1108 _scrollToIfNeeded(); 1109 if (typeof _settings.on.open == "function") { 1110 var d = _getDataAndUI(); 1111 _settings.on.open(d.data, d.ui); 1112 }; 1113 } else { 1114 $("#" + childid)[animStyle]("fast", function () { 1115 _scrollToIfNeeded(); 1116 if (typeof _settings.on.open == "function") { 1117 var d = _getDataAndUI(); 1118 _settings.on.open(d.data, d.ui); 1119 }; 1120 }); 1121 }; 1122 _bind_on_events(); 1123 } else { 1124 if(_settings.event!=='mouseover') { 1125 _close(); 1126 }; 1127 }; 1128 }; 1129 var _close = function (e) { 1130 _isOpen = false; 1131 var id = _getPostID("postID"); 1132 var childid = _getPostID("postChildID"); 1133 if (_isList === false || _settings.enableCheckbox===true) { 1134 $("#" + childid).css({display: "none"}); 1135 $("#" + id).removeClass("borderRadiusTp borderRadiusBtm").addClass("borderRadius"); 1136 }; 1137 _unbind_on_events(); 1138 if (typeof _settings.on.close == "function") { 1139 var d = _getDataAndUI(); 1140 _settings.on.close(d.data, d.ui); 1141 }; 1142 //rest some old stuff 1143 _hideFilterBox(); 1144 _childHeight(_childHeight()); //its needed after filter applied 1145 $("#" + childid).css({zIndex:1}) 1146 }; 1147 /*********************** </layout> *************************************/ 1148 var _mergeAllProp = function () { 1149 _orginial = $.extend(true, {}, getElement(_element)); 1150 for (var i in _orginial) { 1151 if (typeof _orginial[i] != "function") { 1152 _this[i] = _orginial[i]; //properties 1153 }; 1154 }; 1155 _this.selectedText = (_orginial.selectedIndex >= 0) ? _orginial.options[_orginial.selectedIndex].text : ""; 1156 _this.version = msBeautify.version.msDropdown; 1157 _this.author = msBeautify.author; 1158 }; 1159 var _getDataAndUIByOption = function (opt) { 1160 if (opt != null && typeof opt != "undefined") { 1161 var childid = _getPostID("postChildID"); 1162 var data = _parseOption(opt); 1163 var ui = $("#" + childid + " li." + _styles_i.li + ":eq(" + (opt.index) + ")"); 1164 return {data: data, ui: ui, option: opt, index: opt.index}; 1165 }; 1166 return null; 1167 }; 1168 var _getDataAndUI = function () { 1169 var childid = _getPostID("postChildID"); 1170 var ele = getElement(_element); 1171 var data, ui, option, index; 1172 if (ele.selectedIndex == -1) { 1173 data = null; 1174 ui = null; 1175 option = null; 1176 index = -1; 1177 } else { 1178 ui = $("#" + childid + " li." + _styles.selected); 1179 if (ui.length > 1) { 1180 var d = [], op = [], ind = []; 1181 for (var i = 0; i < ui.length; i++) { 1182 var pd = _getIndex(ui[i]); 1183 d.push(pd); 1184 op.push(ele.options[pd]); 1185 }; 1186 data = d; 1187 option = op; 1188 index = d; 1189 } else { 1190 option = ele.options[ele.selectedIndex]; 1191 data = _parseOption(option); 1192 index = ele.selectedIndex; 1193 }; 1194 }; 1195 return {data: data, ui: ui, index: index, option: option}; 1196 }; 1197 var _updateTitleUI = function (index, byvalue) { 1198 var titleid = _getPostID("postTitleID"); 1199 var value = {}; 1200 if (index == -1) { 1201 value.text = " "; 1202 value.className = ""; 1203 value.description = ""; 1204 value.image = ""; 1205 } else if (typeof index != "undefined") { 1206 var opt = getElement(_element).options[index]; 1207 value = _parseOption(opt); 1208 } else { 1209 value = byvalue; 1210 }; 1211 1212 //update title and current 1213 $("#" + titleid).find("." + _styles.label).html(value.text); 1214 var textnode = document.createTextNode( value.description ); 1215 var desc = _createElement("span", {className: _styles.description}); 1216 desc.appendChild( textnode ); 1217 $("#" + titleid).find("." + _styles.description).remove(); 1218 $("#" + titleid).find("." + _styles.label).append( desc ); 1219 getElement(titleid).className = _styles.ddTitleText + " " + value.className; 1220 1221 $("#" + titleid).removeClass( 'btNotSelected' ); 1222 $("#" + titleid).closest( '.btContactField' ).removeClass( 'btNotSelected' ); 1223 if ( value.value == '' ) { 1224 $("#" + titleid).addClass( 'btNotSelected' ); 1225 $("#" + titleid).closest( '.btContactField' ).addClass( 'btNotSelected' ); 1226 } 1227 1228 //update desction 1229 if (value.description != "") { 1230 $("#" + titleid).find("." + _styles.description).html(value.description).show(); 1231 } else { 1232 $("#" + titleid).find("." + _styles.description).html("").hide(); 1233 }; 1234 //update icon 1235 var img = $("#" + titleid).find("img"); 1236 if (img.length > 0) { 1237 $(img).parent().remove(); 1238 }; 1239 if (value.image != "" && _settings.showIcon) { 1240 img = _createElement("img", {src: value.image}); 1241 var imgParent = _createElement("span", {className: "ddImage"}); 1242 imgParent.appendChild(img); 1243 $("#" + titleid).prepend(imgParent); 1244 if(value.imagecss!="") { 1245 img.className = value.imagecss+" "; 1246 }; 1247 if (value.description == "") { 1248 img.className = img.className+_styles_i.fnone; 1249 }; 1250 }; 1251 }; 1252 var _updateProp = function (p, v) { 1253 _this[p] = v; 1254 }; 1255 var _updateUI = function (a, opt, i) { //action, index, opt 1256 var childid = _getPostID("postChildID"); 1257 var wasSelected = false; 1258 switch (a) { 1259 case "add": 1260 var li = _createChild(opt || getElement(_element).options[i]); 1261 var index; 1262 if (arguments.length == 3) { 1263 index = i; 1264 } else { 1265 index = $("#" + childid + " li." + _styles_i.li).length - 1; 1266 }; 1267 if (index < 0 || !index) { 1268 $("#" + childid + " ul").append(li); 1269 } else { 1270 var at = $("#" + childid + " li." + _styles_i.li)[index]; 1271 $(at).before(li); 1272 }; 1273 _removeChildEvents(); 1274 _applyChildEvents(); 1275 if (_settings.on.add != null) { 1276 _settings.on.add.apply(this, arguments); 1277 }; 1278 break; 1279 case "remove": 1280 wasSelected = $($("#" + childid + " li." + _styles_i.li)[i]).hasClass(_styles.selected); 1281 $("#" + childid + " li." + _styles_i.li + ":eq(" + i + ")").remove(); 1282 var items = $("#" + childid + " li." + _styles.enabled); 1283 if (wasSelected == true) { 1284 if (items.length > 0) { 1285 $(items[0]).addClass(_styles.selected); 1286 var ind = $("#" + childid + " li." + _styles_i.li).index(items[0]); 1287 _setValue(ind); 1288 }; 1289 }; 1290 if (items.length == 0) { 1291 _setValue(-1); 1292 }; 1293 if ($("#" + childid + " li." + _styles_i.li).length < _settings.visibleRows && !_isList) { 1294 _childHeight(-1); //set autoheight 1295 }; 1296 if (_settings.on.remove != null) { 1297 _settings.on.remove.apply(this, arguments); 1298 }; 1299 break; 1300 }; 1301 }; 1302 /************************** public methods/events **********************/ 1303 this.act = function () { 1304 var action = arguments[0]; 1305 Array.prototype.shift.call(arguments); 1306 switch (action) { 1307 case "add": 1308 _this.add.apply(this, arguments); 1309 break; 1310 case "remove": 1311 _this.remove.apply(this, arguments); 1312 break; 1313 default:
1314 try { 1315 getElement(_element)[action].apply(getElement(_element), arguments); 1316 } catch (e) { 1317 //there is some error. 1318 }; 1319 break; 1320 }; 1321 }; 1322 1323 this.add = function () { 1324 var text, value, title, image, description; 1325 var obj = arguments[0]; 1326 if (typeof obj == "string") { 1327 text = obj; 1328 value = text; 1329 opt = new Option(text, value); 1330 } else { 1331 text = obj.text || ''; 1332 value = obj.value || text; 1333 title = obj.title || ''; 1334 image = obj.image || ''; 1335 description = obj.description || ''; 1336 //image:imagePath, title:title, description:description, value:opt.value, text:opt.text, className:opt.className||"" 1337 opt = new Option(text, value); 1338 $(opt).data("description", description); 1339 $(opt).data("image", image); 1340 $(opt).data("title", title); 1341 }; 1342 arguments[0] = opt; //this option 1343 getElement(_element).add.apply(getElement(_element), arguments); 1344 _updateProp("children", getElement(_element)["children"]); 1345 _updateProp("length", getElement(_element).length); 1346 _updateUI("add", opt, arguments[1]); 1347 }; 1348 this.remove = function (i) { 1349 getElement(_element).remove(i); 1350 _updateProp("children", getElement(_element)["children"]); 1351 _updateProp("length", getElement(_element).length); 1352 _updateUI("remove", undefined, i); 1353 }; 1354 this.set = function (prop, val) { 1355 if (typeof prop == "undefined" || typeof val == "undefined") return false; 1356 prop = prop.toString(); 1357 try { 1358 _updateProp(prop, val); 1359 } catch (e) {/*this is ready only */}; 1360 1361 switch (prop) { 1362 case "size": 1363 getElement(_element)[prop] = val; 1364 if (val == 0) { 1365 getElement(_element).multiple = false; //if size is zero multiple should be false 1366 }; 1367 _isList = (getElement(_element).size > 1 || getElement(_element).multiple == true) ? true : false; 1368 _fixedForList(); 1369 break; 1370 case "multiple": 1371 getElement(_element)[prop] = val; 1372 _isList = (getElement(_element).size > 1 || getElement(_element).multiple == true) ? true : false; 1373 _isMultiple = getElement(_element).multiple; 1374 _fixedForList(); 1375 _updateProp(prop, val); 1376 break; 1377 case "disabled": 1378 getElement(_element)[prop] = val; 1379 _isDisabled = val; 1380 _fixedForDisabled(); 1381 break; 1382 case "selectedIndex": 1383 case "value": 1384 getElement(_element)[prop] = val; 1385 var childid = _getPostID("postChildID"); 1386 $("#" + childid + " li." + _styles_i.li).removeClass(_styles.selected); 1387 $($("#" + childid + " li." + _styles_i.li)[getElement(_element).selectedIndex]).addClass(_styles.selected); 1388 _setValue(getElement(_element).selectedIndex); 1389 break; 1390 case "length": 1391 var childid = _getPostID("postChildID"); 1392 if (val < getElement(_element).length) { 1393 getElement(_element)[prop] = val; 1394 if (val == 0) { 1395 $("#" + childid + " li." + _styles_i.li).remove(); 1396 _setValue(-1); 1397 } else { 1398 $("#" + childid + " li." + _styles_i.li + ":gt(" + (val - 1) + ")").remove(); 1399 if ($("#" + childid + " li." + _styles.selected).length == 0) { 1400 $("#" + childid + " li." + _styles.enabled + ":eq(0)").addClass(_styles.selected); 1401 }; 1402 }; 1403 _updateProp(prop, val); 1404 _updateProp("children", getElement(_element)["children"]); 1405 }; 1406 break; 1407 case "id": 1408 //please i need this. so preventing to change it. will work on this later 1409 break; 1410 default: 1411 //check if this is not a readonly properties 1412 try { 1413 getElement(_element)[prop] = val; 1414 _updateProp(prop, val); 1415 } catch (e) { 1416 //silent 1417 }; 1418 break; 1419 } 1420 }; 1421 this.get = function (prop) { 1422 return _this[prop] || getElement(_element)[prop]; //return if local else from original 1423 }; 1424 this.visible = function (val) { 1425 var id = _getPostID("postID"); 1426 if (val === true) { 1427 $("#" + id).show(); 1428 } else if (val === false) { 1429 $("#" + id).hide(); 1430 } else { 1431 return ($("#" + id).css("display")=="none") ? false : true; 1432 }; 1433 }; 1434 this.debug = function (v) { 1435 msBeautify.debug(v); 1436 }; 1437 this.close = function () { 1438 _close(); 1439 }; 1440 this.open = function () { 1441 _open(); 1442 }; 1443 this.showRows = function (r) { 1444 if (typeof r == "undefined" || r == 0) { 1445 return false; 1446 }; 1447 _settings.visibleRows = r; 1448 _childHeight(_childHeight()); 1449 }; 1450 this.visibleRows = this.showRows;
1451 this.on = function (type, fn) { 1452 $("#" + _element).on(type, fn); 1453 }; 1454 this.off = function (type, fn) { 1455 $("#" + _element).off(type, fn); 1456 }; 1457 this.addMyEvent = this.on; 1458 this.getData = function () { 1459 return _getDataAndUI() 1460 }; 1461 this.namedItem = function () { 1462 var opt = getElement(_element).namedItem.apply(getElement(_element), arguments); 1463 return _getDataAndUIByOption(opt); 1464 }; 1465 this.item = function () { 1466 var opt = getElement(_element).item.apply(getElement(_element), arguments); 1467 return _getDataAndUIByOption(opt); 1468 }; 1469 //v 3.2 1470 this.setIndexByValue = function(val) { 1471 this.set("value", val); 1472 }; 1473 this.destory = function () { 1474 var hidid = _getPostID("postElementHolder"); 1475 var id = _getPostID("postID"); 1476 $("#" + id + ", #" + id + " *").off(); 1477 $("#" + id).remove(); 1478 $("#" + _element).parent().replaceWith($("#" + _element)); 1479 $("#" + _element).data("dd", null); 1480 }; 1481 //Create msDropDown 1482 _construct(); 1483}; 1484//bind in jquery 1485$.fn.extend({ 1486 msDropDown: function(settings) 1487 { 1488 return this.each(function() 1489 { 1490 if (!$(this).data('dd')){ 1491 var mydropdown = new dd(this, settings); 1492 $(this).data('dd', mydropdown); 1493 }; 1494 }); 1495 } 1496}); 1497$.fn.msDropdown = $.fn.msDropDown; //make a copy 1498})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.