189e2f9d1SAndreas Gohrconst plugin = document.getElementById('plugin_info'); 289e2f9d1SAndreas Gohrconst component = document.getElementById('plugin_components'); 389e2f9d1SAndreas Gohrconst output = document.getElementById('output'); 489e2f9d1SAndreas GohrupdatePluginNameStatus(); 589e2f9d1SAndreas Gohr 689e2f9d1SAndreas Gohr/** 789e2f9d1SAndreas Gohr * disable the plugin name field when a component has been added 889e2f9d1SAndreas Gohr */ 989e2f9d1SAndreas Gohrfunction updatePluginNameStatus() { 1089e2f9d1SAndreas Gohr plugin.querySelector('input').readOnly = output.children.length > 0; 1189e2f9d1SAndreas Gohr document.querySelector('button[type=submit]').disabled = output.children.length === 0; 1289e2f9d1SAndreas Gohr} 1389e2f9d1SAndreas Gohr 1489e2f9d1SAndreas Gohr/** 1589e2f9d1SAndreas Gohr * Create the HTML element for a component 1689e2f9d1SAndreas Gohr * 1789e2f9d1SAndreas Gohr * @param {string} plugin The plugin base name 1889e2f9d1SAndreas Gohr * @param {string} type The component type 1989e2f9d1SAndreas Gohr * @param {string} name The component name 2089e2f9d1SAndreas Gohr * @returns {HTMLLIElement|null} null if the component already exists 2189e2f9d1SAndreas Gohr */ 2289e2f9d1SAndreas Gohrfunction createComponentElement(plugin, type, name) { 2389e2f9d1SAndreas Gohr let id = `${type}_plugin_${plugin}`; 2489e2f9d1SAndreas Gohr if (name !== '') { 2589e2f9d1SAndreas Gohr id += `_${name}`; 2689e2f9d1SAndreas Gohr } 2789e2f9d1SAndreas Gohr 2889e2f9d1SAndreas Gohr if (document.getElementById(`component-${id}`)) { 2989e2f9d1SAndreas Gohr return null; 3089e2f9d1SAndreas Gohr } 3189e2f9d1SAndreas Gohr 3289e2f9d1SAndreas Gohr const li = document.createElement('li'); 3389e2f9d1SAndreas Gohr li.id = `component-${id}`; 3489e2f9d1SAndreas Gohr li.dataset.type = type; 3589e2f9d1SAndreas Gohr li.dataset.name = name; 3689e2f9d1SAndreas Gohr 3789e2f9d1SAndreas Gohr const hidden = document.createElement('input'); 3889e2f9d1SAndreas Gohr hidden.type = 'hidden'; 3989e2f9d1SAndreas Gohr hidden.name = 'components[]'; 4089e2f9d1SAndreas Gohr hidden.value = id; 4189e2f9d1SAndreas Gohr li.append(hidden); 4289e2f9d1SAndreas Gohr 4389e2f9d1SAndreas Gohr const remove = document.createElement('button'); 4489e2f9d1SAndreas Gohr remove.type = 'button'; 4589e2f9d1SAndreas Gohr remove.innerText = 'X'; 4689e2f9d1SAndreas Gohr remove.title = 'Remove this component'; 4789e2f9d1SAndreas Gohr remove.addEventListener('click', function (event) { 4889e2f9d1SAndreas Gohr li.remove(); 4989e2f9d1SAndreas Gohr updatePluginNameStatus(); 5089e2f9d1SAndreas Gohr }); 5189e2f9d1SAndreas Gohr li.append(remove); 5289e2f9d1SAndreas Gohr 5389e2f9d1SAndreas Gohr const label = document.createElement('span'); 5489e2f9d1SAndreas Gohr label.innerText = id; 5589e2f9d1SAndreas Gohr li.append(label); 5689e2f9d1SAndreas Gohr 5789e2f9d1SAndreas Gohr 58*cde324c2SAndreas Gohr // add auto-completion for events 5989e2f9d1SAndreas Gohr if (type === 'action') { 6089e2f9d1SAndreas Gohr const events = document.createElement('input'); 6189e2f9d1SAndreas Gohr events.type = 'text'; 6289e2f9d1SAndreas Gohr events.name = `options[${id}]`; 6389e2f9d1SAndreas Gohr events.placeholder = 'EVENTS_TO_HANDLE, ...'; 6489e2f9d1SAndreas Gohr li.append(events); 6589e2f9d1SAndreas Gohr new Awesomplete(events, { 6689e2f9d1SAndreas Gohr list: ACTION_EVENTS, 6789e2f9d1SAndreas Gohr filter: function (text, input) { 6889e2f9d1SAndreas Gohr return Awesomplete.FILTER_CONTAINS(text, input.match(/[^,]*$/)[0]); 6989e2f9d1SAndreas Gohr }, 7089e2f9d1SAndreas Gohr 7189e2f9d1SAndreas Gohr item: function (text, input) { 7289e2f9d1SAndreas Gohr return Awesomplete.ITEM(text, input.match(/[^,]*$/)[0]); 7389e2f9d1SAndreas Gohr }, 7489e2f9d1SAndreas Gohr 7589e2f9d1SAndreas Gohr replace: function (text) { 7689e2f9d1SAndreas Gohr var before = this.input.value.match(/^.+,\s*|/)[0]; 7789e2f9d1SAndreas Gohr this.input.value = before + text + ", "; 7889e2f9d1SAndreas Gohr } 7989e2f9d1SAndreas Gohr }); 8089e2f9d1SAndreas Gohr } 8189e2f9d1SAndreas Gohr 82*cde324c2SAndreas Gohr // allow picking the renderer to extend 83*cde324c2SAndreas Gohr if(type === 'renderer') { 84*cde324c2SAndreas Gohr const renderers = document.createElement('select'); 85*cde324c2SAndreas Gohr renderers.name = `options[${id}]`; 86*cde324c2SAndreas Gohr 87*cde324c2SAndreas Gohr const opt1 = document.createElement('option'); 88*cde324c2SAndreas Gohr opt1.value = 'Doku_Renderer_xhtml'; 89*cde324c2SAndreas Gohr opt1.innerText = 'Extend Doku_Renderer_xhtml'; 90*cde324c2SAndreas Gohr 91*cde324c2SAndreas Gohr const opt2 = document.createElement('option'); 92*cde324c2SAndreas Gohr opt2.value = 'Doku_Renderer'; 93*cde324c2SAndreas Gohr opt2.innerText = 'Extend Doku_Renderer'; 94*cde324c2SAndreas Gohr 95*cde324c2SAndreas Gohr renderers.append(opt1); 96*cde324c2SAndreas Gohr renderers.append(opt2); 97*cde324c2SAndreas Gohr li.append(renderers); 98*cde324c2SAndreas Gohr } 99*cde324c2SAndreas Gohr 10089e2f9d1SAndreas Gohr 10189e2f9d1SAndreas Gohr return li; 10289e2f9d1SAndreas Gohr} 10389e2f9d1SAndreas Gohr 10489e2f9d1SAndreas Gohr 10589e2f9d1SAndreas Gohr/** 10689e2f9d1SAndreas Gohr * Add a component to the output list 10789e2f9d1SAndreas Gohr */ 10889e2f9d1SAndreas Gohrcomponent.querySelector('button').addEventListener('click', function (event) { 10989e2f9d1SAndreas Gohr const plugin_base = plugin.querySelector('input'); // first input field is plugin base name 11089e2f9d1SAndreas Gohr const component_type = component.querySelector('select'); 11189e2f9d1SAndreas Gohr const component_name = component.querySelector('input'); 11289e2f9d1SAndreas Gohr 11389e2f9d1SAndreas Gohr if (!plugin_base.validity.valid) { 11489e2f9d1SAndreas Gohr plugin_base.reportValidity(); 11589e2f9d1SAndreas Gohr plugin_base.focus(); 11689e2f9d1SAndreas Gohr return; 11789e2f9d1SAndreas Gohr } 11889e2f9d1SAndreas Gohr 11989e2f9d1SAndreas Gohr if (!component_name.validity.valid) { 12089e2f9d1SAndreas Gohr component_name.reportValidity(); 12189e2f9d1SAndreas Gohr component_name.focus(); 12289e2f9d1SAndreas Gohr return; 12389e2f9d1SAndreas Gohr } 12489e2f9d1SAndreas Gohr 12589e2f9d1SAndreas Gohr const li = createComponentElement(plugin_base.value, component_type.value, component_name.value); 12689e2f9d1SAndreas Gohr if (!li) return; 12789e2f9d1SAndreas Gohr 12889e2f9d1SAndreas Gohr output.appendChild(li); 12989e2f9d1SAndreas Gohr updatePluginNameStatus(); 13089e2f9d1SAndreas Gohr}); 131