@g2crowd/widget
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -50,2 +50,3 @@ // @format | ||
import camelize from "./camelize.js"; | ||
import queue from "./queue.js"; | ||
import initiationStrategies from "./initiationStrategies.js"; | ||
@@ -63,2 +64,3 @@ import selectorBuilder from "./selectorBuilder.js"; | ||
const widgetQueue = queue(); | ||
const strategies = initiationStrategies({ | ||
@@ -120,3 +122,6 @@ nextTick(pluginFn, $$, options, ready) { | ||
const options = $.extend({}, pluginFn.defaults, extractOptions(data, pluginName)); | ||
strategies.get(pluginFn.init)(pluginFn, $$, options, ready); | ||
widgetQueue.add(() => { | ||
strategies.get(pluginFn.init)(pluginFn, $$, options, ready); | ||
}); | ||
widgetQueue.flush(); | ||
$$.data(`vvidget:${name}`, true); | ||
@@ -132,3 +137,2 @@ } | ||
names.split(' ').forEach(name => loadWidget($$, name, $$.data())); | ||
$$.trigger('vvidget:ready'); | ||
}); | ||
@@ -135,0 +139,0 @@ }; |
@@ -0,4 +1,131 @@ | ||
import 'core-js/modules/es6.array.find'; | ||
import 'core-js/modules/es6.regexp.split'; | ||
import 'core-js/modules/es6.function.name'; | ||
import $ from 'jquery'; | ||
import { extractOptions } from '@g2crowd/extract-options'; | ||
import 'core-js/modules/es6.regexp.replace'; | ||
import 'core-js/modules/es6.promise'; | ||
import 'core-js/modules/es6.object.to-string'; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _inherits(subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) _setPrototypeOf(subClass, superClass); | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function isNativeReflectConstruct() { | ||
if (typeof Reflect === "undefined" || !Reflect.construct) return false; | ||
if (Reflect.construct.sham) return false; | ||
if (typeof Proxy === "function") return true; | ||
try { | ||
Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function _construct(Parent, args, Class) { | ||
if (isNativeReflectConstruct()) { | ||
_construct = Reflect.construct; | ||
} else { | ||
_construct = function _construct(Parent, args, Class) { | ||
var a = [null]; | ||
a.push.apply(a, args); | ||
var Constructor = Function.bind.apply(Parent, a); | ||
var instance = new Constructor(); | ||
if (Class) _setPrototypeOf(instance, Class.prototype); | ||
return instance; | ||
}; | ||
} | ||
return _construct.apply(null, arguments); | ||
} | ||
function _isNativeFunction(fn) { | ||
return Function.toString.call(fn).indexOf("[native code]") !== -1; | ||
} | ||
function _wrapNativeSuper(Class) { | ||
var _cache = typeof Map === "function" ? new Map() : undefined; | ||
_wrapNativeSuper = function _wrapNativeSuper(Class) { | ||
if (Class === null || !_isNativeFunction(Class)) return Class; | ||
if (typeof Class !== "function") { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
if (typeof _cache !== "undefined") { | ||
if (_cache.has(Class)) return _cache.get(Class); | ||
_cache.set(Class, Wrapper); | ||
} | ||
function Wrapper() { | ||
return _construct(Class, arguments, _getPrototypeOf(this).constructor); | ||
} | ||
Wrapper.prototype = Object.create(Class.prototype, { | ||
constructor: { | ||
value: Wrapper, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
return _setPrototypeOf(Wrapper, Class); | ||
}; | ||
return _wrapNativeSuper(Class); | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
function _possibleConstructorReturn(self, call) { | ||
if (call && (typeof call === "object" || typeof call === "function")) { | ||
return call; | ||
} | ||
return _assertThisInitialized(self); | ||
} | ||
// @format | ||
@@ -12,13 +139,47 @@ function camelize (str) { | ||
// @format | ||
function queue() { | ||
var list = []; | ||
var completed = null; | ||
function _flush() { | ||
list.forEach(function (i) { | ||
return i(); | ||
}); | ||
list = []; | ||
} | ||
var me = { | ||
add: function add(item) { | ||
list.push(item); | ||
}, | ||
flush: function flush() { | ||
if (!completed) { | ||
completed = new Promise(function (resolve) { | ||
window.setTimeout(function () { | ||
_flush(); | ||
resolve(); | ||
}, 0); | ||
}); | ||
} | ||
return completed; | ||
}, | ||
size: function size() { | ||
return list.length; | ||
} | ||
}; | ||
return me; | ||
} | ||
// @format | ||
function initiationStrategies () { | ||
let strategies = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var strategies = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return { | ||
add(name, strategy) { | ||
add: function add(name, strategy) { | ||
strategies[name] = strategy; | ||
}, | ||
get(name) { | ||
get: function get(name) { | ||
return strategies[name]; | ||
} | ||
}; | ||
@@ -28,12 +189,12 @@ } | ||
function selectorBuilder (_ref) { | ||
let attr = _ref.attr, | ||
var attr = _ref.attr, | ||
data = _ref.data; | ||
const result = []; | ||
var result = []; | ||
if (attr) { | ||
result.push(`[${attr}]`); | ||
result.push("[".concat(attr, "]")); | ||
} | ||
if (data) { | ||
result.push(`[data-${data}]`); | ||
result.push("[data-".concat(data, "]")); | ||
} | ||
@@ -44,44 +205,54 @@ | ||
// @format | ||
var AlreadyRegisteredError = | ||
/*#__PURE__*/ | ||
function (_Error) { | ||
_inherits(AlreadyRegisteredError, _Error); | ||
class AlreadyRegisteredError extends Error { | ||
constructor(name) { | ||
super(); | ||
this.name = 'AlreadyRegisteredError'; | ||
this.message = `${name} has already been registered.`; | ||
function AlreadyRegisteredError(name) { | ||
var _this; | ||
_classCallCheck(this, AlreadyRegisteredError); | ||
_this = _possibleConstructorReturn(this, _getPrototypeOf(AlreadyRegisteredError).call(this)); | ||
_this.name = 'AlreadyRegisteredError'; | ||
_this.message = "".concat(name, " has already been registered."); | ||
return _this; | ||
} | ||
} | ||
return AlreadyRegisteredError; | ||
}(_wrapNativeSuper(Error)); | ||
const strategies = initiationStrategies({ | ||
nextTick(pluginFn, $$, options, ready) { | ||
return window.setTimeout(() => pluginFn.call($$, options, ready), 0); | ||
var widgetQueue = queue(); | ||
var strategies = initiationStrategies({ | ||
nextTick: function nextTick(pluginFn, $$, options, ready) { | ||
return window.setTimeout(function () { | ||
return pluginFn.call($$, options, ready); | ||
}, 0); | ||
}, | ||
immediate(pluginFn, $$, options, ready) { | ||
immediate: function immediate(pluginFn, $$, options, ready) { | ||
return pluginFn.call($$, options, ready) || {}; | ||
}, | ||
hover(pluginFn, $$, options, ready) { | ||
return $$.one('mouseover', () => pluginFn.call($$, options, ready)); | ||
hover: function hover(pluginFn, $$, options, ready) { | ||
return $$.one('mouseover', function () { | ||
return pluginFn.call($$, options, ready); | ||
}); | ||
} | ||
}); | ||
function emit($el, eventName) { | ||
const event = new CustomEvent(eventName); | ||
var event = new CustomEvent(eventName); | ||
$el.get(0).dispatchEvent(event); | ||
} | ||
const widget = function widget(_ref, loadEvents, fragmentLoadEvents) { | ||
let attr = _ref.attr, | ||
var widget = function widget(_ref, loadEvents, fragmentLoadEvents) { | ||
var attr = _ref.attr, | ||
data = _ref.data; | ||
const selector = selectorBuilder({ | ||
attr, | ||
data | ||
var selector = selectorBuilder({ | ||
attr: attr, | ||
data: data | ||
}); | ||
const registered = {}; | ||
var registered = {}; | ||
const register = function register(name, plugin) { | ||
let settings = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var register = function register(name, plugin) { | ||
var settings = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
@@ -97,6 +268,6 @@ if (registered[name]) { | ||
const loadWidget = function loadWidget($$, name, data) { | ||
var loadWidget = function loadWidget($$, name, data) { | ||
if (name) { | ||
const existingPlugin = $$.data(`vvidget:${name}`); | ||
const pluginFn = registered[name]; | ||
var existingPlugin = $$.data("vvidget:".concat(name)); | ||
var pluginFn = registered[name]; | ||
@@ -107,3 +278,3 @@ if (!pluginFn) { | ||
const ready = () => { | ||
var ready = function ready() { | ||
emit($$, 'vvidget:initialized'); | ||
@@ -113,6 +284,9 @@ }; | ||
if (!existingPlugin) { | ||
const pluginName = camelize(name); | ||
const options = $.extend({}, pluginFn.defaults, extractOptions(data, pluginName)); | ||
strategies.get(pluginFn.init)(pluginFn, $$, options, ready); | ||
$$.data(`vvidget:${name}`, true); | ||
var pluginName = camelize(name); | ||
var options = $.extend({}, pluginFn.defaults, extractOptions(data, pluginName)); | ||
widgetQueue.add(function () { | ||
strategies.get(pluginFn.init)(pluginFn, $$, options, ready); | ||
}); | ||
widgetQueue.flush(); | ||
$$.data("vvidget:".concat(name), true); | ||
} | ||
@@ -122,8 +296,9 @@ } | ||
const initWidgets = function initWidgets($elements) { | ||
var initWidgets = function initWidgets($elements) { | ||
$elements.each(function () { | ||
const $$ = $(this); | ||
const names = `${$$.data(data) || ''} ${$$.attr(attr) || ''}`; | ||
names.split(' ').forEach(name => loadWidget($$, name, $$.data())); | ||
$$.trigger('vvidget:ready'); | ||
var $$ = $(this); | ||
var names = "".concat($$.data(data) || '', " ").concat($$.attr(attr) || ''); | ||
names.split(' ').forEach(function (name) { | ||
return loadWidget($$, name, $$.data()); | ||
}); | ||
}); | ||
@@ -140,3 +315,7 @@ }; | ||
register.initAllWidgets = () => $(document).ready(() => initWidgets($(selector))); | ||
register.initAllWidgets = function () { | ||
return $(document).ready(function () { | ||
return initWidgets($(selector)); | ||
}); | ||
}; | ||
@@ -143,0 +322,0 @@ register.strategies = strategies; |
{ | ||
"name": "@g2crowd/widget", | ||
"description": "Rails-friendly jquery plugin wrapper", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"license": "MIT", | ||
@@ -20,5 +20,5 @@ "files": [ | ||
"devDependencies": { | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.2.2", | ||
"@babel/preset-env": "^7.3.1", | ||
"@babel/cli": "^7.5.0", | ||
"@babel/core": "^7.5.4", | ||
"@babel/preset-env": "^7.5.4", | ||
"@pika/plugin-build-web": "^0.3.10", | ||
@@ -25,0 +25,0 @@ "@pika/plugin-standard-pkg": "^0.3.10", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13644
7
451
0