From 5394480ac8a6dd555e21b3d1675dff757bd05f04 Mon Sep 17 00:00:00 2001 From: Karl Kemister-Sheppard Date: Tue, 8 Oct 2024 15:42:02 +1000 Subject: [PATCH] DOC-2513: fix store variable declaration issue, add missing semicolons and added advtemplate_get_template throw error. --- .../advtemplate-readonly-template/example.js | 223 ++++++++---------- .../advtemplate-readonly-template/index.js | 167 +++++++------ 2 files changed, 180 insertions(+), 210 deletions(-) diff --git a/modules/ROOT/examples/live-demos/advtemplate-readonly-template/example.js b/modules/ROOT/examples/live-demos/advtemplate-readonly-template/example.js index 9e17f2a608..ea6e922e91 100644 --- a/modules/ROOT/examples/live-demos/advtemplate-readonly-template/example.js +++ b/modules/ROOT/examples/live-demos/advtemplate-readonly-template/example.js @@ -1,130 +1,101 @@ const headers = { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - } - - const handleResponse = (message) => (response) => { - if (!response.ok) { - response.text().then((message) => console.error(message)) + 'Accept': 'application/json', + 'Content-Type': 'application/json', +}; + +const handleResponse = (message) => (response) => { + if (!response.ok) { + return response.text().then((error) => { + console.error(error); throw new Error(message); - } else { - return response.json(); - } - } - - const advtemplate_list = () => fetch('/categories', { - method: 'GET', - headers, - }) - .then(handleResponse('Failed to get template list')) - - const advtemplate_get_template = (id) => fetch('/templates/' + id, { - method: 'GET', - headers, - }) - .then(handleResponse('Failed to get template')) - - const advtemplate_create_category = (title) => { - return fetch('/categories', { - method: 'POST', - body: JSON.stringify({ - title - }), - headers, - }) - .then(handleResponse('Failed to create category')) - } - - const advtemplate_create_template = (title, content, categoryId) => { - return fetch('/templates', { - method: 'POST', - body: JSON.stringify({ - title, - content, - categoryId - }), - headers, - }) - .then(handleResponse('Failed to create template')) - } - - const advtemplate_rename_category = (id, title) => { - return fetch('/categories/' + id, { - method: 'PUT', - body: JSON.stringify({ - title - }), - headers, - }) - .then(handleResponse('Failed to rename category')) + }); } - - const advtemplate_rename_template = (id, title) => { - return fetch('/templates/' + id, { - method: 'PUT', - body: JSON.stringify({ - title - }), - headers, - }) - .then(handleResponse('Failed to rename template')) - } - - const advtemplate_delete_template = (id) => { - return fetch('/templates/' + id, { - method: 'DELETE', - headers, - }) - .then(handleResponse('Failed to delete template')) - } - - const advtemplate_delete_category = (id) => { - return fetch('/categories/' + id, { - method: 'DELETE', - headers, - }) - .then(handleResponse('Failed to delete category')) - } - const advtemplate_move_template = (id, categoryId) => { - return fetch('/templates/' + id, { - method: 'PATCH', - body: JSON.stringify({ - categoryId - }), - headers, - }) - .then(handleResponse('Failed to move template')) - } - - const advtemplate_move_category_items = (id, categoryId) => { - return fetch('/categories/' + id, { - method: 'PATCH', - body: JSON.stringify({ - categoryId - }), - headers, - }) - .then(handleResponse('Failed to move all templates to new category')) - } + return response.json(); +}; + +const advtemplate_list = () => + fetch('/categories', { + method: 'GET', + headers, + }).then(handleResponse('Failed to get template list')); + +const advtemplate_get_template = (id) => + fetch(`/templates/${id}`, { + method: 'GET', + headers, + }).then(handleResponse('Failed to get template')); + +const advtemplate_create_category = (title) => + fetch('/categories', { + method: 'POST', + body: JSON.stringify({ title }), + headers, + }).then(handleResponse('Failed to create category')); + +const advtemplate_create_template = (title, content, categoryId) => + fetch('/templates', { + method: 'POST', + body: JSON.stringify({ title, content, categoryId }), + headers, + }).then(handleResponse('Failed to create template')); + +const advtemplate_rename_category = (id, title) => + fetch(`/categories/${id}`, { + method: 'PUT', + body: JSON.stringify({ title }), + headers, + }).then(handleResponse('Failed to rename category')); + +const advtemplate_rename_template = (id, title) => + fetch(`/templates/${id}`, { + method: 'PUT', + body: JSON.stringify({ title }), + headers, + }).then(handleResponse('Failed to rename template')); + +const advtemplate_delete_template = (id) => + fetch(`/templates/${id}`, { + method: 'DELETE', + headers, + }).then(handleResponse('Failed to delete template')); + +const advtemplate_delete_category = (id) => + fetch(`/categories/${id}`, { + method: 'DELETE', + headers, + }).then(handleResponse('Failed to delete category')); + +const advtemplate_move_template = (id, categoryId) => + fetch(`/templates/${id}`, { + method: 'PATCH', + body: JSON.stringify({ categoryId }), + headers, + }).then(handleResponse('Failed to move template')); + +const advtemplate_move_category_items = (id, categoryId) => + fetch(`/categories/${id}`, { + method: 'PATCH', + body: JSON.stringify({ categoryId }), + headers, + }).then(handleResponse('Failed to move all templates to new category')); - tinymce.init({ - selector: "textarea#readonly-locked-template", - plugins: [ - "advlist", "anchor", "autolink", "charmap", "code", "fullscreen", - "help", "image", "insertdatetime", "link", "lists", "media", - "preview", "searchreplace", "table", "visualblocks", "advtemplate" - ], - contextmenu: 'advtemplate', - toolbar: "addtemplate inserttemplate | undo redo | styles | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image", - advtemplate_list, - advtemplate_get_template, - advtemplate_create_category, - advtemplate_create_template, - advtemplate_rename_category, - advtemplate_rename_template, - advtemplate_delete_template, - advtemplate_delete_category, - advtemplate_move_template, - advtemplate_move_category_items - }); - \ No newline at end of file +tinymce.init({ + selector: "textarea#readonly-locked-template", + plugins: [ + "advlist", "anchor", "autolink", "charmap", "code", "fullscreen", + "help", "image", "insertdatetime", "link", "lists", "media", + "preview", "searchreplace", "table", "visualblocks", "advtemplate" + ], + contextmenu: 'advtemplate', + toolbar: "addtemplate inserttemplate | undo redo | styles | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image", + advtemplate_list, + advtemplate_get_template, + advtemplate_create_category, + advtemplate_create_template, + advtemplate_rename_category, + advtemplate_rename_template, + advtemplate_delete_template, + advtemplate_delete_category, + advtemplate_move_template, + advtemplate_move_category_items, +}); diff --git a/modules/ROOT/examples/live-demos/advtemplate-readonly-template/index.js b/modules/ROOT/examples/live-demos/advtemplate-readonly-template/index.js index 5e7ed7808e..54716c8138 100644 --- a/modules/ROOT/examples/live-demos/advtemplate-readonly-template/index.js +++ b/modules/ROOT/examples/live-demos/advtemplate-readonly-template/index.js @@ -59,14 +59,14 @@ const isStoreError = (e) => const create = (data = []) => { - const store = { + const storeItems = { items: [] } let id = 0; - const genId = () => (++id).toString() - const categoryIndex = {} - const templateIndex = {} + const genId = () => (++id).toString(); + const categoryIndex = {}; + const templateIndex = {}; const isNonEmptyString = (value) => typeof value === 'string' && value.length > 0 @@ -77,57 +77,57 @@ const create = (data = []) => { } const isValidId = (value) => - isNonEmptyString(value) && isPositiveInteger(value) + isNonEmptyString(value) && isPositiveInteger(value) const validate = (value, predicate, message) => { if (predicate(value)) { return } else { - throw new StoreError(400, message) + throw new StoreError(400, message); } } const validateId = (id) => - validate(id, isValidId, `Invalid "id" parameter: ${id}`) + validate(id, isValidId, `Invalid "id" parameter: ${id}`); const validateTitle = (title) => - validate(title, isNonEmptyString, `Invalid "title" parameter: ${title}`) + validate(title, isNonEmptyString, `Invalid "title" parameter: ${title}`); const getParent = (id) => - typeof id === 'undefined' ? store : getCategory(id); + typeof id === 'undefined' ? storeItems : getCategory(id); const filterOut = (items, id) => - items.filter((item) => item.id !== id) + items.filter((item) => item.id !== id); const list = () => { - const resp = [...store.items] + const resp = [...storeItems.items]; return resp } const getCategory = (id) => { validate(id, isValidId, `Invalid "id" parameter: ${id}`) - const category = categoryIndex[id] - if (typeof category !== 'undefined'){ + const category = categoryIndex[id]; + if (typeof category !== 'undefined') { return category } else { - throw new StoreError(404, 'Category not found') + throw new StoreError(404, 'Category not found'); } } const getTemplate = (id) => { validateId(id) - const res = templateIndex[id] - if (typeof res !== 'undefined'){ + const res = templateIndex[id]; + if (typeof res !== 'undefined') { return res } else { - throw new StoreError(404, 'Template not found') + throw new StoreError(404, 'Template not found'); } } const createCategory = (title, locked) => { - validateTitle(title) + validateTitle(title); - const id = genId() + const id = genId(); const category = { id, title, @@ -135,80 +135,81 @@ const create = (data = []) => { items: [] } categoryIndex[id] = category; - store.items = [...store.items, category] + storeItems.items = [...storeItems.items, category]; return category } const createTemplate = (title, content, categoryId) => { - validateTitle(title) - validate(content, isNonEmptyString, `Invalid "content" parameter: ${content}`) - const id = genId() + validateTitle(title); + validate(content, isNonEmptyString, `Invalid "content" parameter: ${content}`); + const id = genId(); const template = { id, title, content, } - const parent = getParent(categoryId) - parent.items = [...parent.items, template] + const parent = getParent(categoryId); + parent.items = [...parent.items, template]; templateIndex[id] = [template, categoryId]; return template } const renameCategory = (id, title) => { - validateTitle(title) - const category = getCategory(id) + validateTitle(title); + const category = getCategory(id); category.title = title; } const renameTemplate = (id, title) => { - validateTitle(title) - const [template] = getTemplate(id) + validateTitle(title); + const [template] = getTemplate(id); template.title = title; } const deleteTemplate = (id) => { - const [, categoryId] = getTemplate(id) - const parent = getParent(categoryId) - parent.items = filterOut(parent.items, id) - delete templateIndex[id] + const [, categoryId] = getTemplate(id); + const parent = getParent(categoryId); + parent.items = filterOut(parent.items, id); + delete templateIndex[id]; } const deleteCategory = (id) => { const category = getCategory(id); - category.items.forEach((template) => deleteTemplate(template.id)) - store.items = filterOut(store.items, id) - delete categoryIndex[category.id] + category.items.forEach((template) => deleteTemplate(template.id)); + store.items = filterOut(storeItems.items, id); + delete categoryIndex[category.id]; } const moveTemplate = (id, newCategoryId) => { - const [template, categoryId] = getTemplate(id) + const [template, categoryId] = getTemplate(id); if (categoryId === newCategoryId) { return } - const oldParent = getParent(categoryId) - const newParent = getParent(newCategoryId) - newParent.items = [...newParent.items, template] - oldParent.items = filterOut(oldParent.items, id) - templateIndex[id] = [template, newCategoryId] + const oldParent = getParent(categoryId); + const newParent = getParent(newCategoryId); + newParent.items = [...newParent.items, template]; + oldParent.items = filterOut(oldParent.items, id); + templateIndex[id] = [template, newCategoryId]; } const moveCategoryItems = (id, newCategoryId) => { - const oldCategory = getCategory(id) + const oldCategory = getCategory(id); if (id === newCategoryId) { return } - oldCategory.items.forEach((item) => moveTemplate(item.id, newCategoryId)) + oldCategory.items.forEach((item) => moveTemplate(item.id, newCategoryId)); } const load = (items, id) => { items.forEach((item) => { if (item.items) { const category = createCategory(item.title, item.locked); - load(item.items, category.id ); + load(item.items, category.id); } else { createTemplate(item.title, item.content, id); } }); + }; load(data); @@ -228,119 +229,117 @@ const create = (data = []) => { const store = create(data); -const advtemplate_list = async () => store.list() +const advtemplate_list = async () => store.list(); const advtemplate_create_category = async (title) => { try { - return store.createCategory(title) + return store.createCategory(title); } catch (e) { - console.error(e) - throw new Error('Failed to create category') + console.error(e); + throw new Error('Failed to create category'); } } - + const advtemplate_create_template = async (title, content, categoryId) => { try { - return store.createTemplate(title, content, categoryId) + return store.createTemplate(title, content, categoryId); } catch (e) { - console.error(e) - throw new Error('Failed to create template') + console.error(e); + throw new Error('Failed to create template'); } } - + const advtemplate_get_template = async (id) => { try { - const [template ] = store.getTemplate(id) + const [template] = store.getTemplate(id); return template } catch (e) { - console.error(e) + console.error(e); + throw new Error('Failed to get template'); } } const advtemplate_rename_category = async (id, title) => { try { - store.renameCategory(id, title) + store.renameCategory(id, title); return {} } catch (e) { - console.error(e) - throw new Error('Failed to rename category') + console.error(e); + throw new Error('Failed to rename category'); } } - + const advtemplate_rename_template = async (id, title) => { try { - store.renameTemplate(id, title) + store.renameTemplate(id, title); return {} } catch (e) { - console.error(e) - throw new Error('Failed to rename template') + console.error(e); + throw new Error('Failed to rename template'); } } const advtemplate_delete_category = async (id) => { try { - store.deleteCategory(id) + store.deleteCategory(id); return {} } catch (e) { - console.error(e) - throw new Error('Failed to delete category') + console.error(e); + throw new Error('Failed to delete category'); } } const advtemplate_delete_template = async (id) => { try { - store.deleteTemplate(id) + store.deleteTemplate(id); return {} } catch (e) { - console.error(e) - throw new Error('Failed to delete template') + console.error(e); + throw new Error('Failed to delete template'); } } const advtemplate_move_template = async (id, categoryId) => { try { - store.moveTemplate(id, categoryId) + store.moveTemplate(id, categoryId); return {} } catch (e) { - console.error(e) - throw new Error('Failed to move template') + console.error(e); + throw new Error('Failed to move template'); } } const advtemplate_move_category_items = async (id, categoryId) => { try { - store.moveCategoryItems(id, categoryId) + store.moveCategoryItems(id, categoryId); return {} } catch (e) { - console.error(e) - throw new Error('Failed to move category items') + console.error(e); + throw new Error('Failed to move category items'); } } tinymce.init({ selector: "textarea#readonly-locked-template", - plugins: [ - "advlist", "anchor", "autolink", "charmap", "code", "fullscreen", - "help", "image", "insertdatetime", "link", "lists", "media", - "preview", "searchreplace", "table", "visualblocks", "advtemplate" - ], + plugins: [ + "advlist", "anchor", "autolink", "charmap", "code", "fullscreen", + "help", "image", "insertdatetime", "link", "lists", "media", + "preview", "searchreplace", "table", "visualblocks", "advtemplate" + ], contextmenu: 'advtemplate', toolbar: "addtemplate inserttemplate | undo redo | styles | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image", advtemplate_list, advtemplate_get_template, - advtemplate_create_category, advtemplate_create_template, - advtemplate_rename_category, advtemplate_move_category_items, advtemplate_delete_category, - advtemplate_rename_template, advtemplate_move_template, advtemplate_delete_template, -}); \ No newline at end of file +});