- Refactoring: Although openclipart is not currently working, we still add routine for copying query-result/qr-manipulation to be able to use them in other script

master
Brett Zamir 2018-09-30 13:33:00 +08:00
parent c1a5a96c0e
commit e735142a40
5 changed files with 668 additions and 47 deletions

View File

@ -51,6 +51,12 @@
};
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
@ -69,6 +75,121 @@
return _extends.apply(this, arguments);
}
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);
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
@ -2038,87 +2159,215 @@
var body = doc && doc.body;
var nbsp = '\xA0'; // Very commonly needed in templates
const LOAD = 'load';
const DOM_CONTENT_LOADED = 'DOMContentLoaded';
var LOAD = 'load';
var DOM_CONTENT_LOADED = 'DOMContentLoaded';
class QueryResult extends Array {}
var QueryResult =
/*#__PURE__*/
function (_Array) {
_inherits(QueryResult, _Array);
const search = (list, el) => {
const nodes = [];
function QueryResult() {
_classCallCheck(this, QueryResult);
for (const CSS of list) {
const css = CSS.trim();
if (css.slice(-6) === ':first') {
const node = el.querySelector(css.slice(0, -6));
if (node) nodes.push(node);
} else for (const node of el.querySelectorAll(css)) nodes.push(node);
return _possibleConstructorReturn(this, _getPrototypeOf(QueryResult).apply(this, arguments));
}
return new QueryResult(...nodes);
return QueryResult;
}(_wrapNativeSuper(Array));
var search = function search(list, el) {
var nodes = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = list[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var CSS = _step.value;
var css = CSS.trim();
if (css.slice(-6) === ':first') {
var node = el.querySelector(css.slice(0, -6));
if (node) nodes.push(node);
} else {
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = el.querySelectorAll(css)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _node = _step2.value;
nodes.push(_node);
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return _construct(QueryResult, nodes);
};
const {
defineProperty
} = Object;
const $$1 = defineProperty((CSS, parent = document) => {
switch (typeof CSS) {
var defineProperty = Object.defineProperty;
var $$1 = defineProperty(function (CSS) {
var parent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document;
switch (_typeof(CSS)) {
case 'string':
return search(CSS.split(','), parent);
case 'object':
return new QueryResult(...('nodeType' in CSS || 'postMessage' in CSS ? [CSS] : CSS));
return _construct(QueryResult, _toConsumableArray('nodeType' in CSS || 'postMessage' in CSS ? [CSS] : CSS));
case 'function':
const $parent = $$1(parent);
const $window = $$1(parent.defaultView);
const handler = {
handleEvent(event) {
var $parent = $$1(parent);
var $window = $$1(parent.defaultView);
var handler = {
handleEvent: function handleEvent(event) {
$parent.off(DOM_CONTENT_LOADED, handler);
$window.off(LOAD, handler);
CSS(event);
}
};
$parent.on(DOM_CONTENT_LOADED, handler);
$window.on(LOAD, handler);
const rs = parent.readyState;
if (rs == 'complete' || rs != 'loading' && !parent.documentElement.doScroll) setTimeout(() => $parent.dispatch(DOM_CONTENT_LOADED));
var rs = parent.readyState;
if (rs == 'complete' || rs != 'loading' && !parent.documentElement.doScroll) setTimeout(function () {
return $parent.dispatch(DOM_CONTENT_LOADED);
});
return $$1;
}
}, Symbol.hasInstance, {
value: instance => instance instanceof QueryResult
value: function value(instance) {
return instance instanceof QueryResult;
}
});
$$1.extend = (key, value) => (defineProperty(QueryResult.prototype, key, {
configurable: true,
value
}), $$1);
$$1.extend = function (key, value) {
return defineProperty(QueryResult.prototype, key, {
configurable: true,
value: value
}), $$1;
};
$$1.extend('dispatch', function dispatch(type, init = {}) {
const event = new CustomEvent(type, init);
$$1.extend('dispatch', function dispatch(type) {
var init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var event = new CustomEvent(type, init);
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
for (const node of this) node.dispatchEvent(event);
try {
for (var _iterator3 = this[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var node = _step3.value;
node.dispatchEvent(event);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return this;
}).extend('off', function off(type, handler, options = false) {
for (const node of this) node.removeEventListener(type, handler, options);
}).extend('off', function off(type, handler) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = this[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var node = _step4.value;
node.removeEventListener(type, handler, options);
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
return this;
}).extend('on', function on(type, handler, options = false) {
for (const node of this) node.addEventListener(type, handler, options);
}).extend('on', function on(type, handler) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = this[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var node = _step5.value;
node.addEventListener(type, handler, options);
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return != null) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
return this;
});
function _typeof$2(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof$2 = function (obj) {
return typeof obj;
if (typeof Symbol === "function" && _typeof(Symbol.iterator) === "symbol") {
_typeof$2 = function _typeof$$1(obj) {
return _typeof(obj);
};
} else {
_typeof$2 = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
_typeof$2 = function _typeof$$1(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof(obj);
};
}
@ -2131,7 +2380,9 @@
function _arrayWithoutHoles$1(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
}

View File

@ -1,7 +1,7 @@
import {jml, body, nbsp} from '../../external/jamilih/jml-es.js';
import $ from '../../../node_modules/query-result/esm/index.js';
import {manipulation} from '../../../node_modules/qr-manipulation/dist/index-es.js';
import $ from '../../external/query-result/esm/index.js';
import {manipulation} from '../../external/qr-manipulation/dist/index-es.js';
manipulation($, jml);
const baseAPIURL = 'https://openclipart.org/search/json/';

View File

@ -0,0 +1,308 @@
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
}
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
function convertToString(type, content) {
switch (_typeof(content)) {
case 'object':
{
if (!content) {
throw new TypeError('Cannot supply `null`');
}
switch (content.nodeType) {
case 1:
{
// ELEMENT
return content.outerHTML;
}
case 3:
{
// TEXT
return content.nodeValue;
}
case 11:
{
// DOCUMENT_FRAGMENT_NODE
return _toConsumableArray(content.childNodes).reduce(function (s, node) {
return s + convertToString(type, node);
}, '');
}
} // Todo: array of elements/text nodes (or Jamilih array?), QueryResult objects?
return;
}
case 'string':
{
return content;
}
default:
throw new TypeError('Bad content for ' + type + '; type: ' + _typeof(content));
}
}
function convertToDOM(type, content, avoidClone) {
switch (_typeof(content)) {
case 'object':
{
if (!content) {
throw new TypeError('Cannot supply `null`');
}
if ([1, // ELEMENT
3, // TEXT
11 // Document fragment
].includes(content.nodeType)) {
return avoidClone ? content : content.cloneNode(true);
} // Todo: array of elements/text nodes (or Jamilih array?), QueryResult objects?
return;
}
case 'string':
{
var div = document.createElement('div');
div.innerHTML = content;
return div.firstElementChild || div.firstChild;
}
default:
throw new TypeError('Bad content for ' + type + '; type: ' + _typeof(content));
}
}
function insert(type) {
return function () {
var _this = this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var cbOrContent = args[0];
switch (_typeof(cbOrContent)) {
case 'function':
{
this.forEach(function (node, i) {
var ret = cbOrContent.call(_this, i, node.textContent);
node[type](ret);
});
break;
}
default:
{
this.forEach(function (node) {
node[type].apply(node, _toConsumableArray(args.map(function (content, i) {
return convertToDOM(type, content, i === args.length - 1);
})));
});
break;
}
}
return this;
};
}
function insertText(type) {
return function (cbOrContent) {
var _this2 = this;
switch (_typeof(cbOrContent)) {
case 'function':
{
this.forEach(function (node, i) {
var ret = cbOrContent.call(_this2, i, node[type]);
node[type] = convertToString(type, ret);
});
break;
}
default:
{
this.forEach(function (node) {
node[type] = convertToString(type, cbOrContent);
});
break;
}
}
return this;
};
}
var after = insert('after');
var before = insert('before');
var append = insert('append');
var prepend = insert('prepend');
var html = insertText('innerHTML');
var text = insertText('textContent');
function classManipulation(type) {
return function (cbOrContent) {
var _this3 = this;
switch (_typeof(cbOrContent)) {
case 'function':
{
this.forEach(function (node, i) {
var _node$classList;
var ret = cbOrContent.call(_this3, i, node.className);
(_node$classList = node.classList)[type].apply(_node$classList, _toConsumableArray(ret.split(' ')));
});
break;
}
default:
{
if (type === 'remove' && !cbOrContent) {
this.forEach(function (node) {
node.className = '';
});
break;
}
this.forEach(function (node) {
var _node$classList2;
(_node$classList2 = node.classList)[type].apply(_node$classList2, _toConsumableArray(cbOrContent.split(' ')));
});
break;
}
}
return this;
};
}
var addClass = classManipulation('add');
var removeClass = classManipulation('remove');
var hasClass = function hasClass(className) {
return this.some(function (node) {
return node.classList.contains(className);
});
};
var toggleClass = function toggleClass(classNameOrCb, state) {
var _this4 = this;
switch (typeof cbOrContent === "undefined" ? "undefined" : _typeof(cbOrContent)) {
case 'function':
{
if (typeof state === 'boolean') {
this.forEach(function (node, i) {
var _node$classList3;
var ret = classNameOrCb.call(_this4, i, node.className, state);
(_node$classList3 = node.classList).toggle.apply(_node$classList3, _toConsumableArray(ret.split(' ')).concat([state]));
});
} else {
this.forEach(function (node, i) {
var _node$classList4;
var ret = classNameOrCb.call(_this4, i, node.className, state);
(_node$classList4 = node.classList).toggle.apply(_node$classList4, _toConsumableArray(ret.split(' ')));
});
}
break;
}
case 'string':
{
if (typeof state === 'boolean') {
this.forEach(function (node) {
var _node$classList5;
(_node$classList5 = node.classList).toggle.apply(_node$classList5, _toConsumableArray(classNameOrCb.split(' ')).concat([state]));
});
} else {
this.forEach(function (node) {
var _node$classList6;
(_node$classList6 = node.classList).toggle.apply(_node$classList6, _toConsumableArray(classNameOrCb.split(' ')));
});
}
break;
}
}
};
var methods = {
after: after,
before: before,
append: append,
prepend: prepend,
html: html,
text: text
};
var manipulation = function manipulation($, jml) {
['after', 'before', 'append', 'prepend', 'html', 'text'].forEach(function (method) {
$.extend(method, methods[method]);
});
if (jml) {
$.extend('jml', function () {
var _this5 = this;
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
this.forEach(function (node) {
while (node.hasChildNodes()) {
node.firstChild.remove();
}
var n = jml.apply(void 0, args);
return append.call(_this5, n);
});
});
}
return $;
};
export { after, before, append, prepend, html, text, addClass, removeClass, hasClass, toggleClass, manipulation };

View File

@ -0,0 +1,62 @@
const LOAD = 'load';
const DOM_CONTENT_LOADED = 'DOMContentLoaded';
class QueryResult extends Array {}
const search = (list, el) => {
const nodes = [];
for (const CSS of list) {
const css = CSS.trim();
if (css.slice(-6) === ':first') {
const node = el.querySelector(css.slice(0, -6));
if (node) nodes.push(node);
} else
for (const node of el.querySelectorAll(css))
nodes.push(node);
}
return new QueryResult(...nodes);
};
const {defineProperty} = Object;
const $ = defineProperty(
(CSS, parent = document) => {
switch (typeof CSS) {
case 'string': return search(CSS.split(','), parent);
case 'object': return new QueryResult(
...(('nodeType' in CSS || 'postMessage' in CSS) ? [CSS] : CSS)
);
case 'function':
const $parent = $(parent);
const $window = $(parent.defaultView);
const handler = {handleEvent(event) {
$parent.off(DOM_CONTENT_LOADED, handler);
$window.off(LOAD, handler);
CSS(event);
}};
$parent.on(DOM_CONTENT_LOADED, handler);
$window.on(LOAD, handler);
const rs = parent.readyState;
if (rs == 'complete' || (rs != 'loading' && !parent.documentElement.doScroll))
setTimeout(() => $parent.dispatch(DOM_CONTENT_LOADED));
return $;
}
},
Symbol.hasInstance,
{value: instance => instance instanceof QueryResult}
);
$.extend = (key, value) => (defineProperty(
QueryResult.prototype,
key, {configurable: true, value}
), $);
$.extend('dispatch', function dispatch(type, init = {}) {
const event = new CustomEvent(type, init);
for (const node of this) node.dispatchEvent(event);
return this;
})
.extend('off', function off(type, handler, options = false) {
for (const node of this) node.removeEventListener(type, handler, options);
return this;
})
.extend('on', function on(type, handler, options = false) {
for (const node of this) node.addEventListener(type, handler, options);
return this;
});
export default $;

View File

@ -18,7 +18,7 @@
"build-doc": "rm -rf docs/jsdoc/*;jsdoc --pedantic -c docs/jsdoc-config.json editor",
"build-html": "node build-html.js",
"compress-images": "imageoptim 'chrome-app/*.png' && imageoptim 'editor/extensions/*.png' && imageoptim 'editor/spinbtn/*.png' && imageoptim 'editor/jgraduate/images/*.{png,gif}' && imageoptim 'editor/images/*.png'",
"copy-deps": "cp node_modules/load-stylesheets/dist/index-es.js editor/external/load-stylesheets/index-es.js && cp node_modules/@babel/polyfill/dist/polyfill.min.js editor/external/@babel/polyfill/polyfill.min.js && cp node_modules/@babel/polyfill/dist/polyfill.js editor/external/@babel/polyfill/polyfill.js && cp node_modules/jamilih/dist/jml-es.js editor/external/jamilih/jml-es.js",
"copy-deps": "cp node_modules/load-stylesheets/dist/index-es.js editor/external/load-stylesheets/index-es.js && cp node_modules/@babel/polyfill/dist/polyfill.min.js editor/external/@babel/polyfill/polyfill.min.js && cp node_modules/@babel/polyfill/dist/polyfill.js editor/external/@babel/polyfill/polyfill.js && cp node_modules/jamilih/dist/jml-es.js editor/external/jamilih/jml-es.js && cp node_modules/query-result/esm/index.js editor/external/query-result/esm/index.js && cp node_modules/qr-manipulation/dist/index-es.js editor/external/qr-manipulation/dist/index-es.js",
"eslint": "eslint .",
"rollup": "rollup -c",
"start-embedded": "echo \"Open file to http://localhost:8000/editor/embedapi.html\" && static -p 8000 | static -p 8001 -H '{\"Access-Control-Allow-Origin\": \"*\"}'",