UNPKG

@util.js/jquery

Version:

JavaScript utility methods for jQuery

75 lines (74 loc) 3.01 kB
"use strict"; /** * JavaScript utility methods for [jQuery]{@link https://jquery.com/} * @public * @class */ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass")); var JQueryUtils = /*#__PURE__*/function () { function JQueryUtils() { (0, _classCallCheck2["default"])(this, JQueryUtils); } (0, _createClass2["default"])(JQueryUtils, [{ key: "loadjQuery", value: /** * Loads jQuery asynchronously and sends a notification when loading has finished * * @example * const jQueryUtils = require("@util.js/jquery"); * jQueryUtils * .loadjQuery({ window }) * .then($ => console.log($)) * .catch(console.log); * * @example * const jQueryUtils = require("@util.js/jquery"); * jQueryUtils * .loadjQuery({ * src: "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js", * window * }) * .then($ => console.log($)) * .catch(console.log); * * @example * const jQueryUtils = require("@util.js/jquery"); * jQueryUtils.loadjQuery({ window }, (error, $) => { * if (error) return console.log(error); * console.log($); * }); * * @param {Object} params * @param {string} [params.src=https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js] - The jQuery script to load * @param {Window} params.window - The browser's window * @param {Function} [callback] - A callback function that takes two arguments: error and jQuery * @returns {(undefined|Promise)} undefined if a callback is specified or a Promise that returns jQuery once jQuery has finished loading * @throws {Error} If params is undefined, null, or params has an invalid window object * @public * @instance * @function */ function loadjQuery(params, callback) { if (!callback) return promises().callCallback(this, this.loadjQuery, params); var src = params.src || "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"; var wnd = params.window; var scriptElement = wnd.document.createElement("script"); scriptElement.async = 1; scriptElement.onload = function () { if (wnd.jQuery) callback(null, wnd.jQuery);else callback(new Error("We failed to load jQuery from " + src + ".")); }; scriptElement.src = src; var existingScriptElement = wnd.document.getElementsByTagName("script")[0]; existingScriptElement.parentNode.insertBefore(scriptElement, existingScriptElement); } }]); return JQueryUtils; }(); module.exports = JQueryUtils; var dependencies = {}; function promises() { return dependencies["@util.js/promises"] || (dependencies["@util.js/promises"] = require("@util.js/promises")); }