Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.31 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  
  5. <style type='text/css'>
  6. #ddList-fruitTypeList > a > label, #ddList-fruitTypeList > a > small { -ms-text-overflow: ellipsis; -o-text-overflow: ellipsis; text-overflow: ellipsis; white-space: nowrap; }
  7. .ddListContainer { position: relative; display: inline-block; text-align: center; font: 1.1em/1.4 "Georgia"; letter-spacing: 0.04em; }
  8. /* Fonts setup */
  9. .ddListContainer > a > label, .ddListContainer > ul li a > label, .ddListContainer > a > small,
  10. .ddListContainer > ul li a > small { font: 1em "Verdana"; }
  11. /* The 'selection' rectangle of the dropdownlist */
  12. .ddListContainer > a { background: #000000; border: solid 1px #000000; border-radius: 4px; cursor: pointer; overflow: hidden; display: block; padding: 5px 0 5px 0; color: yellow; }
  13. .ddListContainer > a:hover { border: solid 1px #000000; color: #f2e8c9; }
  14. .ddListContainer.ddListDisabled > a { border: solid 2px #e0e0e0; cursor: default; background: #f8f8f8; color: #999; }
  15. /* The 'selection' rectangle of the dropdownlist when open */
  16. .ddListIsOpen > a, .ddListIsOpen > a:hover { border:solid 1px #000000; background-color:#000000; color:#f2e8c9; }
  17. /* The right arrow in the 'selection' rectangle of the dropdownlist */
  18. .ddListArrow { width: 0; height: 0; position: absolute; right: 8px; top: 50%; border: solid 4px transparent; border-top: solid 7px #f2e8c9; }
  19. .ddListDisabled .ddListArrow { border-top: solid 5px #999; }
  20. .ddListIsOpen .ddListArrow { margin-top: -10px; border: solid 4px transparent !important; border-bottom: solid 7px #f2e8c9 !important; }
  21. /* The 'options' list of the dropdownlist */
  22. .ddListContainer > ul { display: none; position: absolute; z-index: 2000; border: solid 1px #000000; border-radius: 4px; border-top: none; list-style: none; margin: 0; padding: 0; background: #000000; overflow: auto; -moz-box-shadow: 0 1px 5px #ddd; -webkit-box-shadow: 0 1px 5px #ddd; box-shadow: 0 1px 5px #ddd; }
  23. .ddListContainer > ul li { margin-bottom: 0; }
  24. .ddListContainer > ul > li:last-child > a { border-bottom: none; }
  25. /* Any 'option' inside the 'options' list of the dropdownlist */
  26. .ddListContainer > ul li a { padding: 5px 0 5px 0; display: block; overflow: hidden; text-decoration: none; color: #b5a77d; cursor: pointer; -webkit-transition: all 0.25s ease-in-out; -moz-transition: all 0.25s ease-in-out; -o-transition: all 0.25s ease-in-out; -ms-transition: all 0.25s ease-in-out; transition: all 0.25s ease-in-out; }
  27. .ddListContainer > ul li a:hover { background: grey; color: #ffffff; }
  28. /* A 'selected' option inside the 'options' list of the dropdownlist */
  29. .ddListOptionIsSelected { background: #000000; }
  30. /* Additional styling of text, img, description for 'selection' and 'option' */
  31. .ddListContainer:not(.ddListDisabled) > a > label,
  32. .ddListContainer:not(.ddListDisabled) > ul li a > label { cursor: pointer; }
  33. .ddListContainer > a > label { padding-right: 15px; }
  34. .ddListContainer > a > small { display:block; overflow: hidden; padding-right: 15px; }
  35. .ddListContainer > ul li a > small { display:block; overflow: hidden; }
  36. .ddListContainer > a > img, .ddListContainer > ul li a > img { vertical-align: middle; float: left; margin-left: 5px; max-width: 64px; }
  37. </style>
  38.  
  39. </head>
  40. <body>
  41. <div id="ddList-fruitTypeList" >
  42. <select id="fruitTypeList" name="cargoType" >
  43. <option value="1" selected="selected" data-description="oranges">Yellow</option>
  44. <option value="2" data-description="bananas">White</option>
  45. <option value="3" data-description="lemon">Green</option>
  46. </select>
  47. </div>
  48.  
  49. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  50.  
  51. <script type="text/javascript">
  52. $(function() { $('#fruitTypeList').ddlist({
  53. width: 200,
  54. onSelected: function (index, value, text) {
  55. if (value == 1) { /* do something */ }
  56. if (value == 2) { alert ("second choice"); }
  57. }
  58. });
  59. });
  60.  
  61. // ----------------------------------------------- Plugin ----------------------------------------
  62. ; (function ($, win, document, undefined) {
  63. var version = '1.1.1';
  64. var pluginName = 'ddlist';
  65. var ENTER = 13,
  66. ESCAPE = 27,
  67. SPACE = 32,
  68. UP = 38,
  69. DOWN = 40;
  70.  
  71. function Plugin(element, options) {
  72. // Get the main element
  73. this.element = element;
  74. this.selObj = $(element);
  75. this.isOpen = false;
  76. this.settings = {
  77. width: 260,
  78. selectionIndex: 0,
  79. disabled: false,
  80. showSelectionTextOnly: false,
  81. onSelectedOnInit: false,
  82. onSelected: function (index, value, text) { },
  83. itemsSource: null
  84. };
  85. this._init(options);
  86. };
  87.  
  88. Plugin.prototype = {
  89.  
  90. // Select index
  91. _selectIndex: function(index) {
  92. // Remove current 'ddListOptionIsSelected' class
  93. this.ddOptions.find('a').removeClass('ddListOptionIsSelected');
  94.  
  95. // Set 'ddListOptionIsSelected' class to element that corresponds with index
  96. var selectedOption = this.ddOptions.find('a').eq(index);
  97. selectedOption.addClass('ddListOptionIsSelected');
  98.  
  99. // Update selected index/value/text
  100. var optionData = this.options[index];
  101. this.selectedIndex = index;
  102. this.selectedValue = optionData.value;
  103. this.selectedText = optionData.text;
  104.  
  105. // Update <select> element (if we are not dealing with json-provided data)
  106. if (this.settings.itemsSource == null) {
  107. // Clear current 'selected'
  108. this.selObj.find('option').attr('selected', false);
  109. // Set new 'selected'
  110. var selOption = this.selObj.find('option').eq(index);
  111. selOption.attr('selected', true);
  112. }
  113.  
  114. // Update ddListSelection element
  115. if (this.settings.showSelectionTextOnly) {
  116. this.ddSelection.html(optionData.text);
  117. }
  118. else {
  119. this.ddSelection.html((optionData.imageSrc ? '<img src="' + optionData.imageSrc + '" />' : '') +
  120. (optionData.text ? '<label>' + optionData.text + '</label>' : '') +
  121. (optionData.description ? '<small>' + optionData.description + '</small>' : ''));
  122. }
  123. },
  124.  
  125. _open: function() {
  126. // Close possible other open dropdown lists
  127. var otherOpenListObjs = $('.ddListIsOpen').not(this.ddListObj);
  128. if (otherOpenListObjs.length != 0) {
  129. otherOpenListObjs.removeClass('ddListIsOpen');
  130. otherOpenListObjs.find('> ul').slideUp(50);
  131. }
  132. this.ddListObj.find('> ul').slideDown('fast');
  133. this.ddListObj.addClass('ddListIsOpen');
  134. this.isOpen = true;
  135. },
  136.  
  137. _close: function () {
  138. // Close drop down
  139. this.ddListObj.removeClass('ddListIsOpen');
  140. this.ddListObj.find('> ul').slideUp(50);
  141. this.isOpen = false;
  142. },
  143.  
  144. _enable: function () {
  145. var self = this;
  146. this.settings.disabled = false;
  147. this.ddListObj.removeClass('ddListDisabled');
  148. // Bind event handlers
  149. // this.ddSelection.on('click.ddlist', function () {
  150. this.ddSelection.parent().on('click.ddlist', function (e) {
  151. if (self.isOpen) {
  152. self._close();
  153. }
  154. else {
  155. self._open();
  156. }
  157. e.stopPropagation();
  158. });
  159. this.ddOptions.find('a').on('click.ddlist', function (e) {
  160. // Select (new) index
  161. self._selectIndex($(this).closest('li').index());
  162. // Close
  163. self._close();
  164. // Callback function on selection
  165. self.settings.onSelected.call(self, self.selectedIndex, self.selectedValue, self.selectedText);
  166. e.stopPropagation();
  167. });
  168. //Click anywhere to close
  169. this.ddListObj.on('click.ddlist', function (e) {
  170. e.stopPropagation();
  171. });
  172. $('body').on('click.ddlist-' + this.selObj.attr('id'), function () {
  173. self._close();
  174. });
  175. },
  176.  
  177. _disable: function () {
  178. this.settings.disabled = true;
  179. // Unbind event handlers
  180. $('body').off('.ddlist-' + this.selObj.attr('id'));
  181. this.ddListObj.off('.ddlist');
  182. this.ddOptions.find('a').off('.ddlist');
  183. // this.ddSelection.off('.ddlist');
  184. this.ddSelection.parent().off('.ddlist');
  185. this.ddListObj.addClass('ddListDisabled');
  186. },
  187.  
  188. // Add items to the ddOptions list
  189. _bindListItems: function (disableAccess) {
  190. var self = this;
  191. // Add options to <ul> element
  192. $.each(this.options, function (index, item) {
  193. if (item.selected) { self.settings.selectionIndex = index; }
  194. self.ddOptions.append('<li>' + '<a>' + (item.imageSrc ? ' <img src="' + item.imageSrc + '" />' : '') + (item.text ? ' <label>' + item.text + '</label>' : '') + (item.description ? ' <small>' + item.description + '</small>' : '') + '</a>' + '</li>');
  195. });
  196.  
  197. // Bind event handlers
  198. if (!disableAccess) { this._enable(); }
  199. else { this.ddListObj.addClass('ddListDisabled'); }
  200.  
  201. // Show 'selected' item in the top selection element
  202. this._selectIndex(this.settings.selectionIndex);
  203.  
  204. // Invoke 'onSelected' callback (unless configured not to do that during initialization)
  205. if (this.settings.onSelectedOnInit) {
  206. this.settings.onSelected.call(this, this.selectedIndex, this.selectedValue, this.selectedText);
  207. }
  208. },
  209.  
  210. // Unbind event handlers and remove items from the ddOptions list
  211. _unbindListItems: function () {
  212. this._disable();
  213. this.ddOptions.empty();
  214. },
  215.  
  216. // Initialize
  217. _init: function (options) {
  218. this.isOpen = false;
  219. this.settings = $.extend({}, this.settings, options);
  220. var self = this;
  221.  
  222. // Load data from HTML select options or from a user-provided json object
  223. // in a list options data array
  224. this.options = [];
  225. if (this.settings.itemsSource == null) {
  226. this.selObj.find('option').each(function () {
  227. var optObj = $(this);
  228. self.options.push({
  229. text: $.trim(optObj.text()),
  230. value: optObj.val(),
  231. selected: (optObj.attr('selected') == 'selected'),
  232. description: optObj.data('description'),
  233. imageSrc: optObj.data('imagesrc')
  234. });
  235. });
  236. }
  237. else {
  238. this.options = this.settings.itemsSource;
  239. }
  240.  
  241. // Hide select element
  242. this.selObj.hide();
  243. // Insert select replacement container
  244. this.ddListObj = $('<div id="ddList-' + this.selObj.attr('id') + '" class="ddListContainer"><a></a><span class="ddListArrow"></span><ul></ul></div>');
  245. this.ddListObj.insertAfter(this.selObj);
  246. // Get newly created <a> element (which shows the selected item) and set its width
  247. this.ddSelection = this.ddListObj.find('> a');
  248. this.ddSelection.css({ width: this.settings.width });
  249. // Get newly created <ul> element (which will hold the list options) and set its width
  250. this.ddOptions = this.ddListObj.find('> ul');
  251. this.ddOptions.css({ width: this.settings.width });
  252.  
  253. // Insert all list items, bind event handlers and set 'selected' item
  254. if (!this.settings.disabled) {
  255. this.settings.disabled = this.selObj.is(':disabled');
  256. }
  257. this._bindListItems(this.settings.disabled);
  258. },
  259.  
  260. // Select a given option
  261. // Arguments:
  262. // - argsArray: An array where the first element is an object in which the user can
  263. // mark the item to be selected in 3 ways:
  264. // - through property 'index'
  265. // - through property 'text'.
  266. // - through property 'value'.
  267. select: function (argsArray) {
  268. var arg = argsArray[0];
  269. if (arg.index) {
  270. this.settings.selectionIndex = arg.index;
  271. this._selectIndex(arg.index, false);
  272. }
  273. else if (arg.text) {
  274. // Find text in the list options data array
  275. for (var i = 0; i < this.options.length; i++) {
  276. if (this.options[i].text == arg.text) {
  277. this.settings.selectionIndex = i;
  278. this._selectIndex(i);
  279. break;
  280. }
  281. }
  282. }
  283. else if (arg.value) {
  284. // Find value in the list options data array
  285. for (var i = 0; i < this.options.length; i++) {
  286. if (this.options[i].value == arg.value) {
  287. this.settings.selectionIndex = i;
  288. this._selectIndex(i);
  289. break;
  290. }
  291. }
  292. }
  293. },
  294.  
  295. // Enable/disable dropdown
  296. // Arguments:
  297. // - argsArray: An array where the first element is an boolean.
  298. // - When set to true the dropdown list is enabled
  299. // - When set to false the dropdown list is disabled
  300. enable: function (argsArray) {
  301. var isEnable = argsArray[0];
  302. if (isEnable) {
  303. this._enable();
  304. }
  305. else {
  306. this._disable();
  307. }
  308. },
  309.  
  310. // isDisabled
  311. isDisabled: function () {
  312. return this.settings.disabled;
  313. },
  314.  
  315. // setItemsSource
  316. // Arguments:
  317. // - argsArray: An array where the first element is a reference to a json
  318. // ItemsSource array.
  319. setItemsSource: function (argsArray) {
  320. // Remove event handlers and unbind the current list items
  321. var disableAccess = this.settings.disabled;
  322. this._unbindListItems();
  323. // Set itemsSource
  324. this.options = argsArray[0];
  325. // Insert all list items, bind event handlers and set 'selected' item
  326. this._bindListItems(disableAccess);
  327. },
  328.  
  329. };
  330.  
  331.  
  332. $.fn[pluginName] = function (methodOrOptions) {
  333. var instance = $(this).data(pluginName);
  334. if (instance && methodOrOptions.indexOf('_') != 0) {
  335. return instance[methodOrOptions](Array.prototype.slice.call(arguments, 1));
  336. }
  337. if (typeof methodOrOptions === 'object' || !methodOrOptions) {
  338. instance = new Plugin(this, methodOrOptions);
  339. $(this).data(pluginName, instance);
  340. return $(this);
  341. }
  342. $.error('Wrong call to ' + pluginName);
  343. };
  344.  
  345. })(jQuery);
  346. </script>
  347. </body>
  348. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement