/webcommon/javascript2.jquery/test/unit/data/testfiles/jquery/issue236722.js

https://github.com/apache/incubator-netbeans · JavaScript · 184 lines · 95 code · 26 blank · 63 comment · 14 complexity · 74ee6f8cf125bfeb8ea2d795dfbf3ddf MD5 · raw file

  1. /**
  2. * pluginWithoutElementName to be used without an element, so $.pluginWithoutElementName();
  3. * based on boilerplate version 1.0
  4. **/
  5. (function($) {
  6. "use strict"; //ECMA5 strict modus
  7. $.extend({"pluginWithoutElementName": function(settings) {
  8. /* define vars
  9. */
  10. /* this object will be exposed to other objects */
  11. var publicObj = {};
  12. //the version number of the plugin
  13. publicObj.version = '1.0'
  14. /* this object holds functions used by the plugin boilerplate */
  15. var _helper = {
  16. /**
  17. * Call hooks, additinal parameters will be passed on to registered plugins
  18. * @param {string} name
  19. */
  20. "doHook": function(name) {
  21. var i;
  22. var pluginFunctionArgs = [];
  23. /* call function */
  24. if (_globals.plugins !== undefined) {
  25. /* remove first two arguments */
  26. for (i = 1; i < arguments.length; i++) {
  27. pluginFunctionArgs.push(arguments[i]);
  28. }
  29. $.each(_globals.plugins, function(pluginWithoutElementName, extPlugin) {
  30. if (extPlugin.__hooks !== undefined && extPlugin.__hooks[name] !== undefined) {
  31. extPlugin.__hooks[name].apply(publicObj, pluginFunctionArgs);
  32. }
  33. });
  34. }
  35. },
  36. /**
  37. * Registers a plugin
  38. * @param {string} name Name of plugin, must be unique
  39. * @param {object} object An object {("functions": {},) (, "hooks: {})}
  40. */
  41. "registerPlugin": function(name, object) {
  42. var plugin;
  43. var hooks;
  44. /* reorder plugin */
  45. hooks = $.extend(true, {}, object.hooks);
  46. plugin = object.functions !== undefined ? object.functions : {};
  47. plugin.__hooks = hooks;
  48. /* add plugin */
  49. _globals.plugins[name] = plugin;
  50. },
  51. /**
  52. * Calls a plugin function, all additional arguments will be passed on
  53. * @param {string} pluginWithoutElementName
  54. * @param {string} pluginFunctionName
  55. */
  56. "callPluginFunction": function(pluginWithoutElementName, pluginFunctionName) {
  57. var i;
  58. /* remove first two arguments */
  59. var pluginFunctionArgs = [];
  60. for (i = 2; i < arguments.length; i++) {
  61. pluginFunctionArgs.push(arguments[i]);
  62. }
  63. /* call function */
  64. _globals.plugins[pluginWithoutElementName][pluginFunctionName].apply(null, pluginFunctionArgs);
  65. },
  66. /**
  67. * Checks dependencies based on the _globals.dependencies object
  68. * @returns {boolean}
  69. */
  70. "checkDependencies": function() {
  71. var dependenciesPresent = true;
  72. for (var libName in _globals.dependencies) {
  73. var callback = _globals.dependencies[libName];
  74. if (callback.call() === false) {
  75. console.error('jquery.pluginWithoutElementName: Library ' + libName + ' not found! This may give unexpected results or errors.')
  76. dependenciesPresent = false;
  77. }
  78. }
  79. return dependenciesPresent;
  80. }
  81. };
  82. /* this object holds all global variables */
  83. var _globals = {};
  84. /* handle settings */
  85. var defaultSettings = {
  86. };
  87. _globals.settings = {};
  88. if ($.isPlainObject(settings) === true) {
  89. _globals.settings = $.extend(true, {}, defaultSettings, settings);
  90. } else {
  91. _globals.settings = defaultSettings;
  92. }
  93. /* this object contains a number of functions to test for dependencies,
  94. * functies should return TRUE if the library/browser/etc is present
  95. */
  96. _globals.dependencies = {
  97. /* check for jQuery 1.6+ to be present */
  98. "jquery1.6+": function() {
  99. var jqv, jqv_main, jqv_sub;
  100. if (window.jQuery) {
  101. jqv = jQuery().jquery.split('.');
  102. jqv_main = parseInt(jqv[0], 10);
  103. jqv_sub = parseInt(jqv[1], 10);
  104. if (jqv_main > 1 || (jqv_main === 1 && jqv_sub >= 6)) {
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. }
  111. };
  112. _helper.checkDependencies();
  113. //this object holds all plugins
  114. _globals.plugins = {};
  115. /**
  116. * Init function
  117. **/
  118. publicObj.init = function() {
  119. };
  120. /**
  121. * Public function
  122. */
  123. publicObj.myFunction = function() {
  124. _helper.doHook('myFunction');
  125. };
  126. /**
  127. * Registers a plugin
  128. * @param {string} name Name of plugin, must be unique
  129. * @param {object} object An object {("functions": {},) (, "hooks: {}) (, "targetpluginWithoutElementNames": [])}
  130. */
  131. publicObj.registerPlugin = function(name, object) {
  132. _helper.registerPlugin(name, object);
  133. };
  134. /**
  135. * Calls a plugin function, all additional arguments will be passed on
  136. * @param {string} pluginWithoutElementName
  137. * @param {string} pluginFunctionName
  138. */
  139. publicObj.callPluginFunction = function(pluginWithoutElementName, pluginFunctionName) {
  140. /* call function */
  141. _helper.callPluginFunction.apply(null, arguments);
  142. };
  143. /**
  144. * Private function
  145. **/
  146. function myFunction(myParam) {
  147. //call hook
  148. _helper.doHook('onMyFunctionCalled', myParam);
  149. }
  150. /* initialize pluginWithoutElementName
  151. */
  152. $(document).trigger('pluginWithoutElementName.beforeInit', publicObj, settings); //trigger event on document
  153. publicObj.init();
  154. $(document).trigger('pluginWithoutElementName.init', publicObj); //trigger event on document
  155. return publicObj;
  156. }
  157. });
  158. })(jQuery);