MCPcopy
hub / github.com/autoNumeric/autoNumeric / init

Method init

src/AutoNumeric.js:2969–3024  ·  view source on GitHub ↗

* Use the current AutoNumeric element settings to initialize the DOM element(s) given as a parameter. * Doing so will *link* the AutoNumeric elements together since they will share the same local AutoNumeric element list. * (cf. prototype pattern : https://en.wikipedia.org/wiki/Prototype_p

(domElementOrArrayOrString, attached = true)

Source from the content-addressed store, hash-verified

2967 * @returns {AutoNumeric|[AutoNumeric]}
2968 */
2969 init(domElementOrArrayOrString, attached = true) {
2970 let returnASingleAutoNumericObject = false; // By default, this function returns an array of AutoNumeric objects
2971 let domElementsArray = [];
2972 if (AutoNumericHelper.isString(domElementOrArrayOrString)) {
2973 domElementsArray = [...document.querySelectorAll(domElementOrArrayOrString)]; // Convert a NodeList to an Array
2974 } else if (AutoNumericHelper.isElement(domElementOrArrayOrString)) {
2975 domElementsArray.push(domElementOrArrayOrString);
2976 returnASingleAutoNumericObject = true; // Special case when only one DOM element is passed as a parameter
2977 } else if (AutoNumericHelper.isArray(domElementOrArrayOrString)) {
2978 domElementsArray = domElementOrArrayOrString;
2979 } else {
2980 AutoNumericHelper.throwError(`The given parameters to the 'init' function are invalid.`);
2981 }
2982
2983 if (domElementsArray.length === 0) {
2984 AutoNumericHelper.warning(`No valid DOM elements were given hence no AutoNumeric object were instantiated.`, true);
2985
2986 return [];
2987 }
2988
2989 const currentLocalList = this._getLocalList();
2990 const autoNumericObjectsArray = [];
2991
2992 // Instantiate (and link depending on `attached`) each AutoNumeric objects
2993 domElementsArray.forEach(domElement => {
2994 // Initialize the new AutoNumeric element
2995 const originalCreateLocalListSetting = this.settings.createLocalList;
2996 if (attached) {
2997 // Temporary variable to know if we should create the local list during the initialization (since we'll remove it afterward)
2998 this.settings.createLocalList = false;
2999 }
3000
3001 const newAutoNumericElement = new AutoNumeric(domElement, AutoNumericHelper.getElementValue(domElement), this.settings);
3002
3003 // Set the common shared local list if needed
3004 // If the user wants to create a detached new AutoNumeric element, then skip the following step that bind the two elements together by default
3005 if (attached) {
3006 // 1) Set the local list reference to point to the initializer's one
3007 newAutoNumericElement._setLocalList(currentLocalList);
3008
3009 // 2) Add the new element to that existing list
3010 this._addToLocalList(domElement, newAutoNumericElement); // Here we use the *new* AutoNumeric object reference to add to the local list, since we'll need the reference to `this` in the methods to points to that new AutoNumeric object.
3011 this.settings.createLocalList = originalCreateLocalListSetting;
3012 }
3013
3014 autoNumericObjectsArray.push(newAutoNumericElement);
3015 });
3016
3017 if (returnASingleAutoNumericObject) {
3018 // If a single DOM element was used as the parameter, then we return an AutoNumeric object directly
3019 return autoNumericObjectsArray[0];
3020 }
3021
3022 // ...otherwise we return an Array of AutoNumeric objects
3023 return autoNumericObjectsArray;
3024 }
3025
3026 /**

Callers 1

Calls 9

_getLocalListMethod · 0.95
_setLocalListMethod · 0.95
_addToLocalListMethod · 0.95
isStringMethod · 0.80
isElementMethod · 0.80
isArrayMethod · 0.80
throwErrorMethod · 0.80
warningMethod · 0.80
getElementValueMethod · 0.80

Tested by

no test coverage detected