CoreFiles by mySites.guru
joomla-field-subform-es5.js673 lines, 27,400 bytes, from Joomla_4.0.0-alpha5-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha3
md5
933d0a24c7934331e20202f7dc1f7d3f
sha1
61532a8eb52abd9acf1165770a70afb381273010
sha256
37d5e394c3e687170526894b66b3929ebf79f7e32e15af6fbbf79c654d0939c9
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/**
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

;(function (customElements) {
    "use strict";

    var KEYCODE = {
        SPACE: 32,
        ESC: 27,
        ENTER: 13
    };

    // Find matchesFn with vendor prefix
    var matchesFn = 'matches';
    ['matches', 'msMatchesSelector'].some(function (fn) {
        if (typeof document.body[fn] === 'function') {
            matchesFn = fn;
            return true;
        }
        return false;
    });

    var JoomlaFieldSubform = function (_HTMLElement) {
        _inherits(JoomlaFieldSubform, _HTMLElement);

        _createClass(JoomlaFieldSubform, [{
            key: 'buttonAdd',


            // Attribute getters
            get: function get() {
                return this.getAttribute('button-add');
            }
        }, {
            key: 'buttonRemove',
            get: function get() {
                return this.getAttribute('button-remove');
            }
        }, {
            key: 'buttonMove',
            get: function get() {
                return this.getAttribute('button-move');
            }
        }, {
            key: 'rowsContainer',
            get: function get() {
                return this.getAttribute('rows-container');
            }
        }, {
            key: 'repeatableElement',
            get: function get() {
                return this.getAttribute('repeatable-element');
            }
        }, {
            key: 'minimum',
            get: function get() {
                return this.getAttribute('minimum');
            }
        }, {
            key: 'maximum',
            get: function get() {
                return this.getAttribute('maximum');
            }
        }, {
            key: 'name',
            get: function get() {
                return this.getAttribute('name');
            },
            set: function set(value) {
                // Update the template
                this.template = this.template.replace(new RegExp(' name="' + this.name.replace(/[\[\]]/g, '\\$&'), 'g'), ' name="' + value);

                return this.setAttribute('name', value);
            }
        }]);

        function JoomlaFieldSubform() {
            _classCallCheck(this, JoomlaFieldSubform);

            var _this = _possibleConstructorReturn(this, (JoomlaFieldSubform.__proto__ || Object.getPrototypeOf(JoomlaFieldSubform)).call(this));

            var that = _this;

            // Get the rows container
            _this.containerWithRows = _this;

            if (_this.rowsContainer) {
                var allContainers = _this.querySelectorAll(_this.rowsContainer);

                // Find closest, and exclude nested
                for (var i = 0, l = allContainers.length; i < l; i++) {
                    if (closest(allContainers[i], 'joomla-field-subform') === _this) {
                        _this.containerWithRows = allContainers[i];
                        break;
                    }
                }
            }

            // Last row number, help to avoid the name duplications
            _this.lastRowNum = _this.getRows().length;

            // Template for the repeating group
            _this.template = '';

            // Prepare a row template, and find available field names
            _this.prepareTemplate();

            // Bind buttons
            if (_this.buttonAdd || _this.buttonRemove) {
                _this.addEventListener('click', function (event) {
                    var btnAdd = null,
                        btnRem = null;

                    if (that.buttonAdd) {
                        btnAdd = event.target[matchesFn](that.buttonAdd) ? event.target : closest(event.target, that.buttonAdd);
                    }

                    if (that.buttonRemove) {
                        btnRem = event.target[matchesFn](that.buttonRemove) ? event.target : closest(event.target, that.buttonRemove);
                    }

                    // Check actine, with extra check for nested joomla-field-subform
                    if (btnAdd && closest(btnAdd, 'joomla-field-subform') === that) {
                        var row = closest(btnAdd, that.repeatableElement);
                        row = closest(row, 'joomla-field-subform') === that ? row : null;
                        that.addRow(row);
                        event.preventDefault();
                    } else if (btnRem && closest(btnRem, 'joomla-field-subform') === that) {
                        var _row = closest(btnRem, that.repeatableElement);
                        that.removeRow(_row);
                        event.preventDefault();
                    }
                });

                _this.addEventListener('keydown', function (event) {
                    if (event.keyCode !== KEYCODE.SPACE) return;
                    var isAdd = that.buttonAdd && event.target[matchesFn](that.buttonAdd);
                    var isRem = that.buttonRemove && event.target[matchesFn](that.buttonRemove);

                    if ((isAdd || isRem) && closest(event.target, 'joomla-field-subform') === that) {
                        var row = closest(event.target, that.repeatableElement);
                        row = closest(row, 'joomla-field-subform') === that ? row : null;
                        if (isRem && row) {
                            that.removeRow(row);
                        } else if (isAdd) {
                            that.addRow(row);
                        }
                        event.preventDefault();
                    }
                });
            }

            // Sorting
            if (_this.buttonMove) {
                _this.setUpDragSort();
            }
            return _this;
        }

        /**
         * Search for existing rows
         * @returns {HTMLElement[]}
         */


        _createClass(JoomlaFieldSubform, [{
            key: 'getRows',
            value: function getRows() {
                var rows = this.containerWithRows.children,
                    matchesFn = document.body['msMatchesSelector'] ? 'msMatchesSelector' : 'matches',
                    result = [];

                // Filter out the rows
                for (var i = 0, l = rows.length; i < l; i++) {
                    if (rows[i][matchesFn](this.repeatableElement)) {
                        result.push(rows[i]);
                    }
                }

                return result;
            }

            /**
             * Prepare a row template
             */

        }, {
            key: 'prepareTemplate',
            value: function prepareTemplate() {
                var tmplElement = [].slice.call(this.children).filter(function (el) {
                    return el.classList.contains('subform-repeatable-template-section');
                });

                if (tmplElement[0]) {
                    this.template = tmplElement[0].innerHTML;
                }

                if (!this.template) {
                    throw new Error('The row template are required to subform element to work');
                }
            }

            /**
             * Add new row
             * @param {HTMLElement} after
             * @returns {HTMLElement}
             */

        }, {
            key: 'addRow',
            value: function addRow(after) {
                // Count how much we already have
                var count = this.getRows().length;
                if (count >= this.maximum) {
                    return null;
                }

                // Make a new row from the template
                var tmpEl = void 0;
                if (this.containerWithRows.nodeName === 'TBODY' || this.containerWithRows.nodeName === 'TABLE') {
                    tmpEl = document.createElement('tbody');
                } else {
                    tmpEl = document.createElement('div');
                }
                tmpEl.innerHTML = this.template;
                var row = tmpEl.children[0];

                // Add to container
                if (after) {
                    after.parentNode.insertBefore(row, after.nextSibling);
                } else {
                    this.containerWithRows.append(row);
                }

                // Add dragable attributes
                if (this.buttonMove) {
                    row.setAttribute('draggable', 'false');
                    row.setAttribute('aria-grabbed', 'false');
                    row.setAttribute('tabindex', '0');
                }

                // Marker that it is new
                row.setAttribute('data-new', '1');
                // Fix names and ids, and reset values
                this.fixUniqueAttributes(row, count);

                // Tell about the new row
                this.dispatchEvent(new CustomEvent('subform-row-add', {
                    detail: { row: row },
                    bubbles: true
                }));

                if (window.Joomla) {
                    Joomla.Event.dispatch(row, 'joomla:updated');
                }

                return row;
            }

            /**
             * Remove the row
             * @param {HTMLElement} row
             */

        }, {
            key: 'removeRow',
            value: function removeRow(row) {
                // Count how much we have
                var count = this.getRows().length;
                if (count <= this.minimum) {
                    return;
                }

                // Tell about the row will be removed
                this.dispatchEvent(new CustomEvent('subform-row-remove', {
                    detail: { row: row },
                    bubbles: true
                }));

                if (window.Joomla) {
                    Joomla.Event.dispatch(row, 'joomla:removed');
                }

                row.parentNode.removeChild(row);
            }

            /**
             * Fix names ind id`s for field that in the row
             * @param {HTMLElement} row
             * @param {Number} count
             */

        }, {
            key: 'fixUniqueAttributes',
            value: function fixUniqueAttributes(row, count) {
                var _this2 = this;

                this.lastRowNum++;
                count = count || 0;

                var group = row.getAttribute('data-group'),
                    // current group name
                basename = row.getAttribute('data-base-name'),
                    // group base name, without count
                countnew = Math.max(this.lastRowNum, count + 1),
                    groupnew = basename + countnew; // new group name

                this.lastRowNum = countnew;
                row.setAttribute('data-group', groupnew);

                // Fix inputs that have a "name" attribute
                var haveName = row.querySelectorAll('[name]'),
                    ids = {}; // Collect id for fix checkboxes and radio

                // Filter out nested
                haveName = [].slice.call(haveName).filter(function (el) {
                    return closest(el, 'joomla-field-subform') === _this2;
                });

                for (var i = 0, l = haveName.length; i < l; i++) {
                    var $el = haveName[i],
                        name = $el.getAttribute('name'),
                        id = name.replace(/(\[\]$)/g, '').replace(/(\]\[)/g, '__').replace(/\[/g, '_').replace(/\]/g, ''),
                        // id from name
                    nameNew = name.replace('[' + group + '][', '[' + groupnew + ']['),
                        // New name
                    idNew = id.replace(group, groupnew),
                        // Count new id
                    countMulti = 0,
                        // count for multiple radio/checkboxes
                    forOldAttr = id; // Fix "for" in the labels

                    if ($el.type === 'checkbox' && name.match(/\[\]$/)) {
                        // <input type="checkbox" name="name[]"> fix
                        // Recount id
                        countMulti = ids[id] ? ids[id].length : 0;
                        if (!countMulti) {
                            // Set the id for fieldset and group label
                            var fieldset = closest($el, 'fieldset.checkboxes'),
                                elLbl = row.querySelector('label[for="' + id + '"]');

                            if (fieldset) {
                                fieldset.setAttribute('id', idNew);
                            }

                            if (elLbl) {
                                elLbl.setAttribute('for', idNew);
                                elLbl.setAttribute('id', idNew + '-lbl');
                            }
                        }
                        forOldAttr = forOldAttr + countMulti;
                        idNew = idNew + countMulti;
                    } else if ($el.type === 'radio') {
                        // <input type="radio"> fix
                        // Recount id
                        countMulti = ids[id] ? ids[id].length : 0;
                        if (!countMulti) {
                            // Set the id for fieldset and group label
                            var _fieldset = closest($el, 'fieldset.radio'),
                                _elLbl = row.querySelector('label[for="' + id + '"]');

                            if (_fieldset) {
                                _fieldset.setAttribute('id', idNew);
                            }

                            if (_elLbl) {
                                _elLbl.setAttribute('for', idNew);
                                _elLbl.setAttribute('id', idNew + '-lbl');
                            }
                        }
                        forOldAttr = forOldAttr + countMulti;
                        idNew = idNew + countMulti;
                    }

                    // Cache already used id
                    if (ids[id]) {
                        ids[id].push(true);
                    } else {
                        ids[id] = [true];
                    }

                    // Replace the name to new one
                    $el.name = nameNew;
                    if ($el.id) {
                        $el.id = idNew;
                    }

                    // Guess there a label for this input
                    var lbl = row.querySelector('label[for="' + forOldAttr + '"]');
                    if (lbl) {
                        lbl.setAttribute('for', idNew);
                        lbl.setAttribute('id', idNew + '-lbl');
                    }
                }
            }

            /**
             * Use of HTML Drag and Drop API
             * https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
             * https://www.sitepoint.com/accessible-drag-drop/
             */

        }, {
            key: 'setUpDragSort',
            value: function setUpDragSort() {
                var that = this; // Self reference
                var item = null; // Storing the selected item
                var touched = false; // We have a touch events

                // Find all existing rows and add dragable attributes
                var rows = this.getRows();
                for (var ir = 0, lr = rows.length; ir < lr; ir++) {
                    var childRow = rows[ir];

                    childRow.setAttribute('draggable', 'false');
                    childRow.setAttribute('aria-grabbed', 'false');
                    childRow.setAttribute('tabindex', '0');
                }

                // Helper method to test whether Handler was clicked
                function getMoveHandler(element) {
                    return !element.form // This need to test whether the element is :input
                    && element[matchesFn](that.buttonMove) ? element : closest(element, that.buttonMove);
                }

                // Helper method to mover row to selected position
                function switchRowPositions(src, dest) {
                    var isRowBefore = false;
                    if (src.parentNode === dest.parentNode) {
                        for (var cur = src; cur; cur = cur.previousSibling) {
                            if (cur === dest) {
                                isRowBefore = true;
                                break;
                            }
                        }
                    }

                    if (isRowBefore) {
                        dest.parentNode.insertBefore(src, dest);
                    } else {
                        dest.parentNode.insertBefore(src, dest.nextSibling);
                    }
                }

                // Touch interaction:
                // - a touch of "move button" mark a row dragable / "selected", or deselect previous selected
                // - a touch of "move button" in the destination row will move a selected row to a new position
                this.addEventListener('touchstart', function (event) {
                    touched = true;

                    // Check for .move button
                    var handler = getMoveHandler(event.target),
                        row = handler ? closest(handler, that.repeatableElement) : null;

                    if (!row || closest(row, 'joomla-field-subform') !== that) {
                        return;
                    }

                    // First selection
                    if (!item) {
                        row.setAttribute('draggable', 'true');
                        row.setAttribute('aria-grabbed', 'true');
                        item = row;
                    }
                    // Second selection
                    else {
                            // Move to selected position
                            if (row !== item) {
                                switchRowPositions(item, row);
                            }

                            item.setAttribute('draggable', 'false');
                            item.setAttribute('aria-grabbed', 'false');
                            item = null;
                        }

                    event.preventDefault();
                });

                // Mouse interaction
                // - mouse down, enable "draggable" and allow to drag the row,
                // - mouse up, disable "draggable"
                this.addEventListener('mousedown', function (event) {
                    if (touched) return;

                    // Check for .move button
                    var handler = getMoveHandler(event.target),
                        row = handler ? closest(handler, that.repeatableElement) : null;

                    if (!row || closest(row, 'joomla-field-subform') !== that) {
                        return;
                    }

                    row.setAttribute('draggable', 'true');
                    row.setAttribute('aria-grabbed', 'true');
                    item = row;
                });

                this.addEventListener('mouseup', function (event) {
                    if (item && !touched) {
                        item.setAttribute('draggable', 'false');
                        item.setAttribute('aria-grabbed', 'false');
                        item = null;
                    }
                });

                // Keyboard interaction
                // - "tab" to navigate to needed row,
                // - modifier (ctr,alt,shift) + "space" select the row,
                // - "tab" to select destination,
                // - "enter" to place selected row in to destination
                // - "esc" to cancel selection
                this.addEventListener('keydown', function (event) {
                    if (event.keyCode !== KEYCODE.ESC && event.keyCode !== KEYCODE.SPACE && event.keyCode !== KEYCODE.ENTER || event.target.form || !event.target[matchesFn](that.repeatableElement)) {
                        return;
                    }

                    var row = event.target;

                    // Make sure we handle correct children
                    if (!row || closest(row, 'joomla-field-subform') !== that) {
                        return;
                    }

                    // Space is the selection or unselection keystroke
                    if (event.keyCode === KEYCODE.SPACE && hasModifier(event)) {
                        // Unselect previously selected
                        if (row.getAttribute('aria-grabbed') === 'true') {
                            row.setAttribute('draggable', 'false');
                            row.setAttribute('aria-grabbed', 'false');
                            item = null;
                        }
                        // Select new
                        else {
                                // If there was previously selected
                                if (item) {
                                    item.setAttribute('draggable', 'false');
                                    item.setAttribute('aria-grabbed', 'false');
                                    item = null;
                                }

                                // Mark new selection
                                row.setAttribute('draggable', 'true');
                                row.setAttribute('aria-grabbed', 'true');
                                item = row;
                            }

                        // Prevent default to suppress any native actions
                        event.preventDefault();
                    }

                    // Escape is the abort keystroke (for any target element)
                    if (event.keyCode === KEYCODE.ESC && item) {
                        item.setAttribute('draggable', 'false');
                        item.setAttribute('aria-grabbed', 'false');
                        item = null;
                    }

                    // Enter, to place selected item in selected position
                    if (event.keyCode === KEYCODE.ENTER && item) {
                        item.setAttribute('draggable', 'false');
                        item.setAttribute('aria-grabbed', 'false');

                        // Do nothing here
                        if (row === item) {
                            item = null;
                            return;
                        }

                        // Move the item to selected position
                        switchRowPositions(item, row);

                        event.preventDefault();
                        item = null;
                    }
                });

                // dragstart event to initiate mouse dragging
                this.addEventListener('dragstart', function (event) {
                    if (item) {
                        // We going to move the row
                        event.dataTransfer.effectAllowed = 'move';

                        // This need to work in Firefox and IE10+
                        event.dataTransfer.setData('text', '');
                    }
                });

                this.addEventListener('dragover', function (event) {
                    if (item) {
                        event.preventDefault();
                    }
                });

                // Handle drag action, move element to hovered position
                this.addEventListener('dragenter', function (event) {
                    // Make sure the target in the correct container
                    if (!item || that.rowsContainer && closest(event.target, that.rowsContainer) !== that.containerWithRows) {
                        return;
                    }

                    // Find a hovered row, and replace it
                    var row = event.target[matchesFn](that.repeatableElement) ? event.target : closest(event.target, that.repeatableElement);
                    if (!row) return;

                    switchRowPositions(item, row);
                });

                // dragend event to clean-up after drop or abort
                // which fires whether or not the drop target was valid
                this.addEventListener('dragend', function () {
                    if (item) {
                        item.setAttribute('draggable', 'false');
                        item.setAttribute('aria-grabbed', 'false');
                        item = null;
                    }
                });
            }
        }]);

        return JoomlaFieldSubform;
    }(HTMLElement);

    customElements.define('joomla-field-subform', JoomlaFieldSubform);

    /**
     * Helper to find a closest parent element
     *
     * @param {HTMLElement} element
     * @param {String}      selector
     *
     * @returns {HTMLElement|null}
     */
    function closest(element, selector) {
        var parent = void 0;

        // Traverse parents
        while (element) {
            parent = element.parentElement;
            if (parent && parent[matchesFn](selector)) {
                return parent;
            }
            element = parent;
        }

        return null;
    }

    /**
     * Helper for testing whether a selection modifier is pressed
     * @param {Event} event
     *
     * @returns {boolean|*}
     */
    function hasModifier(event) {
        return event.ctrlKey || event.metaKey || event.shiftKey;
    }
})(customElements);

},{}]},{},[1]);

No other release has this exact content. See every version of this file.