diff --git a/FAQ/sections/faq.liquid b/FAQ/sections/faq.liquid new file mode 100644 index 0000000..3e310f6 --- /dev/null +++ b/FAQ/sections/faq.liquid @@ -0,0 +1,390 @@ + +{%- comment -%} ---------------- THE CSS ---------------- {%- endcomment -%} + + + + +{%- comment -%} ---------------- THE MARKUP ---------------- {%- endcomment -%} + +
+ {%- for block in section.blocks -%} + {%- if block.settings.title != blank and block.settings.content != blank -%} + + {%- if block.settings.checkbox_expanded == true -%} + {%- assign expanded = 'true' -%} + {%- assign hidden = '' -%} + {%- else -%} + {%- assign expanded = 'false' -%} + {%- assign hidden = 'hidden' -%} + {%- endif -%} + +

+ +

+ + + + {%- endif -%} + {%- endfor -%} +
+ + +{%- comment -%} -------------- THE RICH SCHEMA ------------- {%- endcomment -%} + + + + +{%- comment -%} ---------------- THE CONFIG ---------------- {%- endcomment -%} + + + + +{%- comment -%} ---------------- THE SETTINGS ---------------- {%- endcomment -%} + +{% schema %} +{ + "name": "FAQ", + "class": "sd-faq", + "tag": "section", + + "settings": [ + { + "type": "color", + "id": "border_color", + "label": "Border color", + "default": "#eeeeee" + }, + { + "type": "color", + "id": "panel_color", + "label": "Panel color" + } + ], + + "blocks": [ + { + "type": "faq", + "name": "FAQ", + "settings": [ + { + "type": "checkbox", + "id": "checkbox_expanded", + "default": false, + "label": "Expanded?" + }, + { + "type": "text", + "id": "title", + "label": "FAQ title", + "default": "FAQ title" + }, + { + "type": "richtext", + "id": "content", + "label": "FAQ content", + "default": "

FAQ content

" + } + ] + } + ], + + "presets": [ + { + "name": "FAQ" + } + ] +} +{% endschema %} + +{%- comment -%} ------------------ THE JS ----------------- {%- endcomment -%} + +{% javascript %} + +(function FAQ(SD) { + 'use strict'; + + var support = getSupport(); + var sectionsIds = getSectionIds('data-faq-config'); + var sectionInit = compose(publicAPI, setEvents, getBlocks, getConfig); + var sections = sectionsIds.map(sectionInit); + + SD.faq = zipObj(sectionsIds, sections); + + function publicAPI(config) { + return { + id: config.sectionId, + config: config, + blocks: zipObj(config.blockIds, config.blocks), + init: sectionInit + } + } + + //************************** + + + function blockEvents(block) { + block.trigger.addEventListener('click', function triggerClick() { toggle(block) }); + } + + function toggle(block) { + block.collapsed ? expand(block) : collapse(block); + } + + function expand(block) { + block.button.setAttribute('aria-expanded', true); + block.panel.removeAttribute('hidden'); + animate(block.panel, 'normal'); + return block; + } + + function collapse(block) { + block.button.setAttribute('aria-expanded', false); + block.panel.setAttribute('hidden', ''); + animate(block.panel, 'reverse'); + return block; + } + + function isCollapsed(block) { + return (attr('aria-expanded', block.button) === 'false'); + } + + function animate(element, direction) { + if (!support.WebAnimations) return; + + element.setAttribute('data-is-animating', true); + + element.animate([ + { height: 0 }, + { height: element.offsetHeight + 'px' }], + + { duration: 240, + fill: 'both', + easing: 'cubic-bezier(0.4, 0.0, 0.2, 1)', + direction: direction + } + + ).onfinish = function() { + element.removeAttribute('data-is-animating'); + this.cancel(); + }; + } + + //************************** + + + function getBlocks(config) { + config.blocks = config.blockIds.map(block); + return config; + } + + function block(blockId) { + return { + trigger: document.querySelector('[data-faq-trigger="' + blockId + '"]'), + button: document.querySelector('[data-faq-button="' + blockId + '"]'), + panel: document.querySelector('[data-faq-panel="' + blockId + '"]'), + + get collapsed() { return isCollapsed(this) }, + select: function select() { return expand(this) }, + deselect: function deselect() { return collapse(this) } + } + } + + function setEvents(config) { + config.blocks.forEach(blockEvents); + return config; + } + + function getSectionIds(sectionAttr) { + var sectionConfigAttr = ['[',sectionAttr,']'].join(''); + + return nodeList(sectionConfigAttr).map( + function sectionId(element) { + return element.getAttribute(sectionAttr); + } + ); + } + + function getConfig(sectionId) { + return JSON.parse(document.querySelector('[data-faq-config="' + sectionId + '"]').innerHTML); + } + + //************************** + + + function nodeList(str, root) { + if (!root) root = document; + var nodeList = root.querySelectorAll(str); + return Array.prototype.slice.call(nodeList); + } + + function attr(str, element) { + return element.getAttribute(str); + } + + function zipObj(keys, values) { + return keys.reduce( + function zipObj(acc, key, idx) { + acc[key] = values[idx]; + return acc; + }, {} + ) + } + + function compose() { + var funcs = Array.prototype.slice.call(arguments).reverse(); + return function() { + return funcs.slice(1).reduce(function(res, fn) { + return fn(res); + }, funcs[0].apply(undefined, arguments)); + }; + } + + function getSupport() { + return { + WebAnimations: (typeof Element.prototype.animate === 'function') + } + } + +})(window.SectionsDesign = window.SectionsDesign || {}) + +{% endjavascript %} + + +{%- comment -%} ---------------- THE NO-JS ---------------- {%- endcomment -%} + + + + +{%- comment -%} ---------------- THE EDITOR ------------------ {%- endcomment -%} + +{%- if section.blocks[0].shopify_attributes != blank -%} + +{%- endif -%}