You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1060 lines
35 KiB

2 years ago
  1. /*
  2. Uniform v2.1.2
  3. Copyright © 2009 Josh Pyles / Pixelmatrix Design LLC
  4. http://pixelmatrixdesign.com
  5. Requires jQuery 1.3 or newer
  6. Much thanks to Thomas Reynolds and Buck Wilson for their help and advice on
  7. this.
  8. Disabling text selection is made possible by Mathias Bynens
  9. <http://mathiasbynens.be/> and his noSelect plugin.
  10. <https://github.com/mathiasbynens/jquery-noselect>, which is embedded.
  11. Also, thanks to David Kaneda and Eugene Bond for their contributions to the
  12. plugin.
  13. Tyler Akins has also rewritten chunks of the plugin, helped close many issues,
  14. and ensured version 2 got out the door.
  15. License:
  16. MIT License - http://www.opensource.org/licenses/mit-license.php
  17. Enjoy!
  18. */
  19. /*global jQuery, document, navigator*/
  20. (function (wind, $, undef) {
  21. "use strict";
  22. /**
  23. * Use .prop() if jQuery supports it, otherwise fall back to .attr()
  24. *
  25. * @param jQuery $el jQuery'd element on which we're calling attr/prop
  26. * @param ... All other parameters are passed to jQuery's function
  27. * @return The result from jQuery
  28. */
  29. function attrOrProp($el) {
  30. var args = Array.prototype.slice.call(arguments, 1);
  31. if ($el.prop) {
  32. // jQuery 1.6+
  33. return $el.prop.apply($el, args);
  34. }
  35. // jQuery 1.5 and below
  36. return $el.attr.apply($el, args);
  37. }
  38. /**
  39. * For backwards compatibility with older jQuery libraries, only bind
  40. * one thing at a time. Also, this function adds our namespace to
  41. * events in one consistent location, shrinking the minified code.
  42. *
  43. * The properties on the events object are the names of the events
  44. * that we are supposed to add to. It can be a space separated list.
  45. * The namespace will be added automatically.
  46. *
  47. * @param jQuery $el
  48. * @param Object options Uniform options for this element
  49. * @param Object events Events to bind, properties are event names
  50. */
  51. function bindMany($el, options, events) {
  52. var name, namespaced;
  53. for (name in events) {
  54. if (events.hasOwnProperty(name)) {
  55. namespaced = name.replace(/ |$/g, options.eventNamespace);
  56. $el.bind(namespaced, events[name]);
  57. }
  58. }
  59. }
  60. /**
  61. * Bind the hover, active, focus, and blur UI updates
  62. *
  63. * @param jQuery $el Original element
  64. * @param jQuery $target Target for the events (our div/span)
  65. * @param Object options Uniform options for the element $target
  66. */
  67. function bindUi($el, $target, options) {
  68. bindMany($el, options, {
  69. focus: function () {
  70. $target.addClass(options.focusClass);
  71. },
  72. blur: function () {
  73. $target.removeClass(options.focusClass);
  74. $target.removeClass(options.activeClass);
  75. },
  76. mouseenter: function () {
  77. $target.addClass(options.hoverClass);
  78. },
  79. mouseleave: function () {
  80. $target.removeClass(options.hoverClass);
  81. $target.removeClass(options.activeClass);
  82. },
  83. "mousedown touchbegin": function () {
  84. if (!$el.is(":disabled")) {
  85. $target.addClass(options.activeClass);
  86. }
  87. },
  88. "mouseup touchend": function () {
  89. $target.removeClass(options.activeClass);
  90. }
  91. });
  92. }
  93. /**
  94. * Remove the hover, focus, active classes.
  95. *
  96. * @param jQuery $el Element with classes
  97. * @param Object options Uniform options for the element
  98. */
  99. function classClearStandard($el, options) {
  100. $el.removeClass(options.hoverClass + " " + options.focusClass + " " + options.activeClass);
  101. }
  102. /**
  103. * Add or remove a class, depending on if it's "enabled"
  104. *
  105. * @param jQuery $el Element that has the class added/removed
  106. * @param String className Class or classes to add/remove
  107. * @param Boolean enabled True to add the class, false to remove
  108. */
  109. function classUpdate($el, className, enabled) {
  110. if (enabled) {
  111. $el.addClass(className);
  112. } else {
  113. $el.removeClass(className);
  114. }
  115. }
  116. /**
  117. * Updating the "checked" property can be a little tricky. This
  118. * changed in jQuery 1.6 and now we can pass booleans to .prop().
  119. * Prior to that, one either adds an attribute ("checked=checked") or
  120. * removes the attribute.
  121. *
  122. * @param jQuery $tag Our Uniform span/div
  123. * @param jQuery $el Original form element
  124. * @param Object options Uniform options for this element
  125. */
  126. function classUpdateChecked($tag, $el, options) {
  127. var c = "checked",
  128. isChecked = $el.is(":" + c);
  129. if ($el.prop) {
  130. // jQuery 1.6+
  131. $el.prop(c, isChecked);
  132. } else {
  133. // jQuery 1.5 and below
  134. if (isChecked) {
  135. $el.attr(c, c);
  136. } else {
  137. $el.removeAttr(c);
  138. }
  139. }
  140. classUpdate($tag, options.checkedClass, isChecked);
  141. }
  142. /**
  143. * Set or remove the "disabled" class for disabled elements, based on
  144. * if the element is detected to be disabled.
  145. *
  146. * @param jQuery $tag Our Uniform span/div
  147. * @param jQuery $el Original form element
  148. * @param Object options Uniform options for this element
  149. */
  150. function classUpdateDisabled($tag, $el, options) {
  151. classUpdate($tag, options.disabledClass, $el.is(":disabled"));
  152. }
  153. /**
  154. * Wrap an element inside of a container or put the container next
  155. * to the element. See the code for examples of the different methods.
  156. *
  157. * Returns the container that was added to the HTML.
  158. *
  159. * @param jQuery $el Element to wrap
  160. * @param jQuery $container Add this new container around/near $el
  161. * @param String method One of "after", "before" or "wrap"
  162. * @return $container after it has been cloned for adding to $el
  163. */
  164. function divSpanWrap($el, $container, method) {
  165. switch (method) {
  166. case "after":
  167. // Result: <element /> <container />
  168. $el.after($container);
  169. return $el.next();
  170. case "before":
  171. // Result: <container /> <element />
  172. $el.before($container);
  173. return $el.prev();
  174. case "wrap":
  175. // Result: <container> <element /> </container>
  176. $el.wrap($container);
  177. return $el.parent();
  178. }
  179. return null;
  180. }
  181. /**
  182. * Create a div/span combo for uniforming an element
  183. *
  184. * @param jQuery $el Element to wrap
  185. * @param Object options Options for the element, set by the user
  186. * @param Object divSpanConfig Options for how we wrap the div/span
  187. * @return Object Contains the div and span as properties
  188. */
  189. function divSpan($el, options, divSpanConfig) {
  190. var $div, $span, id;
  191. if (!divSpanConfig) {
  192. divSpanConfig = {};
  193. }
  194. divSpanConfig = $.extend({
  195. bind: {},
  196. divClass: null,
  197. divWrap: "wrap",
  198. spanClass: null,
  199. spanHtml: null,
  200. spanWrap: "wrap"
  201. }, divSpanConfig);
  202. $div = $('<div />');
  203. $span = $('<span />');
  204. // Automatically hide this div/span if the element is hidden.
  205. // Do not hide if the element is hidden because a parent is hidden.
  206. if (options.autoHide && $el.is(':hidden') && $el.css('display') === 'none') {
  207. $div.hide();
  208. }
  209. if (divSpanConfig.divClass) {
  210. $div.addClass(divSpanConfig.divClass);
  211. }
  212. if (options.wrapperClass) {
  213. $div.addClass(options.wrapperClass);
  214. }
  215. if (divSpanConfig.spanClass) {
  216. $span.addClass(divSpanConfig.spanClass);
  217. }
  218. id = attrOrProp($el, 'id');
  219. if (options.useID && id) {
  220. attrOrProp($div, 'id', options.idPrefix + '-' + id);
  221. }
  222. if (divSpanConfig.spanHtml) {
  223. $span.html(divSpanConfig.spanHtml);
  224. }
  225. $div = divSpanWrap($el, $div, divSpanConfig.divWrap);
  226. $span = divSpanWrap($el, $span, divSpanConfig.spanWrap);
  227. classUpdateDisabled($div, $el, options);
  228. return {
  229. div: $div,
  230. span: $span
  231. };
  232. }
  233. /**
  234. * Wrap an element with a span to apply a global wrapper class
  235. *
  236. * @param jQuery $el Element to wrap
  237. * @param object options
  238. * @return jQuery Wrapper element
  239. */
  240. function wrapWithWrapperClass($el, options) {
  241. var $span;
  242. if (!options.wrapperClass) {
  243. return null;
  244. }
  245. $span = $('<span />').addClass(options.wrapperClass);
  246. $span = divSpanWrap($el, $span, "wrap");
  247. return $span;
  248. }
  249. /**
  250. * Test if high contrast mode is enabled.
  251. *
  252. * In high contrast mode, background images can not be set and
  253. * they are always returned as 'none'.
  254. *
  255. * @return boolean True if in high contrast mode
  256. */
  257. function highContrast() {
  258. var c, $div, el, rgb;
  259. // High contrast mode deals with white and black
  260. rgb = 'rgb(120,2,153)';
  261. $div = $('<div style="width:0;height:0;color:' + rgb + '">');
  262. $('body').append($div);
  263. el = $div.get(0);
  264. // $div.css() will get the style definition, not
  265. // the actually displaying style
  266. if (wind.getComputedStyle) {
  267. c = wind.getComputedStyle(el, '').color;
  268. } else {
  269. c = (el.currentStyle || el.style || {}).color;
  270. }
  271. $div.remove();
  272. return c.replace(/ /g, '') !== rgb;
  273. }
  274. /**
  275. * Change text into safe HTML
  276. *
  277. * @param String text
  278. * @return String HTML version
  279. */
  280. function htmlify(text) {
  281. if (!text) {
  282. return "";
  283. }
  284. return $('<span />').text(text).html();
  285. }
  286. /**
  287. * If not MSIE, return false.
  288. * If it is, return the version number.
  289. *
  290. * @return false|number
  291. */
  292. function isMsie() {
  293. return navigator.cpuClass && !navigator.product;
  294. }
  295. /**
  296. * Return true if this version of IE allows styling
  297. *
  298. * @return boolean
  299. */
  300. function isMsieSevenOrNewer() {
  301. if (wind.XMLHttpRequest !== undefined) {
  302. return true;
  303. }
  304. return false;
  305. }
  306. /**
  307. * Test if the element is a multiselect
  308. *
  309. * @param jQuery $el Element
  310. * @return boolean true/false
  311. */
  312. function isMultiselect($el) {
  313. var elSize;
  314. if ($el[0].multiple) {
  315. return true;
  316. }
  317. elSize = attrOrProp($el, "size");
  318. if (!elSize || elSize <= 1) {
  319. return false;
  320. }
  321. return true;
  322. }
  323. /**
  324. * Meaningless utility function. Used mostly for improving minification.
  325. *
  326. * @return false
  327. */
  328. function returnFalse() {
  329. return false;
  330. }
  331. /**
  332. * noSelect plugin, very slightly modified
  333. * http://mths.be/noselect v1.0.3
  334. *
  335. * @param jQuery $elem Element that we don't want to select
  336. * @param Object options Uniform options for the element
  337. */
  338. function noSelect($elem, options) {
  339. var none = 'none';
  340. bindMany($elem, options, {
  341. 'selectstart dragstart mousedown': returnFalse
  342. });
  343. $elem.css({
  344. MozUserSelect: none,
  345. msUserSelect: none,
  346. webkitUserSelect: none,
  347. userSelect: none
  348. });
  349. }
  350. /**
  351. * Updates the filename tag based on the value of the real input
  352. * element.
  353. *
  354. * @param jQuery $el Actual form element
  355. * @param jQuery $filenameTag Span/div to update
  356. * @param Object options Uniform options for this element
  357. */
  358. function setFilename($el, $filenameTag, options) {
  359. var filename = $el.val();
  360. if (filename === "") {
  361. filename = options.fileDefaultHtml;
  362. } else {
  363. filename = filename.split(/[\/\\]+/);
  364. filename = filename[(filename.length - 1)];
  365. }
  366. $filenameTag.text(filename);
  367. }
  368. /**
  369. * Function from jQuery to swap some CSS values, run a callback,
  370. * then restore the CSS. Modified to pass JSLint and handle undefined
  371. * values with 'use strict'.
  372. *
  373. * @param jQuery $el Element
  374. * @param object newCss CSS values to swap out
  375. * @param Function callback Function to run
  376. */
  377. function swap($elements, newCss, callback) {
  378. var restore, item;
  379. restore = [];
  380. $elements.each(function () {
  381. var name;
  382. for (name in newCss) {
  383. if (Object.prototype.hasOwnProperty.call(newCss, name)) {
  384. restore.push({
  385. el: this,
  386. name: name,
  387. old: this.style[name]
  388. });
  389. this.style[name] = newCss[name];
  390. }
  391. }
  392. });
  393. callback();
  394. while (restore.length) {
  395. item = restore.pop();
  396. item.el.style[item.name] = item.old;
  397. }
  398. }
  399. /**
  400. * The browser doesn't provide sizes of elements that are not visible.
  401. * This will clone an element and add it to the DOM for calculations.
  402. *
  403. * @param jQuery $el
  404. * @param String method
  405. */
  406. function sizingInvisible($el, callback) {
  407. var targets;
  408. // We wish to target ourselves and any parents as long as
  409. // they are not visible
  410. targets = $el.parents();
  411. targets.push($el[0]);
  412. targets = targets.not(':visible');
  413. swap(targets, {
  414. visibility: "hidden",
  415. display: "block",
  416. position: "absolute"
  417. }, callback);
  418. }
  419. /**
  420. * Standard way to unwrap the div/span combination from an element
  421. *
  422. * @param jQuery $el Element that we wish to preserve
  423. * @param Object options Uniform options for the element
  424. * @return Function This generated function will perform the given work
  425. */
  426. function unwrapUnwrapUnbindFunction($el, options) {
  427. return function () {
  428. $el.unwrap().unwrap().unbind(options.eventNamespace);
  429. };
  430. }
  431. var allowStyling = true, // False if IE6 or other unsupported browsers
  432. highContrastTest = false, // Was the high contrast test ran?
  433. uniformHandlers = [ // Objects that take care of "unification"
  434. {
  435. // Buttons
  436. match: function ($el) {
  437. return $el.is("a, button, :submit, :reset, input[type='button']");
  438. },
  439. apply: function ($el, options) {
  440. var $div, defaultSpanHtml, ds, getHtml, doingClickEvent;
  441. defaultSpanHtml = options.submitDefaultHtml;
  442. if ($el.is(":reset")) {
  443. defaultSpanHtml = options.resetDefaultHtml;
  444. }
  445. if ($el.is("a, button")) {
  446. // Use the HTML inside the tag
  447. getHtml = function () {
  448. return $el.html() || defaultSpanHtml;
  449. };
  450. } else {
  451. // Use the value property of the element
  452. getHtml = function () {
  453. return htmlify(attrOrProp($el, "value")) || defaultSpanHtml;
  454. };
  455. }
  456. ds = divSpan($el, options, {
  457. divClass: options.buttonClass,
  458. spanHtml: getHtml()
  459. });
  460. $div = ds.div;
  461. bindUi($el, $div, options);
  462. doingClickEvent = false;
  463. bindMany($div, options, {
  464. "click touchend": function () {
  465. var ev, res, target, href;
  466. if (doingClickEvent) {
  467. return;
  468. }
  469. if ($el.is(':disabled')) {
  470. return;
  471. }
  472. doingClickEvent = true;
  473. if ($el[0].dispatchEvent) {
  474. ev = document.createEvent("MouseEvents");
  475. ev.initEvent("click", true, true);
  476. res = $el[0].dispatchEvent(ev);
  477. if ($el.is('a') && res) {
  478. target = attrOrProp($el, 'target');
  479. href = attrOrProp($el, 'href');
  480. if (!target || target === '_self') {
  481. document.location.href = href;
  482. } else {
  483. wind.open(href, target);
  484. }
  485. }
  486. } else {
  487. $el.click();
  488. }
  489. doingClickEvent = false;
  490. }
  491. });
  492. noSelect($div, options);
  493. return {
  494. remove: function () {
  495. // Move $el out
  496. $div.after($el);
  497. // Remove div and span
  498. $div.remove();
  499. // Unbind events
  500. $el.unbind(options.eventNamespace);
  501. return $el;
  502. },
  503. update: function () {
  504. classClearStandard($div, options);
  505. classUpdateDisabled($div, $el, options);
  506. $el.detach();
  507. ds.span.html(getHtml()).append($el);
  508. }
  509. };
  510. }
  511. },
  512. {
  513. // Checkboxes
  514. match: function ($el) {
  515. return $el.is(":checkbox");
  516. },
  517. apply: function ($el, options) {
  518. var ds, $div, $span;
  519. ds = divSpan($el, options, {
  520. divClass: options.checkboxClass
  521. });
  522. $div = ds.div;
  523. $span = ds.span;
  524. // Add focus classes, toggling, active, etc.
  525. bindUi($el, $div, options);
  526. bindMany($el, options, {
  527. "click touchend": function () {
  528. classUpdateChecked($span, $el, options);
  529. }
  530. });
  531. classUpdateChecked($span, $el, options);
  532. return {
  533. remove: unwrapUnwrapUnbindFunction($el, options),
  534. update: function () {
  535. classClearStandard($div, options);
  536. $span.removeClass(options.checkedClass);
  537. classUpdateChecked($span, $el, options);
  538. classUpdateDisabled($div, $el, options);
  539. }
  540. };
  541. }
  542. },
  543. {
  544. // File selection / uploads
  545. match: function ($el) {
  546. return $el.is(":file");
  547. },
  548. apply: function ($el, options) {
  549. var ds, $div, $filename, $button;
  550. // The "span" is the button
  551. ds = divSpan($el, options, {
  552. divClass: options.fileClass,
  553. spanClass: options.fileButtonClass,
  554. spanHtml: options.fileButtonHtml,
  555. spanWrap: "after"
  556. });
  557. $div = ds.div;
  558. $button = ds.span;
  559. $filename = $("<span />").html(options.fileDefaultHtml);
  560. $filename.addClass(options.filenameClass);
  561. $filename = divSpanWrap($el, $filename, "after");
  562. // Set the size
  563. if (!attrOrProp($el, "size")) {
  564. attrOrProp($el, "size", $div.width() / 10);
  565. }
  566. // Actions
  567. function filenameUpdate() {
  568. setFilename($el, $filename, options);
  569. }
  570. bindUi($el, $div, options);
  571. // Account for input saved across refreshes
  572. filenameUpdate();
  573. // IE7 doesn't fire onChange until blur or second fire.
  574. if (isMsie()) {
  575. // IE considers browser chrome blocking I/O, so it
  576. // suspends tiemouts until after the file has
  577. // been selected.
  578. bindMany($el, options, {
  579. click: function () {
  580. $el.trigger("change");
  581. setTimeout(filenameUpdate, 0);
  582. }
  583. });
  584. } else {
  585. // All other browsers behave properly
  586. bindMany($el, options, {
  587. change: filenameUpdate
  588. });
  589. }
  590. noSelect($filename, options);
  591. noSelect($button, options);
  592. return {
  593. remove: function () {
  594. // Remove filename and button
  595. $filename.remove();
  596. $button.remove();
  597. // Unwrap parent div, remove events
  598. return $el.unwrap().unbind(options.eventNamespace);
  599. },
  600. update: function () {
  601. classClearStandard($div, options);
  602. setFilename($el, $filename, options);
  603. classUpdateDisabled($div, $el, options);
  604. }
  605. };
  606. }
  607. },
  608. {
  609. // Input fields (text)
  610. match: function ($el) {
  611. if ($el.is("input")) {
  612. var t = (" " + attrOrProp($el, "type") + " ").toLowerCase(),
  613. allowed = " color date datetime datetime-local email month number password search tel text time url week ";
  614. return allowed.indexOf(t) >= 0;
  615. }
  616. return false;
  617. },
  618. apply: function ($el, options) {
  619. var elType, $wrapper;
  620. elType = attrOrProp($el, "type");
  621. $el.addClass(options.inputClass);
  622. $wrapper = wrapWithWrapperClass($el, options);
  623. bindUi($el, $el, options);
  624. if (options.inputAddTypeAsClass) {
  625. $el.addClass(elType);
  626. }
  627. return {
  628. remove: function () {
  629. $el.removeClass(options.inputClass);
  630. if (options.inputAddTypeAsClass) {
  631. $el.removeClass(elType);
  632. }
  633. if ($wrapper) {
  634. $el.unwrap();
  635. }
  636. },
  637. update: returnFalse
  638. };
  639. }
  640. },
  641. {
  642. // Radio buttons
  643. match: function ($el) {
  644. return $el.is(":radio");
  645. },
  646. apply: function ($el, options) {
  647. var ds, $div, $span;
  648. ds = divSpan($el, options, {
  649. divClass: options.radioClass
  650. });
  651. $div = ds.div;
  652. $span = ds.span;
  653. // Add classes for focus, handle active, checked
  654. bindUi($el, $div, options);
  655. bindMany($el, options, {
  656. "click touchend": function () {
  657. // Find all radios with the same name, then update
  658. // them with $.uniform.update() so the right
  659. // per-element options are used
  660. $.uniform.update($(':radio[name="' + attrOrProp($el, "name") + '"]'));
  661. }
  662. });
  663. classUpdateChecked($span, $el, options);
  664. return {
  665. remove: unwrapUnwrapUnbindFunction($el, options),
  666. update: function () {
  667. classClearStandard($div, options);
  668. classUpdateChecked($span, $el, options);
  669. classUpdateDisabled($div, $el, options);
  670. }
  671. };
  672. }
  673. },
  674. {
  675. // Select lists, but do not style multiselects here
  676. match: function ($el) {
  677. if ($el.is("select") && !isMultiselect($el)) {
  678. return true;
  679. }
  680. return false;
  681. },
  682. apply: function ($el, options) {
  683. var ds, $div, $span, origElemWidth;
  684. if (options.selectAutoWidth) {
  685. sizingInvisible($el, function () {
  686. origElemWidth = $el.width();
  687. });
  688. }
  689. ds = divSpan($el, options, {
  690. divClass: options.selectClass,
  691. spanHtml: ($el.find(":selected:first") || $el.find("option:first")).html(),
  692. spanWrap: "before"
  693. });
  694. $div = ds.div;
  695. $span = ds.span;
  696. if (options.selectAutoWidth) {
  697. // Use the width of the select and adjust the
  698. // span and div accordingly
  699. sizingInvisible($el, function () {
  700. // Force "display: block" - related to bug #287
  701. swap($([ $span[0], $div[0] ]), {
  702. display: "block"
  703. }, function () {
  704. var spanPad;
  705. spanPad = $span.outerWidth() - $span.width();
  706. $div.width(origElemWidth + spanPad);
  707. $span.width(origElemWidth);
  708. });
  709. });
  710. } else {
  711. // Force the select to fill the size of the div
  712. $div.addClass('fixedWidth');
  713. }
  714. // Take care of events
  715. bindUi($el, $div, options);
  716. bindMany($el, options, {
  717. change: function () {
  718. $span.html($el.find(":selected").html());
  719. $div.removeClass(options.activeClass);
  720. },
  721. "click touchend": function () {
  722. // IE7 and IE8 may not update the value right
  723. // until after click event - issue #238
  724. var selHtml = $el.find(":selected").html();
  725. if ($span.html() !== selHtml) {
  726. // Change was detected
  727. // Fire the change event on the select tag
  728. $el.trigger('change');
  729. }
  730. },
  731. keyup: function () {
  732. $span.html($el.find(":selected").html());
  733. }
  734. });
  735. noSelect($span, options);
  736. return {
  737. remove: function () {
  738. // Remove sibling span
  739. $span.remove();
  740. // Unwrap parent div
  741. $el.unwrap().unbind(options.eventNamespace);
  742. return $el;
  743. },
  744. update: function () {
  745. if (options.selectAutoWidth) {
  746. // Easier to remove and reapply formatting
  747. $.uniform.restore($el);
  748. $el.uniform(options);
  749. } else {
  750. classClearStandard($div, options);
  751. // Reset current selected text
  752. $span.html($el.find(":selected").html());
  753. classUpdateDisabled($div, $el, options);
  754. }
  755. }
  756. };
  757. }
  758. },
  759. {
  760. // Select lists - multiselect lists only
  761. match: function ($el) {
  762. if ($el.is("select") && isMultiselect($el)) {
  763. return true;
  764. }
  765. return false;
  766. },
  767. apply: function ($el, options) {
  768. var $wrapper;
  769. $el.addClass(options.selectMultiClass);
  770. $wrapper = wrapWithWrapperClass($el, options);
  771. bindUi($el, $el, options);
  772. return {
  773. remove: function () {
  774. $el.removeClass(options.selectMultiClass);
  775. if ($wrapper) {
  776. $el.unwrap();
  777. }
  778. },
  779. update: returnFalse
  780. };
  781. }
  782. },
  783. {
  784. // Textareas
  785. match: function ($el) {
  786. return $el.is("textarea");
  787. },
  788. apply: function ($el, options) {
  789. var $wrapper;
  790. $el.addClass(options.textareaClass);
  791. $wrapper = wrapWithWrapperClass($el, options);
  792. bindUi($el, $el, options);
  793. return {
  794. remove: function () {
  795. $el.removeClass(options.textareaClass);
  796. if ($wrapper) {
  797. $el.unwrap();
  798. }
  799. },
  800. update: returnFalse
  801. };
  802. }
  803. }
  804. ];
  805. // IE6 can't be styled - can't set opacity on select
  806. if (isMsie() && !isMsieSevenOrNewer()) {
  807. allowStyling = false;
  808. }
  809. $.uniform = {
  810. // Default options that can be overridden globally or when uniformed
  811. // globally: $.uniform.defaults.fileButtonHtml = "Pick A File";
  812. // on uniform: $('input').uniform({fileButtonHtml: "Pick a File"});
  813. defaults: {
  814. activeClass: "active",
  815. autoHide: true,
  816. buttonClass: "button",
  817. checkboxClass: "checker",
  818. checkedClass: "checked",
  819. disabledClass: "disabled",
  820. eventNamespace: ".uniform",
  821. fileButtonClass: "action",
  822. fileButtonHtml: "Choose File",
  823. fileClass: "uploader",
  824. fileDefaultHtml: "No file selected",
  825. filenameClass: "filename",
  826. focusClass: "focus",
  827. hoverClass: "hover",
  828. idPrefix: "uniform",
  829. inputAddTypeAsClass: true,
  830. inputClass: "uniform-input",
  831. radioClass: "radio",
  832. resetDefaultHtml: "Reset",
  833. resetSelector: false, // We'll use our own function when you don't specify one
  834. selectAutoWidth: true,
  835. selectClass: "selector",
  836. selectMultiClass: "uniform-multiselect",
  837. submitDefaultHtml: "Submit", // Only text allowed
  838. textareaClass: "uniform",
  839. useID: true,
  840. wrapperClass: null
  841. },
  842. // All uniformed elements - DOM objects
  843. elements: []
  844. };
  845. $.fn.uniform = function (options) {
  846. var el = this;
  847. options = $.extend({}, $.uniform.defaults, options);
  848. // If we are in high contrast mode, do not allow styling
  849. if (!highContrastTest) {
  850. highContrastTest = true;
  851. if (highContrast()) {
  852. allowStyling = false;
  853. }
  854. }
  855. // Only uniform on browsers that work
  856. if (!allowStyling) {
  857. return this;
  858. }
  859. // Code for specifying a reset button
  860. if (options.resetSelector) {
  861. $(options.resetSelector).mouseup(function () {
  862. wind.setTimeout(function () {
  863. $.uniform.update(el);
  864. }, 10);
  865. });
  866. }
  867. return this.each(function () {
  868. var $el = $(this), i, handler, callbacks;
  869. // Avoid uniforming elements already uniformed - just update
  870. if ($el.data("uniformed")) {
  871. $.uniform.update($el);
  872. return;
  873. }
  874. // See if we have any handler for this type of element
  875. for (i = 0; i < uniformHandlers.length; i = i + 1) {
  876. handler = uniformHandlers[i];
  877. if (handler.match($el, options)) {
  878. callbacks = handler.apply($el, options);
  879. $el.data("uniformed", callbacks);
  880. // Store element in our global array
  881. $.uniform.elements.push($el.get(0));
  882. return;
  883. }
  884. }
  885. // Could not style this element
  886. });
  887. };
  888. $.uniform.restore = $.fn.uniform.restore = function (elem) {
  889. if (elem === undef) {
  890. elem = $.uniform.elements;
  891. }
  892. $(elem).each(function () {
  893. var $el = $(this), index, elementData;
  894. elementData = $el.data("uniformed");
  895. // Skip elements that are not uniformed
  896. if (!elementData) {
  897. return;
  898. }
  899. // Unbind events, remove additional markup that was added
  900. elementData.remove();
  901. // Remove item from list of uniformed elements
  902. index = $.inArray(this, $.uniform.elements);
  903. if (index >= 0) {
  904. $.uniform.elements.splice(index, 1);
  905. }
  906. $el.removeData("uniformed");
  907. });
  908. };
  909. $.uniform.update = $.fn.uniform.update = function (elem) {
  910. if (elem === undef) {
  911. elem = $.uniform.elements;
  912. }
  913. $(elem).each(function () {
  914. var $el = $(this), elementData;
  915. elementData = $el.data("uniformed");
  916. // Skip elements that are not uniformed
  917. if (!elementData) {
  918. return;
  919. }
  920. elementData.update($el, elementData.options);
  921. });
  922. };
  923. }(this, jQuery));