X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=skyquake%2Fplugins%2Fcomposer%2Fsrc%2Fsrc%2Flibraries%2Futils.js;fp=skyquake%2Fplugins%2Fcomposer%2Fsrc%2Fsrc%2Flibraries%2Futils.js;h=01e6675b6907e6f205ca0f6860048d98d5efdd8a;hb=e29efc315df33d546237e270470916e26df391d6;hp=0000000000000000000000000000000000000000;hpb=9c5e457509ba5a1822c316635c6308874e61b4b9;p=osm%2FUI.git diff --git a/skyquake/plugins/composer/src/src/libraries/utils.js b/skyquake/plugins/composer/src/src/libraries/utils.js new file mode 100644 index 000000000..01e6675b6 --- /dev/null +++ b/skyquake/plugins/composer/src/src/libraries/utils.js @@ -0,0 +1,119 @@ +/* + * + * Copyright 2016 RIFT.IO Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; + +import changeCase from 'change-case'; + +export default { + addAuthorizationStub(xhr) { + xhr.setRequestHeader('Authorization', 'Basic YWRtaW46YWRtaW4='); + }, + getSearchParams(url) { + var a = document.createElement('a'); + a.href = url; + var params = {}; + var items = a.search.replace('?', '').split('&'); + for (var i = 0; i < items.length; i++) { + if (items[i].length > 0) { + var key_value = items[i].split('='); + params[key_value[0]] = decodeURIComponent(key_value[1]); + } + } + return params; + }, + parseJSONIgnoreErrors(txt) { + try { + return JSON.parse(txt); + } catch (ignore) { + return {}; + } + }, + resolvePath(obj, path) { + // supports a.b, a[1] and foo[bar], etc. + // where obj is ['nope', 'yes', {a: {b: 1}, foo: 2}] + // then [1] returns 'yes'; [2].a.b returns 1; [2].a[foo] returns 2; + path = path.split(/[\.\[\]]/).filter(d => d); + return path.reduce((r, p) => { + if (r) { + return r[p]; + } + }, obj); + }, + assignPathValue(obj, path, value) { + path = path.split(/[\.\[\]]/).filter(d => d); + // enable look-ahead to determine if type is array or object + const pathCopy = path.slice(); + // last item in path used to assign value on the resolved object + const name = path.pop(); + const resolvedObj = path.reduce((r, p, i) => { + if (typeof(r[p]) !== 'object') { + // look-ahead to see if next path item is a number + const isArray = !isNaN(parseInt(pathCopy[i + 1], 10)); + r[p] = isArray ? [] : {} + } + return r[p]; + }, obj); + resolvedObj[name] = value; + }, + updatePathValue(obj, path, value) { + // todo: replace implementation of assignPathValue with this impl and + // remove updatePathValue (only need one function, not both) + // same as assignPathValue except removes property if value is undefined + path = path.split(/[\.\[\]]/).filter(d => d); + const isRemove = typeof value === 'undefined'; + // enable look-ahead to determine if type is array or object + const pathCopy = path.slice(); + // last item in path used to assign value on the resolved object + const name = path.pop(); + const resolvedObj = path.reduce((r, p, i) => { + // look-ahead to see if next path item is a number + const index = parseInt(pathCopy[i + 1], 10); + const isArray = !isNaN(index); + if (typeof(r[p]) !== 'object') { + r[p] = isArray ? [] : {} + } + if (isRemove && ((i + 1) === path.length)) { + if (isArray) { + r[p] = r[p].filter((d, i) => i !== index); + } else { + delete r[p][name]; + } + } + return r[p]; + }, obj); + if (!isRemove) { + resolvedObj[name] = value; + } + }, + removePathValue(obj, path) { + // note updatePathValue removes value if third argument is undefined + return this.updatePathValue(obj, path); + }, + + suffixAsInteger: (field) => { + return (obj) =>{ + const str = String(obj[field]); + const value = str.replace(str.replace(/[\d]+$/, ''), ''); + return 1 + parseInt(value, 10) || 0; + }; + }, + + toBiggestValue: (maxIndex, curIndex) => Math.max(maxIndex, curIndex) + +}