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"
|
2022-11-12 10:10:14 -05:00
|
|
|
import autoSubmit from "./auto-submit.js";
|
2020-09-05 11:50:20 -04:00
|
|
|
import backToTop from "./back-to-top.js"
|
|
|
|
import bookSort from "./book-sort.js"
|
2022-05-18 09:06:40 -04:00
|
|
|
import chapterContents from "./chapter-contents.js"
|
2020-09-05 11:50:20 -04:00
|
|
|
import codeEditor from "./code-editor.js"
|
|
|
|
import codeHighlighter from "./code-highlighter.js"
|
2022-05-17 12:39:31 -04:00
|
|
|
import codeTextarea from "./code-textarea.js"
|
2020-09-05 11:50:20 -04:00
|
|
|
import collapsible from "./collapsible.js"
|
2022-04-20 09:58:37 -04:00
|
|
|
import confirmDialog from "./confirm-dialog"
|
2020-09-05 11:50:20 -04:00
|
|
|
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"
|
2022-10-02 13:09:48 -04:00
|
|
|
import entityPermissions from "./entity-permissions";
|
2020-09-05 11:50:20 -04:00
|
|
|
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 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"
|
2022-10-18 17:02:34 -04:00
|
|
|
import pointer from "./pointer.js";
|
2020-09-05 11:50:20 -04:00
|
|
|
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"
|
2022-11-04 11:20:19 -04:00
|
|
|
import shortcuts from "./shortcuts";
|
2022-11-09 09:40:44 -05:00
|
|
|
import shortcutInput from "./shortcut-input";
|
2020-09-05 11:50:20 -04:00
|
|
|
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"
|
2021-12-08 09:29:42 -05:00
|
|
|
import webhookEvents from "./webhook-events";
|
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,
|
2022-11-12 10:10:14 -05:00
|
|
|
"auto-submit": autoSubmit,
|
2020-09-05 11:50:20 -04:00
|
|
|
"back-to-top": backToTop,
|
|
|
|
"book-sort": bookSort,
|
2022-05-18 09:06:40 -04:00
|
|
|
"chapter-contents": chapterContents,
|
2020-09-05 11:50:20 -04:00
|
|
|
"code-editor": codeEditor,
|
|
|
|
"code-highlighter": codeHighlighter,
|
2022-05-17 12:39:31 -04:00
|
|
|
"code-textarea": codeTextarea,
|
2020-09-05 11:50:20 -04:00
|
|
|
"collapsible": collapsible,
|
2022-04-20 09:58:37 -04:00
|
|
|
"confirm-dialog": confirmDialog,
|
2020-09-05 11:50:20 -04:00
|
|
|
"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,
|
2022-10-02 13:09:48 -04:00
|
|
|
"entity-permissions": entityPermissions,
|
2020-09-05 11:50:20 -04:00
|
|
|
"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,
|
|
|
|
"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,
|
2022-10-18 17:02:34 -04:00
|
|
|
"pointer": pointer,
|
2020-09-05 11:50:20 -04:00
|
|
|
"popup": popup,
|
|
|
|
"setting-app-color-picker": settingAppColorPicker,
|
|
|
|
"setting-color-picker": settingColorPicker,
|
|
|
|
"shelf-sort": shelfSort,
|
2022-11-04 11:20:19 -04:00
|
|
|
"shortcuts": shortcuts,
|
2022-11-09 09:40:44 -05:00
|
|
|
"shortcut-input": shortcutInput,
|
2020-09-05 11:50:20 -04:00
|
|
|
"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,
|
2021-12-08 09:29:42 -05:00
|
|
|
"webhook-events": webhookEvents,
|
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
|
|
|
*/
|