jquery.bind-first

jquery bind-first plugin

As of 2014-06-27. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/2857/8036/jquerybind-first.js

  1. // ==UserScript==
  2. // @name jquery.bind-first
  3. // @namespace private-face
  4. // @description jquery bind-first plugin
  5. // @source https://github.com/private-face/jquery.bind-first
  6. // @copyright Vladimir Zhuravlev
  7. // @version 0.2.3
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11.  
  12. /*
  13. * jQuery.bind-first library v0.2.3
  14. * Copyright (c) 2013 Vladimir Zhuravlev
  15. *
  16. * Released under MIT License
  17. * @license
  18. *
  19. * Date: Thu Feb 6 10:13:59 ICT 2014
  20. **/
  21.  
  22. (function($) {
  23. var splitVersion = $.fn.jquery.split(".");
  24. var major = parseInt(splitVersion[0]);
  25. var minor = parseInt(splitVersion[1]);
  26.  
  27. var JQ_LT_17 = (major < 1) || (major == 1 && minor < 7);
  28. function eventsData($el) {
  29. return JQ_LT_17 ? $el.data('events') : $._data($el[0]).events;
  30. }
  31. function moveHandlerToTop($el, eventName, isDelegated) {
  32. var data = eventsData($el);
  33. var events = data[eventName];
  34.  
  35. if (!JQ_LT_17) {
  36. var handler = isDelegated ? events.splice(events.delegateCount - 1, 1)[0] : events.pop();
  37. events.splice(isDelegated ? 0 : (events.delegateCount || 0), 0, handler);
  38.  
  39. return;
  40. }
  41.  
  42. if (isDelegated) {
  43. data.live.unshift(data.live.pop());
  44. } else {
  45. events.unshift(events.pop());
  46. }
  47. }
  48. function moveEventHandlers($elems, eventsString, isDelegate) {
  49. var events = eventsString.split(/\s+/);
  50. $elems.each(function() {
  51. for (var i = 0; i < events.length; ++i) {
  52. var pureEventName = $.trim(events[i]).match(/[^\.]+/i)[0];
  53. moveHandlerToTop($(this), pureEventName, isDelegate);
  54. }
  55. });
  56. }
  57. function makeMethod(methodName) {
  58. $.fn[methodName + 'First'] = function() {
  59. var args = $.makeArray(arguments);
  60. var eventsString = args.shift();
  61.  
  62. if (eventsString) {
  63. $.fn[methodName].apply(this, arguments);
  64. moveEventHandlers(this, eventsString);
  65. }
  66.  
  67. return this;
  68. }
  69. }
  70.  
  71. // bind
  72. makeMethod('bind');
  73.  
  74. // one
  75. makeMethod('one');
  76.  
  77. // delegate
  78. $.fn.delegateFirst = function() {
  79. var args = $.makeArray(arguments);
  80. var eventsString = args[1];
  81. if (eventsString) {
  82. args.splice(0, 2);
  83. $.fn.delegate.apply(this, arguments);
  84. moveEventHandlers(this, eventsString, true);
  85. }
  86.  
  87. return this;
  88. };
  89.  
  90. // live
  91. $.fn.liveFirst = function() {
  92. var args = $.makeArray(arguments);
  93.  
  94. // live = delegate to the document
  95. args.unshift(this.selector);
  96. $.fn.delegateFirst.apply($(document), args);
  97.  
  98. return this;
  99. };
  100. // on (jquery >= 1.7)
  101. if (!JQ_LT_17) {
  102. $.fn.onFirst = function(types, selector) {
  103. var $el = $(this);
  104. var isDelegated = typeof selector === 'string';
  105.  
  106. $.fn.on.apply($el, arguments);
  107.  
  108. // events map
  109. if (typeof types === 'object') {
  110. for (type in types)
  111. if (types.hasOwnProperty(type)) {
  112. moveEventHandlers($el, type, isDelegated);
  113. }
  114. } else if (typeof types === 'string') {
  115. moveEventHandlers($el, types, isDelegated);
  116. }
  117.  
  118. return $el;
  119. };
  120. }
  121.  
  122. })(jQuery);