|
| 1 | +function _typeof(obj) { |
| 2 | + if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { |
| 3 | + _typeof = function (obj) { |
| 4 | + return typeof obj; |
| 5 | + }; |
| 6 | + } else { |
| 7 | + _typeof = function (obj) { |
| 8 | + return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; |
| 9 | + }; |
| 10 | + } |
| 11 | + |
| 12 | + return _typeof(obj); |
| 13 | +} |
| 14 | + |
| 15 | +function _defineProperty(obj, key, value) { |
| 16 | + if (key in obj) { |
| 17 | + Object.defineProperty(obj, key, { |
| 18 | + value: value, |
| 19 | + enumerable: true, |
| 20 | + configurable: true, |
| 21 | + writable: true |
| 22 | + }); |
| 23 | + } else { |
| 24 | + obj[key] = value; |
| 25 | + } |
| 26 | + |
| 27 | + return obj; |
| 28 | +} |
| 29 | + |
| 30 | +function _slicedToArray(arr, i) { |
| 31 | + return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); |
| 32 | +} |
| 33 | + |
| 34 | +function _arrayWithHoles(arr) { |
| 35 | + if (Array.isArray(arr)) return arr; |
| 36 | +} |
| 37 | + |
| 38 | +function _iterableToArrayLimit(arr, i) { |
| 39 | + if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + var _arr = []; |
| 44 | + var _n = true; |
| 45 | + var _d = false; |
| 46 | + var _e = undefined; |
| 47 | + |
| 48 | + try { |
| 49 | + for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { |
| 50 | + _arr.push(_s.value); |
| 51 | + |
| 52 | + if (i && _arr.length === i) break; |
| 53 | + } |
| 54 | + } catch (err) { |
| 55 | + _d = true; |
| 56 | + _e = err; |
| 57 | + } finally { |
| 58 | + try { |
| 59 | + if (!_n && _i["return"] != null) _i["return"](); |
| 60 | + } finally { |
| 61 | + if (_d) throw _e; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return _arr; |
| 66 | +} |
| 67 | + |
| 68 | +function _nonIterableRest() { |
| 69 | + throw new TypeError("Invalid attempt to destructure non-iterable instance"); |
| 70 | +} |
| 71 | + |
| 72 | +function arrayToObject() { |
| 73 | + var fields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; |
| 74 | + return fields.reduce(function (prev, path) { |
| 75 | + var key = path.split(".").slice(-1)[0]; |
| 76 | + |
| 77 | + if (prev[key]) { |
| 78 | + throw new Error("The key `".concat(key, "` is already in use.")); |
| 79 | + } // eslint-disable-next-line no-param-reassign |
| 80 | + |
| 81 | + |
| 82 | + prev[key] = path; |
| 83 | + return prev; |
| 84 | + }, {}); |
| 85 | +} |
| 86 | + |
| 87 | +function normalizeNamespace(fn) { |
| 88 | + return function () { |
| 89 | + for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) { |
| 90 | + params[_key] = arguments[_key]; |
| 91 | + } |
| 92 | + |
| 93 | + // eslint-disable-next-line prefer-const |
| 94 | + var _ref = typeof params[0] === "string" ? [].concat(params) : [""].concat(params), |
| 95 | + _ref2 = _slicedToArray(_ref, 4), |
| 96 | + namespace = _ref2[0], |
| 97 | + map = _ref2[1], |
| 98 | + getterType = _ref2[2], |
| 99 | + mutationType = _ref2[3]; |
| 100 | + |
| 101 | + if (namespace.length && namespace.charAt(namespace.length - 1) !== "/") { |
| 102 | + namespace += "/"; |
| 103 | + } |
| 104 | + |
| 105 | + getterType = "".concat(namespace).concat(getterType || "getField"); |
| 106 | + mutationType = "".concat(namespace).concat(mutationType || "updateField"); |
| 107 | + return fn(namespace, map, getterType, mutationType); |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +function getField(state) { |
| 112 | + return function (path) { |
| 113 | + return path.split(/[.[\]]+/).reduce(function (prev, key) { |
| 114 | + return prev[key]; |
| 115 | + }, state); |
| 116 | + }; |
| 117 | +} |
| 118 | +function updateField(state, _ref3) { |
| 119 | + var path = _ref3.path, |
| 120 | + value = _ref3.value; |
| 121 | + path.split(/[.[\]]+/).reduce(function (prev, key, index, array) { |
| 122 | + if (array.length === index + 1) { |
| 123 | + // eslint-disable-next-line no-param-reassign |
| 124 | + prev[key] = value; |
| 125 | + } |
| 126 | + |
| 127 | + return prev[key]; |
| 128 | + }, state); |
| 129 | +} |
| 130 | +var mapFields = normalizeNamespace(function (namespace, fields, getterType, mutationType) { |
| 131 | + var fieldsObject = Array.isArray(fields) ? arrayToObject(fields) : fields; |
| 132 | + return Object.keys(fieldsObject).reduce(function (prev, key) { |
| 133 | + var path = fieldsObject[key]; |
| 134 | + var field = { |
| 135 | + get: function get() { |
| 136 | + return this.$store.getters[getterType](path); |
| 137 | + }, |
| 138 | + set: function set(value) { |
| 139 | + this.$store.commit(mutationType, { |
| 140 | + path: path, |
| 141 | + value: value |
| 142 | + }); |
| 143 | + } |
| 144 | + }; // eslint-disable-next-line no-param-reassign |
| 145 | + |
| 146 | + prev[key] = field; |
| 147 | + return prev; |
| 148 | + }, {}); |
| 149 | +}); |
| 150 | +var mapMultiRowFields = normalizeNamespace(function (namespace, paths, getterType, mutationType) { |
| 151 | + var pathsObject = Array.isArray(paths) ? arrayToObject(paths) : paths; |
| 152 | + return Object.keys(pathsObject).reduce(function (entries, key) { |
| 153 | + var path = pathsObject[key]; // eslint-disable-next-line no-param-reassign |
| 154 | + |
| 155 | + entries[key] = { |
| 156 | + get: function get() { |
| 157 | + var store = this.$store; |
| 158 | + var rows = store.getters[getterType](path); |
| 159 | + |
| 160 | + var defineProperties = function defineProperties(fieldsObject, index, path) { |
| 161 | + return Object.keys(fieldsObject).reduce(function (prev, fieldKey) { |
| 162 | + var fieldPath = index !== false ? "".concat(path, "[").concat(index, "].").concat(fieldKey) : "".concat(path, ".").concat(fieldKey); |
| 163 | + return Object.defineProperty(prev, fieldKey, { |
| 164 | + get: function get() { |
| 165 | + if (_typeof(fieldsObject[fieldKey]) === 'object' && fieldsObject[fieldKey] !== null) { |
| 166 | + return defineProperties(fieldsObject[fieldKey], false, fieldPath); |
| 167 | + } |
| 168 | + |
| 169 | + return store.getters[getterType](fieldPath); |
| 170 | + }, |
| 171 | + set: function set(value) { |
| 172 | + store.commit(mutationType, { |
| 173 | + path: fieldPath, |
| 174 | + value: value |
| 175 | + }); |
| 176 | + } |
| 177 | + }); |
| 178 | + }, {}); |
| 179 | + }; |
| 180 | + |
| 181 | + return rows.map(function (fieldsObject, index) { |
| 182 | + return defineProperties(fieldsObject, index, path); |
| 183 | + }); |
| 184 | + } |
| 185 | + }; |
| 186 | + return entries; |
| 187 | + }, {}); |
| 188 | +}); |
| 189 | +var createHelpers = function createHelpers(_ref4) { |
| 190 | + var _ref5; |
| 191 | + |
| 192 | + var getterType = _ref4.getterType, |
| 193 | + mutationType = _ref4.mutationType; |
| 194 | + return _ref5 = {}, _defineProperty(_ref5, getterType, getField), _defineProperty(_ref5, mutationType, updateField), _defineProperty(_ref5, "mapFields", normalizeNamespace(function (namespace, fields) { |
| 195 | + return mapFields(namespace, fields, getterType, mutationType); |
| 196 | + })), _defineProperty(_ref5, "mapMultiRowFields", normalizeNamespace(function (namespace, paths) { |
| 197 | + return mapMultiRowFields(namespace, paths, getterType, mutationType); |
| 198 | + })), _ref5; |
| 199 | +}; |
| 200 | + |
| 201 | +export { createHelpers, getField, mapFields, mapMultiRowFields, updateField }; |
0 commit comments