@peter-debugging-2/peter-minimal-jquery 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/main.js +287 -0
- package/dist/main.js.map +1 -0
- package/dist/main.min.js +2 -0
- package/dist/main.min.js.map +1 -0
- package/index.html +44 -0
- package/package.json +29 -0
- package/src/core.js +164 -0
- package/src/utils.js +29 -0
- package/webpack.config.js +22 -0
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
## Purpose
|
|
2
|
+
This is a *minimal jQuery* library, which used to learn jQuery and how to **bundle & distribute** as an *npm package*
|
|
3
|
+
|
|
4
|
+
## How to use
|
|
5
|
+
Just include this package link as a script tag, this should be delivered via *jsDeliver CDN*
|
|
6
|
+
|
|
7
|
+
## Steps to publish
|
|
8
|
+
- scripts npm to build & bundle with mode
|
|
9
|
+
- add .npmignore to prevent node_modules being push to npm
|
|
10
|
+
- change package name in package.json including username, also mind the version
|
|
11
|
+
- use ```npm pushblish --access public``` to publish
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/******/ (() => { // webpackBootstrap
|
|
2
|
+
/******/ "use strict";
|
|
3
|
+
/******/ var __webpack_modules__ = ({
|
|
4
|
+
|
|
5
|
+
/***/ "./src/utils.js"
|
|
6
|
+
/*!**********************!*\
|
|
7
|
+
!*** ./src/utils.js ***!
|
|
8
|
+
\**********************/
|
|
9
|
+
(__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
|
|
10
|
+
|
|
11
|
+
__webpack_require__.r(__webpack_exports__);
|
|
12
|
+
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
13
|
+
/* harmony export */ deepExtend: () => (/* binding */ deepExtend),
|
|
14
|
+
/* harmony export */ shallowExtend: () => (/* binding */ shallowExtend)
|
|
15
|
+
/* harmony export */ });
|
|
16
|
+
function deepExtend() {
|
|
17
|
+
let target = arguments[0] || {};
|
|
18
|
+
let otherArgs = Array.prototype.slice.apply(arguments, 1); //other options to override
|
|
19
|
+
for (const obj of otherArgs) {
|
|
20
|
+
if (!obj) continue;
|
|
21
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
22
|
+
switch (Object.prototype.toString.call(value)) {
|
|
23
|
+
case '[object Object]':
|
|
24
|
+
target[key] = target[key] || {};
|
|
25
|
+
target[key] = jQuery.fn.extend(target[key], value);
|
|
26
|
+
break;
|
|
27
|
+
case '[object Array]': //review case array
|
|
28
|
+
target[key] = jQuery.fn.extend(new Array(value.length), value);
|
|
29
|
+
break;
|
|
30
|
+
default:
|
|
31
|
+
target[key] = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function shallowExtend(target, ...sources) {
|
|
38
|
+
for (const src of sources) {
|
|
39
|
+
if (!src) continue;
|
|
40
|
+
for (const [key, value] of Object.entries(src)) {
|
|
41
|
+
target[key] = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/***/ }
|
|
47
|
+
|
|
48
|
+
/******/ });
|
|
49
|
+
/************************************************************************/
|
|
50
|
+
/******/ // The module cache
|
|
51
|
+
/******/ var __webpack_module_cache__ = {};
|
|
52
|
+
/******/
|
|
53
|
+
/******/ // The require function
|
|
54
|
+
/******/ function __webpack_require__(moduleId) {
|
|
55
|
+
/******/ // Check if module is in cache
|
|
56
|
+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
57
|
+
/******/ if (cachedModule !== undefined) {
|
|
58
|
+
/******/ return cachedModule.exports;
|
|
59
|
+
/******/ }
|
|
60
|
+
/******/ // Check if module exists (development only)
|
|
61
|
+
/******/ if (__webpack_modules__[moduleId] === undefined) {
|
|
62
|
+
/******/ var e = new Error("Cannot find module '" + moduleId + "'");
|
|
63
|
+
/******/ e.code = 'MODULE_NOT_FOUND';
|
|
64
|
+
/******/ throw e;
|
|
65
|
+
/******/ }
|
|
66
|
+
/******/ // Create a new module (and put it into the cache)
|
|
67
|
+
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
68
|
+
/******/ // no module.id needed
|
|
69
|
+
/******/ // no module.loaded needed
|
|
70
|
+
/******/ exports: {}
|
|
71
|
+
/******/ };
|
|
72
|
+
/******/
|
|
73
|
+
/******/ // Execute the module function
|
|
74
|
+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
75
|
+
/******/
|
|
76
|
+
/******/ // Return the exports of the module
|
|
77
|
+
/******/ return module.exports;
|
|
78
|
+
/******/ }
|
|
79
|
+
/******/
|
|
80
|
+
/************************************************************************/
|
|
81
|
+
/******/ /* webpack/runtime/define property getters */
|
|
82
|
+
/******/ (() => {
|
|
83
|
+
/******/ // define getter functions for harmony exports
|
|
84
|
+
/******/ __webpack_require__.d = (exports, definition) => {
|
|
85
|
+
/******/ for(var key in definition) {
|
|
86
|
+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
87
|
+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
88
|
+
/******/ }
|
|
89
|
+
/******/ }
|
|
90
|
+
/******/ };
|
|
91
|
+
/******/ })();
|
|
92
|
+
/******/
|
|
93
|
+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
94
|
+
/******/ (() => {
|
|
95
|
+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
96
|
+
/******/ })();
|
|
97
|
+
/******/
|
|
98
|
+
/******/ /* webpack/runtime/make namespace object */
|
|
99
|
+
/******/ (() => {
|
|
100
|
+
/******/ // define __esModule on exports
|
|
101
|
+
/******/ __webpack_require__.r = (exports) => {
|
|
102
|
+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
103
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
104
|
+
/******/ }
|
|
105
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
106
|
+
/******/ };
|
|
107
|
+
/******/ })();
|
|
108
|
+
/******/
|
|
109
|
+
/************************************************************************/
|
|
110
|
+
var __webpack_exports__ = {};
|
|
111
|
+
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
|
|
112
|
+
(() => {
|
|
113
|
+
/*!*********************!*\
|
|
114
|
+
!*** ./src/core.js ***!
|
|
115
|
+
\*********************/
|
|
116
|
+
__webpack_require__.r(__webpack_exports__);
|
|
117
|
+
/* harmony import */ var _utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils.js */ "./src/utils.js");
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
(function(global) {
|
|
122
|
+
function jQueryObject(selector) {
|
|
123
|
+
if (selector.nodeType) {
|
|
124
|
+
this.length = 1;
|
|
125
|
+
this[0] = selector;
|
|
126
|
+
}
|
|
127
|
+
else if (typeof selector == 'string') {
|
|
128
|
+
let nodeList = document.querySelectorAll(selector); //an array of nodelist => change it to jquery object
|
|
129
|
+
let index = 0;
|
|
130
|
+
this.length = 0; //Data in constructor, methods in prototype ✅
|
|
131
|
+
for (let node of nodeList) {
|
|
132
|
+
this[index] = node;
|
|
133
|
+
index += 1;
|
|
134
|
+
this.length += 1;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else
|
|
138
|
+
return;
|
|
139
|
+
//new jQueryObject implicitly return this
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let jQuery = (selector) => new jQueryObject(selector);
|
|
143
|
+
jQueryObject.prototype = jQuery.fn = jQuery.prototype = { //alias to prototype
|
|
144
|
+
ready: function(callback) {
|
|
145
|
+
let jQueryObj = this;
|
|
146
|
+
const isReady = Array.prototype.some.call(jQueryObj, function(element) {
|
|
147
|
+
return element.readyState != null && element.readyState != "loading";
|
|
148
|
+
});
|
|
149
|
+
if (isReady) {
|
|
150
|
+
callback();
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
jQueryObj.on('DOMContentLoaded', callback);
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
each: function(callback) { //suger syntax, now callback exec for each match element of jQuery instance and chainable
|
|
157
|
+
return jQuery.each(this, callback);
|
|
158
|
+
},
|
|
159
|
+
on: function(event, callback) {
|
|
160
|
+
const jQueryObj = this;
|
|
161
|
+
return jQuery.each(jQueryObj, function() {
|
|
162
|
+
const element = this;
|
|
163
|
+
element.addEventListener(event, function(e) {
|
|
164
|
+
callback.call(element, e)
|
|
165
|
+
});
|
|
166
|
+
})
|
|
167
|
+
},
|
|
168
|
+
text: function() {
|
|
169
|
+
const jQueryObj = this;
|
|
170
|
+
if (arguments.length == 1) { //setter
|
|
171
|
+
const text = arguments[0];
|
|
172
|
+
return jQuery.each(jQueryObj, function() {
|
|
173
|
+
const element = this;
|
|
174
|
+
element.textContent = text;
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
else if (!arguments || arguments?.length == 0) {
|
|
178
|
+
return jQueryObj[0].textContent;
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
val: function() {
|
|
182
|
+
const jQueryObj = this;
|
|
183
|
+
if (arguments.length == 1) {
|
|
184
|
+
const value = arguments[0];//if call arguments inside jQuery.each then it is different
|
|
185
|
+
return jQuery.each(jQueryObj, function() {
|
|
186
|
+
const element = this;
|
|
187
|
+
element.value = value;
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
else if (!arguments || arguments?.length == 0) {
|
|
191
|
+
return jQueryObj[0].value;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
css: function() {
|
|
195
|
+
let cssCamelcaseProperty = function(cssProperty) {
|
|
196
|
+
return cssProperty.replace(/-([a-z])/g, (fullMatch, captureCharacter) => {
|
|
197
|
+
return captureCharacter.toUpperCase();
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
const jQueryObj = this;
|
|
201
|
+
if (arguments.length == 2) {
|
|
202
|
+
let cssPropertyName = arguments[0];
|
|
203
|
+
let value = arguments[1];
|
|
204
|
+
return jQuery.each(jQueryObj, function() {
|
|
205
|
+
const element = this;
|
|
206
|
+
const camelProperty = cssCamelcaseProperty(cssPropertyName);
|
|
207
|
+
element.style[camelProperty] = value;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
else if (arguments.length == 1 && typeof arguments[0] == 'object') {
|
|
211
|
+
const cssProperties = arguments[0];
|
|
212
|
+
return jQuery.each(jQueryObj, function() {
|
|
213
|
+
const element = this;
|
|
214
|
+
for (const [cssPropertyName, value] of Object.entries(cssProperties)) {
|
|
215
|
+
const camelProperty = cssCamelcaseProperty(cssPropertyName);
|
|
216
|
+
element.style[camelProperty] = value;
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
else if (arguments.length == 1 && typeof arguments[0] == 'string') {
|
|
221
|
+
const cssPropertyName = arguments[0];
|
|
222
|
+
const camelProperty = cssCamelcaseProperty(cssPropertyName);
|
|
223
|
+
let result = getComputedStyle(jQueryObj[0])[camelProperty];
|
|
224
|
+
return jQueryObj[0].style[camelProperty];
|
|
225
|
+
}
|
|
226
|
+
else if (!arguments || arguments?.length == 0) {
|
|
227
|
+
let result = jQueryObj[0].style;
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
hasClass: function(className) {
|
|
232
|
+
const jQueryObj = this;
|
|
233
|
+
const result = Array.prototype.some.call(jQueryObj, (element) => element.classList.contains(className));
|
|
234
|
+
return result;
|
|
235
|
+
},
|
|
236
|
+
addClass: function(className) {
|
|
237
|
+
const jQueryObj = this;
|
|
238
|
+
return jQuery.each(jQueryObj, function() {
|
|
239
|
+
const element = this;
|
|
240
|
+
element.classList.add(className);
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
removeClass: function(className) {
|
|
244
|
+
const jQueryObj = this;
|
|
245
|
+
return jQuery.each(jQueryObj, function() {
|
|
246
|
+
const element = this;
|
|
247
|
+
element.classList.remove(className);
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
toggleClass: function(className) {
|
|
251
|
+
const jQueryObj = this;
|
|
252
|
+
return jQuery.each(jQueryObj, function() {
|
|
253
|
+
const element = this;
|
|
254
|
+
element.classList.toggle(className);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
jQuery.shallowExtend = _utils_js__WEBPACK_IMPORTED_MODULE_0__.shallowExtend;
|
|
259
|
+
jQuery.deepExtend = _utils_js__WEBPACK_IMPORTED_MODULE_0__.deepExtend;
|
|
260
|
+
jQuery.each = function(target, callback) {
|
|
261
|
+
if (target.length > 0) {
|
|
262
|
+
for (let i = 0; i < target.length; i++)
|
|
263
|
+
if (callback.call(target[i], i, target[i]) === false) break; //TODO: not sure why return false will break
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
for (let key in target)
|
|
267
|
+
if (callback.call(target[key], key, target[key]) === false) break;
|
|
268
|
+
}
|
|
269
|
+
return target; //jQuery chainable
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
//NOTE: if Query.fn = jQuery.prototype is after jQueryObject.prototype = jQuery.fn, then prototype of instance not have these methods: shallowExtend, ....
|
|
273
|
+
// jQuery.fn = jQuery.prototype = {
|
|
274
|
+
// shallowExtend: shallowExtend,
|
|
275
|
+
// deepExtend: deepExtend,
|
|
276
|
+
// }
|
|
277
|
+
|
|
278
|
+
global.jQuery = global.$ = jQuery;
|
|
279
|
+
})(window)
|
|
280
|
+
//IIFE to ensure, all var in this file not polute window global object, only jQuery & $
|
|
281
|
+
//or could use export { jQuery, jQuery as $ }, then import these 2 from the core.js (module)
|
|
282
|
+
|
|
283
|
+
})();
|
|
284
|
+
|
|
285
|
+
/******/ })()
|
|
286
|
+
;
|
|
287
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","mappings":";;;;;;;;;;;;;;;AAAO;AACP;AACA,+DAA+D;AAC/D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA,C;;;;;;UC5BA;UACA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;UAEA;UACA;;UAEA;UACA;UACA;;;;;WC5BA;WACA;WACA;WACA;WACA,yCAAyC,wCAAwC;WACjF;WACA;WACA,E;;;;;WCPA,wF;;;;;WCAA;WACA;WACA;WACA,uDAAuD,iBAAiB;WACxE;WACA,gDAAgD,aAAa;WAC7D,E;;;;;;;;;;;;ACNA;AACuD;AACvD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,gEAAgE;AAChE;AACA,6BAA6B;AAC7B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,8DAA8D;AAC9D;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT,mCAAmC;AACnC;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB,aAAa;AACb,SAAS;AACT;AACA;AACA,yCAAyC;AACzC;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA,2CAA2C;AAC3C;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,aAAa;AACb,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,aAAa;AACb,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA,2BAA2B,oDAAa;AACxC,wBAAwB,iDAAU;AAClC;AACA;AACA,4BAA4B,mBAAmB;AAC/C,6EAA6E;AAC7E;AACA;AACA;AACA;AACA;AACA,uBAAuB;AACvB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD;AACA,wBAAwB,qBAAqB","sources":["webpack://minimal-jquery/./src/utils.js","webpack://minimal-jquery/webpack/bootstrap","webpack://minimal-jquery/webpack/runtime/define property getters","webpack://minimal-jquery/webpack/runtime/hasOwnProperty shorthand","webpack://minimal-jquery/webpack/runtime/make namespace object","webpack://minimal-jquery/./src/core.js"],"sourcesContent":["export function deepExtend() {\r\n let target = arguments[0] || {};\r\n let otherArgs = Array.prototype.slice.apply(arguments, 1); //other options to override\r\n for (const obj of otherArgs) {\r\n if (!obj) continue;\r\n for (const [key, value] of Object.entries(obj)) {\r\n switch (Object.prototype.toString.call(value)) {\r\n case '[object Object]':\r\n target[key] = target[key] || {};\r\n target[key] = jQuery.fn.extend(target[key], value);\r\n break;\r\n case '[object Array]': //review case array\r\n target[key] = jQuery.fn.extend(new Array(value.length), value);\r\n break;\r\n default:\r\n target[key] = value;\r\n }\r\n }\r\n }\r\n}\r\n\r\nexport function shallowExtend(target, ...sources) {\r\n for (const src of sources) {\r\n if (!src) continue;\r\n for (const [key, value] of Object.entries(src)) {\r\n target[key] = value;\r\n }\r\n }\r\n}","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Check if module exists (development only)\n\tif (__webpack_modules__[moduleId] === undefined) {\n\t\tvar e = new Error(\"Cannot find module '\" + moduleId + \"'\");\n\t\te.code = 'MODULE_NOT_FOUND';\n\t\tthrow e;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","\r\nimport { shallowExtend, deepExtend } from \"./utils.js\";\r\n\r\n(function(global) {\r\n function jQueryObject(selector) {\r\n if (selector.nodeType) {\r\n this.length = 1;\r\n this[0] = selector;\r\n }\r\n else if (typeof selector == 'string') {\r\n let nodeList = document.querySelectorAll(selector); //an array of nodelist => change it to jquery object\r\n let index = 0;\r\n this.length = 0; //Data in constructor, methods in prototype ✅\r\n for (let node of nodeList) {\r\n this[index] = node;\r\n index += 1;\r\n this.length += 1;\r\n }\r\n }\r\n else \r\n return;\r\n //new jQueryObject implicitly return this\r\n }\r\n\r\n let jQuery = (selector) => new jQueryObject(selector);\r\n jQueryObject.prototype = jQuery.fn = jQuery.prototype = { //alias to prototype\r\n ready: function(callback) {\r\n let jQueryObj = this;\r\n const isReady = Array.prototype.some.call(jQueryObj, function(element) {\r\n return element.readyState != null && element.readyState != \"loading\";\r\n });\r\n if (isReady) {\r\n callback();\r\n }\r\n else {\r\n jQueryObj.on('DOMContentLoaded', callback);\r\n }\r\n },\r\n each: function(callback) { //suger syntax, now callback exec for each match element of jQuery instance and chainable\r\n return jQuery.each(this, callback);\r\n },\r\n on: function(event, callback) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.addEventListener(event, function(e) {\r\n callback.call(element, e)\r\n });\r\n })\r\n },\r\n text: function() {\r\n const jQueryObj = this;\r\n if (arguments.length == 1) { //setter\r\n const text = arguments[0];\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.textContent = text;\r\n })\r\n }\r\n else if (!arguments || arguments?.length == 0) {\r\n return jQueryObj[0].textContent;\r\n }\r\n },\r\n val: function() {\r\n const jQueryObj = this;\r\n if (arguments.length == 1) {\r\n const value = arguments[0];//if call arguments inside jQuery.each then it is different\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.value = value;\r\n })\r\n }\r\n else if (!arguments || arguments?.length == 0) {\r\n return jQueryObj[0].value;\r\n }\r\n },\r\n css: function() {\r\n let cssCamelcaseProperty = function(cssProperty) {\r\n return cssProperty.replace(/-([a-z])/g, (fullMatch, captureCharacter) => {\r\n return captureCharacter.toUpperCase();\r\n });\r\n };\r\n const jQueryObj = this;\r\n if (arguments.length == 2) {\r\n let cssPropertyName = arguments[0];\r\n let value = arguments[1];\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n const camelProperty = cssCamelcaseProperty(cssPropertyName);\r\n element.style[camelProperty] = value;\r\n });\r\n }\r\n else if (arguments.length == 1 && typeof arguments[0] == 'object') {\r\n const cssProperties = arguments[0];\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n for (const [cssPropertyName, value] of Object.entries(cssProperties)) {\r\n const camelProperty = cssCamelcaseProperty(cssPropertyName);\r\n element.style[camelProperty] = value;\r\n }\r\n });\r\n }\r\n else if (arguments.length == 1 && typeof arguments[0] == 'string') {\r\n const cssPropertyName = arguments[0];\r\n const camelProperty = cssCamelcaseProperty(cssPropertyName);\r\n let result = getComputedStyle(jQueryObj[0])[camelProperty];\r\n return jQueryObj[0].style[camelProperty];\r\n }\r\n else if (!arguments || arguments?.length == 0) {\r\n let result = jQueryObj[0].style;\r\n return result;\r\n }\r\n },\r\n hasClass: function(className) {\r\n const jQueryObj = this;\r\n const result = Array.prototype.some.call(jQueryObj, (element) => element.classList.contains(className));\r\n return result;\r\n },\r\n addClass: function(className) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.classList.add(className);\r\n });\r\n },\r\n removeClass: function(className) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.classList.remove(className);\r\n });\r\n },\r\n toggleClass: function(className) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.classList.toggle(className);\r\n });\r\n }\r\n }\r\n jQuery.shallowExtend = shallowExtend;\r\n jQuery.deepExtend = deepExtend;\r\n jQuery.each = function(target, callback) {\r\n if (target.length > 0) {\r\n for (let i = 0; i < target.length; i++)\r\n if (callback.call(target[i], i, target[i]) === false) break; //TODO: not sure why return false will break\r\n }\r\n else {\r\n for (let key in target)\r\n if (callback.call(target[key], key, target[key]) === false) break;\r\n }\r\n return target; //jQuery chainable\r\n }\r\n\r\n //NOTE: if Query.fn = jQuery.prototype is after jQueryObject.prototype = jQuery.fn, then prototype of instance not have these methods: shallowExtend, ....\r\n // jQuery.fn = jQuery.prototype = {\r\n // shallowExtend: shallowExtend,\r\n // deepExtend: deepExtend,\r\n // }\r\n\r\n global.jQuery = global.$ = jQuery;\r\n})(window)\r\n//IIFE to ensure, all var in this file not polute window global object, only jQuery & $\r\n//or could use export { jQuery, jQuery as $ }, then import these 2 from the core.js (module)\r\n"],"names":[],"ignoreList":[],"sourceRoot":""}
|
package/dist/main.min.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
(()=>{"use strict";function t(){let t=arguments[0]||{},e=Array.prototype.slice.apply(arguments,1);for(const n of e)if(n)for(const[e,o]of Object.entries(n))switch(Object.prototype.toString.call(o)){case"[object Object]":t[e]=t[e]||{},t[e]=jQuery.fn.extend(t[e],o);break;case"[object Array]":t[e]=jQuery.fn.extend(new Array(o.length),o);break;default:t[e]=o}}function e(t,...e){for(const n of e)if(n)for(const[e,o]of Object.entries(n))t[e]=o}!function(n){function o(t){if(t.nodeType)this.length=1,this[0]=t;else{if("string"!=typeof t)return;{let e=document.querySelectorAll(t),n=0;this.length=0;for(let t of e)this[n]=t,n+=1,this.length+=1}}}let s=t=>new o(t);o.prototype=s.fn=s.prototype={ready:function(t){Array.prototype.some.call(this,function(t){return null!=t.readyState&&"loading"!=t.readyState})?t():this.on("DOMContentLoaded",t)},each:function(t){return s.each(this,t)},on:function(t,e){return s.each(this,function(){const n=this;n.addEventListener(t,function(t){e.call(n,t)})})},text:function(){const t=this;if(1==arguments.length){const e=arguments[0];return s.each(t,function(){this.textContent=e})}if(!arguments||0==arguments?.length)return t[0].textContent},val:function(){const t=this;if(1==arguments.length){const e=arguments[0];return s.each(t,function(){this.value=e})}if(!arguments||0==arguments?.length)return t[0].value},css:function(){let t=function(t){return t.replace(/-([a-z])/g,(t,e)=>e.toUpperCase())};const e=this;if(2==arguments.length){let n=arguments[0],o=arguments[1];return s.each(e,function(){const e=t(n);this.style[e]=o})}if(1==arguments.length&&"object"==typeof arguments[0]){const n=arguments[0];return s.each(e,function(){const e=this;for(const[o,s]of Object.entries(n)){const n=t(o);e.style[n]=s}})}if(1==arguments.length&&"string"==typeof arguments[0]){const n=t(arguments[0]);return getComputedStyle(e[0])[n],e[0].style[n]}if(!arguments||0==arguments?.length)return e[0].style},hasClass:function(t){return Array.prototype.some.call(this,e=>e.classList.contains(t))},addClass:function(t){return s.each(this,function(){this.classList.add(t)})},removeClass:function(t){return s.each(this,function(){this.classList.remove(t)})},toggleClass:function(t){return s.each(this,function(){this.classList.toggle(t)})}},s.shallowExtend=e,s.deepExtend=t,s.each=function(t,e){if(t.length>0)for(let n=0;n<t.length&&!1!==e.call(t[n],n,t[n]);n++);else for(let n in t)if(!1===e.call(t[n],n,t[n]))break;return t},n.jQuery=n.$=s}(window)})();
|
|
2
|
+
//# sourceMappingURL=main.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.min.js","mappings":"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","sources":["webpack://minimal-jquery/./src/utils.js","webpack://minimal-jquery/./src/core.js"],"sourcesContent":["export function deepExtend() {\r\n let target = arguments[0] || {};\r\n let otherArgs = Array.prototype.slice.apply(arguments, 1); //other options to override\r\n for (const obj of otherArgs) {\r\n if (!obj) continue;\r\n for (const [key, value] of Object.entries(obj)) {\r\n switch (Object.prototype.toString.call(value)) {\r\n case '[object Object]':\r\n target[key] = target[key] || {};\r\n target[key] = jQuery.fn.extend(target[key], value);\r\n break;\r\n case '[object Array]': //review case array\r\n target[key] = jQuery.fn.extend(new Array(value.length), value);\r\n break;\r\n default:\r\n target[key] = value;\r\n }\r\n }\r\n }\r\n}\r\n\r\nexport function shallowExtend(target, ...sources) {\r\n for (const src of sources) {\r\n if (!src) continue;\r\n for (const [key, value] of Object.entries(src)) {\r\n target[key] = value;\r\n }\r\n }\r\n}","\r\nimport { shallowExtend, deepExtend } from \"./utils.js\";\r\n\r\n(function(global) {\r\n function jQueryObject(selector) {\r\n if (selector.nodeType) {\r\n this.length = 1;\r\n this[0] = selector;\r\n }\r\n else if (typeof selector == 'string') {\r\n let nodeList = document.querySelectorAll(selector); //an array of nodelist => change it to jquery object\r\n let index = 0;\r\n this.length = 0; //Data in constructor, methods in prototype ✅\r\n for (let node of nodeList) {\r\n this[index] = node;\r\n index += 1;\r\n this.length += 1;\r\n }\r\n }\r\n else \r\n return;\r\n //new jQueryObject implicitly return this\r\n }\r\n\r\n let jQuery = (selector) => new jQueryObject(selector);\r\n jQueryObject.prototype = jQuery.fn = jQuery.prototype = { //alias to prototype\r\n ready: function(callback) {\r\n let jQueryObj = this;\r\n const isReady = Array.prototype.some.call(jQueryObj, function(element) {\r\n return element.readyState != null && element.readyState != \"loading\";\r\n });\r\n if (isReady) {\r\n callback();\r\n }\r\n else {\r\n jQueryObj.on('DOMContentLoaded', callback);\r\n }\r\n },\r\n each: function(callback) { //suger syntax, now callback exec for each match element of jQuery instance and chainable\r\n return jQuery.each(this, callback);\r\n },\r\n on: function(event, callback) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.addEventListener(event, function(e) {\r\n callback.call(element, e)\r\n });\r\n })\r\n },\r\n text: function() {\r\n const jQueryObj = this;\r\n if (arguments.length == 1) { //setter\r\n const text = arguments[0];\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.textContent = text;\r\n })\r\n }\r\n else if (!arguments || arguments?.length == 0) {\r\n return jQueryObj[0].textContent;\r\n }\r\n },\r\n val: function() {\r\n const jQueryObj = this;\r\n if (arguments.length == 1) {\r\n const value = arguments[0];//if call arguments inside jQuery.each then it is different\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.value = value;\r\n })\r\n }\r\n else if (!arguments || arguments?.length == 0) {\r\n return jQueryObj[0].value;\r\n }\r\n },\r\n css: function() {\r\n let cssCamelcaseProperty = function(cssProperty) {\r\n return cssProperty.replace(/-([a-z])/g, (fullMatch, captureCharacter) => {\r\n return captureCharacter.toUpperCase();\r\n });\r\n };\r\n const jQueryObj = this;\r\n if (arguments.length == 2) {\r\n let cssPropertyName = arguments[0];\r\n let value = arguments[1];\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n const camelProperty = cssCamelcaseProperty(cssPropertyName);\r\n element.style[camelProperty] = value;\r\n });\r\n }\r\n else if (arguments.length == 1 && typeof arguments[0] == 'object') {\r\n const cssProperties = arguments[0];\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n for (const [cssPropertyName, value] of Object.entries(cssProperties)) {\r\n const camelProperty = cssCamelcaseProperty(cssPropertyName);\r\n element.style[camelProperty] = value;\r\n }\r\n });\r\n }\r\n else if (arguments.length == 1 && typeof arguments[0] == 'string') {\r\n const cssPropertyName = arguments[0];\r\n const camelProperty = cssCamelcaseProperty(cssPropertyName);\r\n let result = getComputedStyle(jQueryObj[0])[camelProperty];\r\n return jQueryObj[0].style[camelProperty];\r\n }\r\n else if (!arguments || arguments?.length == 0) {\r\n let result = jQueryObj[0].style;\r\n return result;\r\n }\r\n },\r\n hasClass: function(className) {\r\n const jQueryObj = this;\r\n const result = Array.prototype.some.call(jQueryObj, (element) => element.classList.contains(className));\r\n return result;\r\n },\r\n addClass: function(className) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.classList.add(className);\r\n });\r\n },\r\n removeClass: function(className) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.classList.remove(className);\r\n });\r\n },\r\n toggleClass: function(className) {\r\n const jQueryObj = this;\r\n return jQuery.each(jQueryObj, function() {\r\n const element = this;\r\n element.classList.toggle(className);\r\n });\r\n }\r\n }\r\n jQuery.shallowExtend = shallowExtend;\r\n jQuery.deepExtend = deepExtend;\r\n jQuery.each = function(target, callback) {\r\n if (target.length > 0) {\r\n for (let i = 0; i < target.length; i++)\r\n if (callback.call(target[i], i, target[i]) === false) break; //TODO: not sure why return false will break\r\n }\r\n else {\r\n for (let key in target)\r\n if (callback.call(target[key], key, target[key]) === false) break;\r\n }\r\n return target; //jQuery chainable\r\n }\r\n\r\n //NOTE: if Query.fn = jQuery.prototype is after jQueryObject.prototype = jQuery.fn, then prototype of instance not have these methods: shallowExtend, ....\r\n // jQuery.fn = jQuery.prototype = {\r\n // shallowExtend: shallowExtend,\r\n // deepExtend: deepExtend,\r\n // }\r\n\r\n global.jQuery = global.$ = jQuery;\r\n})(window)\r\n//IIFE to ensure, all var in this file not polute window global object, only jQuery & $\r\n//or could use export { jQuery, jQuery as $ }, then import these 2 from the core.js (module)\r\n"],"names":["deepExtend","target","arguments","otherArgs","Array","prototype","slice","apply","obj","key","value","Object","entries","toString","call","jQuery","fn","extend","length","shallowExtend","sources","src","global","jQueryObject","selector","nodeType","this","nodeList","document","querySelectorAll","index","node","ready","callback","some","element","readyState","on","each","event","addEventListener","e","text","jQueryObj","textContent","val","css","cssCamelcaseProperty","cssProperty","replace","fullMatch","captureCharacter","toUpperCase","cssPropertyName","camelProperty","style","cssProperties","getComputedStyle","hasClass","className","classList","contains","addClass","add","removeClass","remove","toggleClass","toggle","i","$","window"],"ignoreList":[],"sourceRoot":""}
|
package/index.html
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Document</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<input id="toSelect">
|
|
10
|
+
<input id="toSelect2">
|
|
11
|
+
<div class="myDiv"></div>
|
|
12
|
+
<!-- <script src="./src/core.js" type="module"></script> -->
|
|
13
|
+
<script src="./dist/main.js"></script>
|
|
14
|
+
<!-- <script type="module"> -->
|
|
15
|
+
<script>
|
|
16
|
+
jQuery(document).ready(function() {
|
|
17
|
+
console.log("document ready!!");
|
|
18
|
+
});
|
|
19
|
+
let x = jQuery("#toSelect");
|
|
20
|
+
console.log(x);
|
|
21
|
+
let y = {};
|
|
22
|
+
let z = { isSomething: true };
|
|
23
|
+
jQuery.shallowExtend(y, z);
|
|
24
|
+
console.log(y);
|
|
25
|
+
jQuery("input").each(function(index, item) {
|
|
26
|
+
console.log(index);
|
|
27
|
+
console.log(item);
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
jQuery("input")
|
|
31
|
+
.on("click", (event) => {
|
|
32
|
+
console.log(event);
|
|
33
|
+
})
|
|
34
|
+
.val("Just a value")
|
|
35
|
+
.css({"color": "yellow", "display": "block"})
|
|
36
|
+
.toggleClass("myInput");
|
|
37
|
+
jQuery(".myDiv").text("Hello world");
|
|
38
|
+
console.log(jQuery("input").css("color"));
|
|
39
|
+
console.log(jQuery("input").css());
|
|
40
|
+
console.log(jQuery("input").hasClass("myInput"));
|
|
41
|
+
|
|
42
|
+
</script>
|
|
43
|
+
</body>
|
|
44
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peter-debugging-2/peter-minimal-jquery",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A usable simplified jquery like package",
|
|
5
|
+
"main": "./src/core.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
+
"build:dev": "webpack --config webpack.config.js --mode development",
|
|
10
|
+
"build:prod": "webpack --config webpack.config.js --mode production"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/ToiLaThin/minimal-jquery.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"jquery"
|
|
18
|
+
],
|
|
19
|
+
"author": "ToiLaThin",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/ToiLaThin/minimal-jquery/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/ToiLaThin/minimal-jquery#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"webpack": "^5.104.1",
|
|
27
|
+
"webpack-cli": "^6.0.1"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/core.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
|
|
2
|
+
import { shallowExtend, deepExtend } from "./utils.js";
|
|
3
|
+
|
|
4
|
+
(function(global) {
|
|
5
|
+
function jQueryObject(selector) {
|
|
6
|
+
if (selector.nodeType) {
|
|
7
|
+
this.length = 1;
|
|
8
|
+
this[0] = selector;
|
|
9
|
+
}
|
|
10
|
+
else if (typeof selector == 'string') {
|
|
11
|
+
let nodeList = document.querySelectorAll(selector); //an array of nodelist => change it to jquery object
|
|
12
|
+
let index = 0;
|
|
13
|
+
this.length = 0; //Data in constructor, methods in prototype ✅
|
|
14
|
+
for (let node of nodeList) {
|
|
15
|
+
this[index] = node;
|
|
16
|
+
index += 1;
|
|
17
|
+
this.length += 1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else
|
|
21
|
+
return;
|
|
22
|
+
//new jQueryObject implicitly return this
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let jQuery = (selector) => new jQueryObject(selector);
|
|
26
|
+
jQueryObject.prototype = jQuery.fn = jQuery.prototype = { //alias to prototype
|
|
27
|
+
ready: function(callback) {
|
|
28
|
+
let jQueryObj = this;
|
|
29
|
+
const isReady = Array.prototype.some.call(jQueryObj, function(element) {
|
|
30
|
+
return element.readyState != null && element.readyState != "loading";
|
|
31
|
+
});
|
|
32
|
+
if (isReady) {
|
|
33
|
+
callback();
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
jQueryObj.on('DOMContentLoaded', callback);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
each: function(callback) { //suger syntax, now callback exec for each match element of jQuery instance and chainable
|
|
40
|
+
return jQuery.each(this, callback);
|
|
41
|
+
},
|
|
42
|
+
on: function(event, callback) {
|
|
43
|
+
const jQueryObj = this;
|
|
44
|
+
return jQuery.each(jQueryObj, function() {
|
|
45
|
+
const element = this;
|
|
46
|
+
element.addEventListener(event, function(e) {
|
|
47
|
+
callback.call(element, e)
|
|
48
|
+
});
|
|
49
|
+
})
|
|
50
|
+
},
|
|
51
|
+
text: function() {
|
|
52
|
+
const jQueryObj = this;
|
|
53
|
+
if (arguments.length == 1) { //setter
|
|
54
|
+
const text = arguments[0];
|
|
55
|
+
return jQuery.each(jQueryObj, function() {
|
|
56
|
+
const element = this;
|
|
57
|
+
element.textContent = text;
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
else if (!arguments || arguments?.length == 0) {
|
|
61
|
+
return jQueryObj[0].textContent;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
val: function() {
|
|
65
|
+
const jQueryObj = this;
|
|
66
|
+
if (arguments.length == 1) {
|
|
67
|
+
const value = arguments[0];//if call arguments inside jQuery.each then it is different
|
|
68
|
+
return jQuery.each(jQueryObj, function() {
|
|
69
|
+
const element = this;
|
|
70
|
+
element.value = value;
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
else if (!arguments || arguments?.length == 0) {
|
|
74
|
+
return jQueryObj[0].value;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
css: function() {
|
|
78
|
+
let cssCamelcaseProperty = function(cssProperty) {
|
|
79
|
+
return cssProperty.replace(/-([a-z])/g, (fullMatch, captureCharacter) => {
|
|
80
|
+
return captureCharacter.toUpperCase();
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
const jQueryObj = this;
|
|
84
|
+
if (arguments.length == 2) {
|
|
85
|
+
let cssPropertyName = arguments[0];
|
|
86
|
+
let value = arguments[1];
|
|
87
|
+
return jQuery.each(jQueryObj, function() {
|
|
88
|
+
const element = this;
|
|
89
|
+
const camelProperty = cssCamelcaseProperty(cssPropertyName);
|
|
90
|
+
element.style[camelProperty] = value;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else if (arguments.length == 1 && typeof arguments[0] == 'object') {
|
|
94
|
+
const cssProperties = arguments[0];
|
|
95
|
+
return jQuery.each(jQueryObj, function() {
|
|
96
|
+
const element = this;
|
|
97
|
+
for (const [cssPropertyName, value] of Object.entries(cssProperties)) {
|
|
98
|
+
const camelProperty = cssCamelcaseProperty(cssPropertyName);
|
|
99
|
+
element.style[camelProperty] = value;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else if (arguments.length == 1 && typeof arguments[0] == 'string') {
|
|
104
|
+
const cssPropertyName = arguments[0];
|
|
105
|
+
const camelProperty = cssCamelcaseProperty(cssPropertyName);
|
|
106
|
+
let result = getComputedStyle(jQueryObj[0])[camelProperty];
|
|
107
|
+
return jQueryObj[0].style[camelProperty];
|
|
108
|
+
}
|
|
109
|
+
else if (!arguments || arguments?.length == 0) {
|
|
110
|
+
let result = jQueryObj[0].style;
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
hasClass: function(className) {
|
|
115
|
+
const jQueryObj = this;
|
|
116
|
+
const result = Array.prototype.some.call(jQueryObj, (element) => element.classList.contains(className));
|
|
117
|
+
return result;
|
|
118
|
+
},
|
|
119
|
+
addClass: function(className) {
|
|
120
|
+
const jQueryObj = this;
|
|
121
|
+
return jQuery.each(jQueryObj, function() {
|
|
122
|
+
const element = this;
|
|
123
|
+
element.classList.add(className);
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
removeClass: function(className) {
|
|
127
|
+
const jQueryObj = this;
|
|
128
|
+
return jQuery.each(jQueryObj, function() {
|
|
129
|
+
const element = this;
|
|
130
|
+
element.classList.remove(className);
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
toggleClass: function(className) {
|
|
134
|
+
const jQueryObj = this;
|
|
135
|
+
return jQuery.each(jQueryObj, function() {
|
|
136
|
+
const element = this;
|
|
137
|
+
element.classList.toggle(className);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
jQuery.shallowExtend = shallowExtend;
|
|
142
|
+
jQuery.deepExtend = deepExtend;
|
|
143
|
+
jQuery.each = function(target, callback) {
|
|
144
|
+
if (target.length > 0) {
|
|
145
|
+
for (let i = 0; i < target.length; i++)
|
|
146
|
+
if (callback.call(target[i], i, target[i]) === false) break; //TODO: not sure why return false will break
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
for (let key in target)
|
|
150
|
+
if (callback.call(target[key], key, target[key]) === false) break;
|
|
151
|
+
}
|
|
152
|
+
return target; //jQuery chainable
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
//NOTE: if Query.fn = jQuery.prototype is after jQueryObject.prototype = jQuery.fn, then prototype of instance not have these methods: shallowExtend, ....
|
|
156
|
+
// jQuery.fn = jQuery.prototype = {
|
|
157
|
+
// shallowExtend: shallowExtend,
|
|
158
|
+
// deepExtend: deepExtend,
|
|
159
|
+
// }
|
|
160
|
+
|
|
161
|
+
global.jQuery = global.$ = jQuery;
|
|
162
|
+
})(window)
|
|
163
|
+
//IIFE to ensure, all var in this file not polute window global object, only jQuery & $
|
|
164
|
+
//or could use export { jQuery, jQuery as $ }, then import these 2 from the core.js (module)
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function deepExtend() {
|
|
2
|
+
let target = arguments[0] || {};
|
|
3
|
+
let otherArgs = Array.prototype.slice.apply(arguments, 1); //other options to override
|
|
4
|
+
for (const obj of otherArgs) {
|
|
5
|
+
if (!obj) continue;
|
|
6
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
7
|
+
switch (Object.prototype.toString.call(value)) {
|
|
8
|
+
case '[object Object]':
|
|
9
|
+
target[key] = target[key] || {};
|
|
10
|
+
target[key] = jQuery.fn.extend(target[key], value);
|
|
11
|
+
break;
|
|
12
|
+
case '[object Array]': //review case array
|
|
13
|
+
target[key] = jQuery.fn.extend(new Array(value.length), value);
|
|
14
|
+
break;
|
|
15
|
+
default:
|
|
16
|
+
target[key] = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function shallowExtend(target, ...sources) {
|
|
23
|
+
for (const src of sources) {
|
|
24
|
+
if (!src) continue;
|
|
25
|
+
for (const [key, value] of Object.entries(src)) {
|
|
26
|
+
target[key] = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
|
|
4
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
+
const __dirname = path.dirname(__filename);
|
|
6
|
+
|
|
7
|
+
export default (env, argv) => {
|
|
8
|
+
const isProduction = argv.mode == 'production';
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
entry: './src/core.js',
|
|
12
|
+
mode: isProduction ? "production" : "development",
|
|
13
|
+
devtool: "source-map",
|
|
14
|
+
optimization: {
|
|
15
|
+
minimize: isProduction
|
|
16
|
+
},
|
|
17
|
+
output: {
|
|
18
|
+
filename: isProduction ? 'main.min.js' : 'main.js',
|
|
19
|
+
path: path.resolve(__dirname, 'dist')
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|