UNPKG

sk-js

Version:
166 lines (146 loc) 6.15 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } import Mesgs from './Mesgs'; import Proxy0 from './Proxy0'; import SK from './SK'; var Codes = /*#__PURE__*/ function () { function Codes() { _classCallCheck(this, Codes); } _createClass(Codes, null, [{ key: "get", //current language hash value: function get(code) { var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '/'; var validPath = SK.getValidPath(path); var validPaths = Codes.getSubPaths(validPath, false); for (var i = 0; i < validPaths.length; i += 1) { var validPathsCode0 = Codes.code[validPaths[i]]; if (validPathsCode0) { var validPathsCodes = SK.s4a(validPathsCode0.skVal(code)); if (validPathsCodes.length !== 0) { return validPathsCodes; } } } return []; } }, { key: "getSubPaths", value: function getSubPaths(path, justUnExisted) { return justUnExisted ? Proxy0._.difference(SK.getSubPaths(path), Object.keys(Codes.code)) : SK.getSubPaths(path); } }, { key: "load", value: function load() { var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '/'; var async = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; path = SK.getValidPath(path); if (Codes.code[path]) { return Proxy0.$.when(Codes.code[path]); } else if (Proxy0.$.isEmptyObject(Codes.hash)) { var deferred = Proxy0.$.Deferred(); Codes.loadHash(async).done(function () { Codes.load(path, async).always(function () { deferred.resolve(); }); }).fail(function () { Proxy0.$.ajax({ async: async, cache: false, dataType: SK.FILE_TYPE_JSON, method: SK.REQUEST_METHOD_GET, url: Codes.SERVER_URL + Codes.PATH_PREFIX + SK.CHAR_UNDERLINE + SK.getCurrentLanguage() + SK.FILE_TYPE_JSON_WITH_POINT }).done(function (resp) { Mesgs.jsonNodeParser(resp, SK.STR_EMPTY, Codes.code); Object.keys(Codes.code).forEach(function (item) { Codes.loadMesg(Codes.code[item]); }); }).always(function () { deferred.resolve(); }); }); return deferred; } else { var _Proxy0$$; return (_Proxy0$$ = Proxy0.$).when.apply(_Proxy0$$, _toConsumableArray(Codes.getSubPaths(path, true).filter(function (validPath) { return Codes.hash[validPath]; }).map(function (validPath) { return Proxy0.$.ajax({ async: async, cache: true, dataType: SK.FILE_TYPE_JSON, method: SK.REQUEST_METHOD_GET, url: Codes.SERVER_URL + Codes.PATH_PREFIX + validPath + Codes.hash[validPath] + SK.CHAR_UNDERLINE + SK.getCurrentLanguage() + SK.FILE_TYPE_JSON_WITH_POINT }).done(function (resp) { Codes.code[validPath] = resp; Codes.loadMesg(Codes.code[validPath]); }); }))); } } }, { key: "loadHash", value: function loadHash() { var async = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; return Proxy0.$.ajax({ async: async, cache: false, dataType: SK.FILE_TYPE_JSON, method: SK.REQUEST_METHOD_GET, url: Codes.SERVER_URL + Codes.PATH_PREFIX + Mesgs.PART_OF_HASH_PATH + SK.getCurrentLanguage() + SK.FILE_TYPE_JSON_WITH_POINT }).done(function (resp) { Codes.hash = resp; }); } }, { key: "loadMesg", value: function loadMesg(pathObject) { Object.keys(pathObject).forEach(function (key) { if (Proxy0._.isArray(pathObject[key])) { pathObject[key].forEach(function (item) { pathObject[key + SK.STR_UNDERLINE_UNDERLINE + item.id] = item.text ? item.text : item.label; }); } }); } }, { key: "mesg", value: function mesg(key) { var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '/'; var validPath = SK.getValidPath(path); var validPaths = Codes.getSubPaths(validPath, false); var rtn = SK.s4s(key); for (var i = 0; i < validPaths.length; i += 1) { var tmpRtn = SK.s4o(Codes.code[validPaths[i]]).skVal(key); if (!Proxy0._.isNil(tmpRtn)) { rtn = tmpRtn; break; } } return rtn; } }, { key: "unload", value: function unload(path) { Object.keys(Codes.code).filter(function (existPath) { return Proxy0._.startsWith(existPath, path); }).forEach(function (existPath) { delete Codes.code[existPath]; }); } }]); return Codes; }(); Codes.code = {}; Codes.hash = {}; Codes.PATH_PREFIX = '/json/codes'; Codes.SERVER_URL = ''; export { Codes as default };