2020-09-05 11:50:20 -04:00
|
|
|
import addRemoveRows from "./add-remove-rows.js"
|
|
|
|
import ajaxDeleteRow from "./ajax-delete-row.js"
|
|
|
|
import ajaxForm from "./ajax-form.js"
|
|
|
|
import attachments from "./attachments.js"
|
2021-06-06 08:55:56 -04:00
|
|
|
import attachmentsList from "./attachments-list.js"
|
2020-09-05 11:50:20 -04:00
|
|
|
import autoSuggest from "./auto-suggest.js"
|
|
|
|
import backToTop from "./back-to-top.js"
|
|
|
|
import bookSort from "./book-sort.js"
|
|
|
|
import chapterToggle from "./chapter-toggle.js"
|
|
|
|
import codeEditor from "./code-editor.js"
|
|
|
|
import codeHighlighter from "./code-highlighter.js"
|
|
|
|
import collapsible from "./collapsible.js"
|
|
|
|
import customCheckbox from "./custom-checkbox.js"
|
|
|
|
import detailsHighlighter from "./details-highlighter.js"
|
|
|
|
import dropdown from "./dropdown.js"
|
2020-12-31 12:25:20 -05:00
|
|
|
import dropdownSearch from "./dropdown-search.js"
|
2020-09-05 11:50:20 -04:00
|
|
|
import dropzone from "./dropzone.js"
|
|
|
|
import editorToolbox from "./editor-toolbox.js"
|
|
|
|
import entityPermissionsEditor from "./entity-permissions-editor.js"
|
|
|
|
import entitySearch from "./entity-search.js"
|
|
|
|
import entitySelector from "./entity-selector.js"
|
|
|
|
import entitySelectorPopup from "./entity-selector-popup.js"
|
|
|
|
import eventEmitSelect from "./event-emit-select.js"
|
|
|
|
import expandToggle from "./expand-toggle.js"
|
|
|
|
import headerMobileToggle from "./header-mobile-toggle.js"
|
|
|
|
import homepageControl from "./homepage-control.js"
|
|
|
|
import imageManager from "./image-manager.js"
|
|
|
|
import imagePicker from "./image-picker.js"
|
|
|
|
import index from "./index.js"
|
|
|
|
import listSortControl from "./list-sort-control.js"
|
|
|
|
import markdownEditor from "./markdown-editor.js"
|
|
|
|
import newUserPassword from "./new-user-password.js"
|
|
|
|
import notification from "./notification.js"
|
|
|
|
import optionalInput from "./optional-input.js"
|
|
|
|
import pageComments from "./page-comments.js"
|
|
|
|
import pageDisplay from "./page-display.js"
|
|
|
|
import pageEditor from "./page-editor.js"
|
|
|
|
import pagePicker from "./page-picker.js"
|
|
|
|
import permissionsTable from "./permissions-table.js"
|
|
|
|
import popup from "./popup.js"
|
|
|
|
import settingAppColorPicker from "./setting-app-color-picker.js"
|
|
|
|
import settingColorPicker from "./setting-color-picker.js"
|
|
|
|
import shelfSort from "./shelf-sort.js"
|
|
|
|
import sidebar from "./sidebar.js"
|
|
|
|
import sortableList from "./sortable-list.js"
|
2020-09-19 07:06:45 -04:00
|
|
|
import submitOnChange from "./submit-on-change.js"
|
2020-09-05 11:50:20 -04:00
|
|
|
import tabs from "./tabs.js"
|
|
|
|
import tagManager from "./tag-manager.js"
|
|
|
|
import templateManager from "./template-manager.js"
|
|
|
|
import toggleSwitch from "./toggle-switch.js"
|
|
|
|
import triLayout from "./tri-layout.js"
|
2020-12-31 12:25:20 -05:00
|
|
|
import userSelect from "./user-select.js"
|
2020-09-05 11:50:20 -04:00
|
|
|
import wysiwygEditor from "./wysiwyg-editor.js"
|
2017-08-06 13:01:49 -04:00
|
|
|
|
2020-09-05 11:50:20 -04:00
|
|
|
const componentMapping = {
|
|
|
|
"add-remove-rows": addRemoveRows,
|
|
|
|
"ajax-delete-row": ajaxDeleteRow,
|
|
|
|
"ajax-form": ajaxForm,
|
|
|
|
"attachments": attachments,
|
2021-06-06 08:55:56 -04:00
|
|
|
"attachments-list": attachmentsList,
|
2020-09-05 11:50:20 -04:00
|
|
|
"auto-suggest": autoSuggest,
|
|
|
|
"back-to-top": backToTop,
|
|
|
|
"book-sort": bookSort,
|
|
|
|
"chapter-toggle": chapterToggle,
|
|
|
|
"code-editor": codeEditor,
|
|
|
|
"code-highlighter": codeHighlighter,
|
|
|
|
"collapsible": collapsible,
|
|
|
|
"custom-checkbox": customCheckbox,
|
|
|
|
"details-highlighter": detailsHighlighter,
|
|
|
|
"dropdown": dropdown,
|
2020-12-31 12:25:20 -05:00
|
|
|
"dropdown-search": dropdownSearch,
|
2020-09-05 11:50:20 -04:00
|
|
|
"dropzone": dropzone,
|
|
|
|
"editor-toolbox": editorToolbox,
|
|
|
|
"entity-permissions-editor": entityPermissionsEditor,
|
|
|
|
"entity-search": entitySearch,
|
|
|
|
"entity-selector": entitySelector,
|
|
|
|
"entity-selector-popup": entitySelectorPopup,
|
|
|
|
"event-emit-select": eventEmitSelect,
|
|
|
|
"expand-toggle": expandToggle,
|
|
|
|
"header-mobile-toggle": headerMobileToggle,
|
|
|
|
"homepage-control": homepageControl,
|
|
|
|
"image-manager": imageManager,
|
|
|
|
"image-picker": imagePicker,
|
|
|
|
"index": index,
|
|
|
|
"list-sort-control": listSortControl,
|
|
|
|
"markdown-editor": markdownEditor,
|
|
|
|
"new-user-password": newUserPassword,
|
|
|
|
"notification": notification,
|
|
|
|
"optional-input": optionalInput,
|
|
|
|
"page-comments": pageComments,
|
|
|
|
"page-display": pageDisplay,
|
|
|
|
"page-editor": pageEditor,
|
|
|
|
"page-picker": pagePicker,
|
|
|
|
"permissions-table": permissionsTable,
|
|
|
|
"popup": popup,
|
|
|
|
"setting-app-color-picker": settingAppColorPicker,
|
|
|
|
"setting-color-picker": settingColorPicker,
|
|
|
|
"shelf-sort": shelfSort,
|
|
|
|
"sidebar": sidebar,
|
|
|
|
"sortable-list": sortableList,
|
2020-09-19 07:06:45 -04:00
|
|
|
"submit-on-change": submitOnChange,
|
2020-09-05 11:50:20 -04:00
|
|
|
"tabs": tabs,
|
|
|
|
"tag-manager": tagManager,
|
|
|
|
"template-manager": templateManager,
|
|
|
|
"toggle-switch": toggleSwitch,
|
|
|
|
"tri-layout": triLayout,
|
2020-12-31 12:25:20 -05:00
|
|
|
"user-select": userSelect,
|
2020-09-05 11:50:20 -04:00
|
|
|
"wysiwyg-editor": wysiwygEditor,
|
|
|
|
};
|
2017-08-06 13:01:49 -04:00
|
|
|
|
2020-06-24 15:38:08 -04:00
|
|
|
window.components = {};
|
2017-08-06 13:01:49 -04:00
|
|
|
|
2017-09-09 10:56:24 -04:00
|
|
|
/**
|
|
|
|
* Initialize components of the given name within the given element.
|
|
|
|
* @param {String} componentName
|
|
|
|
* @param {HTMLElement|Document} parentElement
|
|
|
|
*/
|
2020-06-24 15:38:08 -04:00
|
|
|
function searchForComponentInParent(componentName, parentElement) {
|
|
|
|
const elems = parentElement.querySelectorAll(`[${componentName}]`);
|
2017-08-06 13:01:49 -04:00
|
|
|
for (let j = 0, jLen = elems.length; j < jLen; j++) {
|
2020-06-24 15:38:08 -04:00
|
|
|
initComponent(componentName, elems[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a component instance on the given dom element.
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
|
|
|
function initComponent(name, element) {
|
|
|
|
const componentModel = componentMapping[name];
|
|
|
|
if (componentModel === undefined) return;
|
|
|
|
|
|
|
|
// Create our component instance
|
|
|
|
let instance;
|
|
|
|
try {
|
|
|
|
instance = new componentModel(element);
|
|
|
|
instance.$el = element;
|
2020-06-27 18:56:01 -04:00
|
|
|
const allRefs = parseRefs(name, element);
|
|
|
|
instance.$refs = allRefs.refs;
|
|
|
|
instance.$manyRefs = allRefs.manyRefs;
|
2020-06-24 15:38:08 -04:00
|
|
|
instance.$opts = parseOpts(name, element);
|
2020-06-30 17:12:45 -04:00
|
|
|
instance.$emit = (eventName, data = {}) => {
|
|
|
|
data.from = instance;
|
|
|
|
const event = new CustomEvent(`${name}-${eventName}`, {
|
|
|
|
bubbles: true,
|
|
|
|
detail: data
|
|
|
|
});
|
|
|
|
instance.$el.dispatchEvent(event);
|
|
|
|
};
|
2020-06-24 15:38:08 -04:00
|
|
|
if (typeof instance.setup === 'function') {
|
|
|
|
instance.setup();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to create component', e, name, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add to global listing
|
|
|
|
if (typeof window.components[name] === "undefined") {
|
|
|
|
window.components[name] = [];
|
|
|
|
}
|
|
|
|
window.components[name].push(instance);
|
|
|
|
|
|
|
|
// Add to element listing
|
|
|
|
if (typeof element.components === 'undefined') {
|
|
|
|
element.components = {};
|
|
|
|
}
|
|
|
|
element.components[name] = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse out the element references within the given element
|
|
|
|
* for the given component name.
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
|
|
|
function parseRefs(name, element) {
|
|
|
|
const refs = {};
|
2020-06-27 18:56:01 -04:00
|
|
|
const manyRefs = {};
|
2020-06-29 17:11:03 -04:00
|
|
|
|
2020-06-24 15:38:08 -04:00
|
|
|
const prefix = `${name}@`
|
2020-06-29 17:11:03 -04:00
|
|
|
const selector = `[refs*="${prefix}"]`;
|
|
|
|
const refElems = [...element.querySelectorAll(selector)];
|
|
|
|
if (element.matches(selector)) {
|
|
|
|
refElems.push(element);
|
|
|
|
}
|
|
|
|
|
2020-06-24 15:38:08 -04:00
|
|
|
for (const el of refElems) {
|
|
|
|
const refNames = el.getAttribute('refs')
|
|
|
|
.split(' ')
|
|
|
|
.filter(str => str.startsWith(prefix))
|
2020-06-29 17:11:03 -04:00
|
|
|
.map(str => str.replace(prefix, ''))
|
|
|
|
.map(kebabToCamel);
|
2020-06-24 15:38:08 -04:00
|
|
|
for (const ref of refNames) {
|
|
|
|
refs[ref] = el;
|
2020-06-27 18:56:01 -04:00
|
|
|
if (typeof manyRefs[ref] === 'undefined') {
|
|
|
|
manyRefs[ref] = [];
|
|
|
|
}
|
|
|
|
manyRefs[ref].push(el);
|
2020-06-24 15:38:08 -04:00
|
|
|
}
|
2017-08-06 13:01:49 -04:00
|
|
|
}
|
2020-06-27 18:56:01 -04:00
|
|
|
return {refs, manyRefs};
|
2020-06-24 15:38:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse out the element component options.
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Element} element
|
|
|
|
* @return {Object<String, String>}
|
|
|
|
*/
|
|
|
|
function parseOpts(name, element) {
|
|
|
|
const opts = {};
|
|
|
|
const prefix = `option:${name}:`;
|
|
|
|
for (const {name, value} of element.attributes) {
|
|
|
|
if (name.startsWith(prefix)) {
|
|
|
|
const optName = name.replace(prefix, '');
|
|
|
|
opts[kebabToCamel(optName)] = value || '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a kebab-case string to camelCase
|
|
|
|
* @param {String} kebab
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function kebabToCamel(kebab) {
|
|
|
|
const ucFirst = (word) => word.slice(0,1).toUpperCase() + word.slice(1);
|
|
|
|
const words = kebab.split('-');
|
2020-07-05 16:18:17 -04:00
|
|
|
return words[0] + words.slice(1).map(ucFirst).join('');
|
2017-09-09 10:56:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize all components found within the given element.
|
|
|
|
* @param parentElement
|
|
|
|
*/
|
|
|
|
function initAll(parentElement) {
|
|
|
|
if (typeof parentElement === 'undefined') parentElement = document;
|
2020-06-24 15:38:08 -04:00
|
|
|
|
|
|
|
// Old attribute system
|
|
|
|
for (const componentName of Object.keys(componentMapping)) {
|
|
|
|
searchForComponentInParent(componentName, parentElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
// New component system
|
|
|
|
const componentElems = parentElement.querySelectorAll(`[component],[components]`);
|
|
|
|
|
|
|
|
for (const el of componentElems) {
|
|
|
|
const componentNames = `${el.getAttribute('component') || ''} ${(el.getAttribute('components'))}`.toLowerCase().split(' ').filter(Boolean);
|
|
|
|
for (const name of componentNames) {
|
|
|
|
initComponent(name, el);
|
|
|
|
}
|
2017-09-09 10:56:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 12:16:30 -04:00
|
|
|
window.components.init = initAll;
|
2020-06-27 18:56:01 -04:00
|
|
|
window.components.first = (name) => (window.components[name] || [null])[0];
|
2018-03-11 12:16:30 -04:00
|
|
|
|
2018-11-11 06:44:35 -05:00
|
|
|
export default initAll;
|
2020-06-24 15:38:08 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef Component
|
|
|
|
* @property {HTMLElement} $el
|
|
|
|
* @property {Object<String, HTMLElement>} $refs
|
2020-06-27 18:56:01 -04:00
|
|
|
* @property {Object<String, HTMLElement[]>} $manyRefs
|
2020-06-24 15:38:08 -04:00
|
|
|
* @property {Object<String, String>} $opts
|
2020-06-30 17:12:45 -04:00
|
|
|
* @property {function(string, Object)} $emit
|
2020-06-24 15:38:08 -04:00
|
|
|
*/
|