Skip to content

Commit

Permalink
remove dependency from window (#10)
Browse files Browse the repository at this point in the history
* remove dependency from window and generalize to exports

* bump version
  • Loading branch information
dealfonso authored Dec 4, 2023
1 parent af4ce50 commit 4e7277e
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 96 deletions.
26 changes: 13 additions & 13 deletions devel.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@6.11.0/css/flag-icons.min.css"/>
<script src="js/init.js"></script>
<script src="js/utils.js"></script>
<script src="js/dialogutils.js"></script>
<script src="js/dialoglegacy.js"></script>
<script src="js/dialog.js"></script>
<script src="js/powerbuttons.js"></script>
<script src="js/actions.js"></script>
<script src="js/plugins/00.actionverify.js"></script>
<script src="js/plugins/10.actionconfirm.js"></script>
<script src="js/plugins/20.actionasynctask.js"></script>
<script src="js/plugins/30.actionshowmessage.js"></script>
<script src="js/plugins/40.actionformset.js"></script>
<script src="js/plugins/80.actionformbutton.js"></script>
<script src="src/init.js"></script>
<script src="src/utils.js"></script>
<script src="src/dialogutils.js"></script>
<script src="src/dialoglegacy.js"></script>
<script src="src/dialog.js"></script>
<script src="src/powerbuttons.js"></script>
<script src="src/actions.js"></script>
<script src="src/plugins/00.actionverify.js"></script>
<script src="src/plugins/10.actionconfirm.js"></script>
<script src="src/plugins/20.actionasynctask.js"></script>
<script src="src/plugins/30.actionshowmessage.js"></script>
<script src="src/plugins/40.actionformset.js"></script>
<script src="src/plugins/80.actionformbutton.js"></script>
<!-- <script src="dist/powerbuttons.js"></script> -->
<style>
html, body {
Expand Down
2 changes: 1 addition & 1 deletion dist/powerbuttons.compress.js

Large diffs are not rendered by default.

43 changes: 23 additions & 20 deletions dist/powerbuttons.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

(function (exports) {
"use strict";
window.powerButtons = function (pluginName, els = [], options = {}) {
if (typeof exports === "undefined") {
var exports = window
}
exports.powerButtons = function (pluginName, els = [], options = {}) {
let elements = els;
if (typeof elements === "string") {
elements = document.querySelectorAll(elements)
Expand All @@ -41,17 +44,17 @@
}
return els
};
window.powerButtons.version = "2.0.1";
window.powerButtons.plugins = function () {
exports.powerButtons.version = "2.0.1";
exports.powerButtons.plugins = function () {
return Object.keys(PowerButtons.actionsRegistered)
};
if (window.$ !== undefined) {
window.$.fn.powerButtons = function (pluginName, options = {}) {
window.powerButtons(pluginName, this, options);
if (exports.$ !== undefined) {
exports.$.fn.powerButtons = function (pluginName, options = {}) {
exports.powerButtons(pluginName, this, options);
return this
};
window.$.fn.powerButtons.version = window.powerButtons.version;
window.$.fn.powerButtons.plugins = window.powerButtons.plugins
exports.$.fn.powerButtons.version = exports.powerButtons.version;
exports.$.fn.powerButtons.plugins = exports.powerButtons.plugins
}

function pascalToSnake(str) {
Expand Down Expand Up @@ -257,7 +260,7 @@
}
class Dialog {
static create(options = {}, onButton = null, onHidden = null) {
if (window.bootstrap === undefined || window.bootstrap.Modal === undefined) {
if (exports.bootstrap === undefined || exports.bootstrap.Modal === undefined) {
return new DialogLegacy(options, onButton, onHidden)
}
if (options.selector !== undefined && options.selector !== null || options.dialogFunction !== undefined && options.dialogFunction !== null) {
Expand Down Expand Up @@ -286,7 +289,7 @@
onHidden = null;
onButton = null;
constructor(options = {}, onButton = null, onHidden = null) {
if (window.bootstrap === undefined || window.bootstrap.Modal === undefined) {
if (exports.bootstrap === undefined || exports.bootstrap.Modal === undefined) {
throw new Error("Bootstrap is required to use this class")
}
this.options = {
Expand Down Expand Up @@ -509,10 +512,10 @@
dialog.show();
return dialog
}
if (window.powerButtons.utils === undefined) {
window.powerButtons.utils = {}
if (exports.powerButtons.utils === undefined) {
exports.powerButtons.utils = {}
}
Object.assign(window.powerButtons.utils, {
Object.assign(exports.powerButtons.utils, {
confirmDialog: confirmDialog,
alertDialog: alertDialog,
loadingDialog: loadingDialog
Expand All @@ -521,22 +524,22 @@
static actionsRegistered = {};
static registerAction(action) {
this.actionsRegistered[action.NAME.toLowerCase()] = action;
if (window.powerButtons === undefined) {
window.powerButtons = {}
if (exports.powerButtons === undefined) {
exports.powerButtons = {}
}
if (window.powerButtons.defaults === undefined) {
window.powerButtons.defaults = {}
if (exports.powerButtons.defaults === undefined) {
exports.powerButtons.defaults = {}
}
window.powerButtons.defaults[action.NAME.toLowerCase()] = Object.assign({}, action.DEFAULTS)
exports.powerButtons.defaults[action.NAME.toLowerCase()] = Object.assign({}, action.DEFAULTS)
}
static getActionSettings(action, options) {
if (this.actionsRegistered[action.NAME.toLowerCase()] === undefined) {
console.error(`The action ${action.NAME} is not registered`);
return {}
}
let defaultsWindow = {};
if (window.powerButtons !== undefined && window.powerButtons.defaults !== undefined && window.powerButtons.defaults[action.NAME.toLowerCase()] !== undefined) {
defaultsWindow = window.powerButtons.defaults[action.NAME.toLowerCase()]
if (exports.powerButtons !== undefined && exports.powerButtons.defaults !== undefined && exports.powerButtons.defaults[action.NAME.toLowerCase()] !== undefined) {
defaultsWindow = exports.powerButtons.defaults[action.NAME.toLowerCase()]
}
return Object.assign({}, action.DEFAULTS, defaultsWindow, options)
}
Expand Down
43 changes: 23 additions & 20 deletions dist/powerbuttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

(function (exports) {
"use strict";
window.powerButtons = function (pluginName, els = [], options = {}) {
if (typeof exports === "undefined") {
var exports = window;
}
exports.powerButtons = function (pluginName, els = [], options = {}) {
let elements = els;
if (typeof elements === "string") {
elements = document.querySelectorAll(elements);
Expand All @@ -41,17 +44,17 @@
}
return els;
};
window.powerButtons.version = "2.0.1";
window.powerButtons.plugins = function () {
exports.powerButtons.version = "2.0.1";
exports.powerButtons.plugins = function () {
return Object.keys(PowerButtons.actionsRegistered);
};
if (window.$ !== undefined) {
window.$.fn.powerButtons = function (pluginName, options = {}) {
window.powerButtons(pluginName, this, options);
if (exports.$ !== undefined) {
exports.$.fn.powerButtons = function (pluginName, options = {}) {
exports.powerButtons(pluginName, this, options);
return this;
};
window.$.fn.powerButtons.version = window.powerButtons.version;
window.$.fn.powerButtons.plugins = window.powerButtons.plugins;
exports.$.fn.powerButtons.version = exports.powerButtons.version;
exports.$.fn.powerButtons.plugins = exports.powerButtons.plugins;
}

function pascalToSnake(str) {
Expand Down Expand Up @@ -257,7 +260,7 @@
}
class Dialog {
static create(options = {}, onButton = null, onHidden = null) {
if (window.bootstrap === undefined || window.bootstrap.Modal === undefined) {
if (exports.bootstrap === undefined || exports.bootstrap.Modal === undefined) {
return new DialogLegacy(options, onButton, onHidden);
}
if (options.selector !== undefined && options.selector !== null || options.dialogFunction !== undefined && options.dialogFunction !== null) {
Expand Down Expand Up @@ -286,7 +289,7 @@
onHidden = null;
onButton = null;
constructor(options = {}, onButton = null, onHidden = null) {
if (window.bootstrap === undefined || window.bootstrap.Modal === undefined) {
if (exports.bootstrap === undefined || exports.bootstrap.Modal === undefined) {
throw new Error("Bootstrap is required to use this class");
}
this.options = {
Expand Down Expand Up @@ -509,10 +512,10 @@
dialog.show();
return dialog;
}
if (window.powerButtons.utils === undefined) {
window.powerButtons.utils = {};
if (exports.powerButtons.utils === undefined) {
exports.powerButtons.utils = {};
}
Object.assign(window.powerButtons.utils, {
Object.assign(exports.powerButtons.utils, {
confirmDialog: confirmDialog,
alertDialog: alertDialog,
loadingDialog: loadingDialog
Expand All @@ -521,22 +524,22 @@
static actionsRegistered = {};
static registerAction(action) {
this.actionsRegistered[action.NAME.toLowerCase()] = action;
if (window.powerButtons === undefined) {
window.powerButtons = {};
if (exports.powerButtons === undefined) {
exports.powerButtons = {};
}
if (window.powerButtons.defaults === undefined) {
window.powerButtons.defaults = {};
if (exports.powerButtons.defaults === undefined) {
exports.powerButtons.defaults = {};
}
window.powerButtons.defaults[action.NAME.toLowerCase()] = Object.assign({}, action.DEFAULTS);
exports.powerButtons.defaults[action.NAME.toLowerCase()] = Object.assign({}, action.DEFAULTS);
}
static getActionSettings(action, options) {
if (this.actionsRegistered[action.NAME.toLowerCase()] === undefined) {
console.error(`The action ${action.NAME} is not registered`);
return {};
}
let defaultsWindow = {};
if (window.powerButtons !== undefined && window.powerButtons.defaults !== undefined && window.powerButtons.defaults[action.NAME.toLowerCase()] !== undefined) {
defaultsWindow = window.powerButtons.defaults[action.NAME.toLowerCase()];
if (exports.powerButtons !== undefined && exports.powerButtons.defaults !== undefined && exports.powerButtons.defaults[action.NAME.toLowerCase()] !== undefined) {
defaultsWindow = exports.powerButtons.defaults[action.NAME.toLowerCase()];
}
return Object.assign({}, action.DEFAULTS, defaultsWindow, options);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/powerbuttons.min.js

Large diffs are not rendered by default.

44 changes: 24 additions & 20 deletions dist/powerbuttons.raw.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';

if (typeof exports === 'undefined') {
var exports = window;
}

// We'll create a namespace for the plugin, that will contain the plugin itself
window.powerButtons = function(pluginName, els = [], options = {}) {
exports.powerButtons = function(pluginName, els = [], options = {}) {
let elements = els;

if (typeof(elements) === "string") {
Expand All @@ -28,19 +32,19 @@ window.powerButtons = function(pluginName, els = [], options = {}) {
return els;
};

window.powerButtons.version = '2.0.1';
window.powerButtons.plugins = function() {
exports.powerButtons.version = '2.0.1';
exports.powerButtons.plugins = function() {
return Object.keys(PowerButtons.actionsRegistered);
}

// Now we add the plugin to jQuery, if it has been loaded
if (window.$ !== undefined) {
window.$.fn.powerButtons = function(pluginName, options = {}) {
window.powerButtons(pluginName, this, options);
if (exports.$ !== undefined) {
exports.$.fn.powerButtons = function(pluginName, options = {}) {
exports.powerButtons(pluginName, this, options);
return this;
}
window.$.fn.powerButtons.version = window.powerButtons.version;
window.$.fn.powerButtons.plugins = window.powerButtons.plugins;
exports.$.fn.powerButtons.version = exports.powerButtons.version;
exports.$.fn.powerButtons.plugins = exports.powerButtons.plugins;
}function pascalToSnake(str) { return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`).replace(/^_*/,'') };
function pascalToKebab(str) { return str.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`).replace(/^-*/,''); }
function snakeCaseToCamel(str) { return str.replace(/-([a-z])/g, g => g[1].toUpperCase()); }
Expand Down Expand Up @@ -289,7 +293,7 @@ function isEmpty(obj) {
}class Dialog {
static create(options = {}, onButton = null, onHidden = null) {
// If bootstrap is not supported, we'll use the legacy system dialog
if ((window.bootstrap === undefined) || (window.bootstrap.Modal === undefined)) {
if ((exports.bootstrap === undefined) || (exports.bootstrap.Modal === undefined)) {
return new DialogLegacy(options, onButton, onHidden);
}
// If the user provided a selector or a dialog function, we'll use the custom dialog
Expand Down Expand Up @@ -348,7 +352,7 @@ function isEmpty(obj) {
onButton = null;

constructor (options = {}, onButton = null, onHidden = null) {
if ((window.bootstrap === undefined) || (window.bootstrap.Modal === undefined)) {
if ((exports.bootstrap === undefined) || (exports.bootstrap.Modal === undefined)) {
throw new Error("Bootstrap is required to use this class");
}

Expand Down Expand Up @@ -622,11 +626,11 @@ function loadingDialog(message, customContent = null, canCancel = null) {
return dialog;
}

if (window.powerButtons.utils === undefined) {
window.powerButtons.utils = {};
if (exports.powerButtons.utils === undefined) {
exports.powerButtons.utils = {};
}

Object.assign(window.powerButtons.utils, {
Object.assign(exports.powerButtons.utils, {
confirmDialog: confirmDialog,
alertDialog: alertDialog,
loadingDialog: loadingDialog
Expand All @@ -641,13 +645,13 @@ Object.assign(window.powerButtons.utils, {
static registerAction(action) {
this.actionsRegistered[action.NAME.toLowerCase()] = action;

if (window.powerButtons === undefined) {
window.powerButtons = {};
if (exports.powerButtons === undefined) {
exports.powerButtons = {};
}
if (window.powerButtons.defaults === undefined) {
window.powerButtons.defaults = {};
if (exports.powerButtons.defaults === undefined) {
exports.powerButtons.defaults = {};
}
window.powerButtons.defaults[action.NAME.toLowerCase()] = Object.assign({}, action.DEFAULTS);
exports.powerButtons.defaults[action.NAME.toLowerCase()] = Object.assign({}, action.DEFAULTS);
}

/**
Expand All @@ -665,8 +669,8 @@ Object.assign(window.powerButtons.utils, {

let defaultsWindow = {};

if ((window.powerButtons !== undefined) && (window.powerButtons.defaults !== undefined) && (window.powerButtons.defaults[action.NAME.toLowerCase()] !== undefined)) {
defaultsWindow = window.powerButtons.defaults[action.NAME.toLowerCase()];
if ((exports.powerButtons !== undefined) && (exports.powerButtons.defaults !== undefined) && (exports.powerButtons.defaults[action.NAME.toLowerCase()] !== undefined)) {
defaultsWindow = exports.powerButtons.defaults[action.NAME.toLowerCase()];
}

// Merge the defaults of the action with the defaults of the window and the options
Expand Down
4 changes: 2 additions & 2 deletions src/dialog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Dialog {
static create(options = {}, onButton = null, onHidden = null) {
// If bootstrap is not supported, we'll use the legacy system dialog
if ((window.bootstrap === undefined) || (window.bootstrap.Modal === undefined)) {
if ((exports.bootstrap === undefined) || (exports.bootstrap.Modal === undefined)) {
return new DialogLegacy(options, onButton, onHidden);
}
// If the user provided a selector or a dialog function, we'll use the custom dialog
Expand Down Expand Up @@ -60,7 +60,7 @@ class Dialog {
onButton = null;

constructor (options = {}, onButton = null, onHidden = null) {
if ((window.bootstrap === undefined) || (window.bootstrap.Modal === undefined)) {
if ((exports.bootstrap === undefined) || (exports.bootstrap.Modal === undefined)) {
throw new Error("Bootstrap is required to use this class");
}

Expand Down
2 changes: 1 addition & 1 deletion src/dialogcustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DialogCustom extends Dialog {
_buttonHandlers = [];

constructor (options = {}, onButton = null, onHidden = null) {
if ((window.bootstrap === undefined) || (window.bootstrap.Modal === undefined)) {
if ((exports.bootstrap === undefined) || (exports.bootstrap.Modal === undefined)) {
throw new Error("Bootstrap is required to use this class");
}

Expand Down
6 changes: 3 additions & 3 deletions src/dialogutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ function loadingDialog(message, customContent = null, canCancel = null) {
return dialog;
}

if (window.powerButtons.utils === undefined) {
window.powerButtons.utils = {};
if (exports.powerButtons.utils === undefined) {
exports.powerButtons.utils = {};
}

Object.assign(window.powerButtons.utils, {
Object.assign(exports.powerButtons.utils, {
confirmDialog: confirmDialog,
alertDialog: alertDialog,
loadingDialog: loadingDialog
Expand Down
Loading

0 comments on commit 4e7277e

Please sign in to comment.