UNPKG

@molejs/mole-jssdk

Version:

mole app jssdk

652 lines (563 loc) 16.8 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.setTitleundefined = factory()); }(this, (function () { 'use strict'; /* object-assign (c) Sindre Sorhus @license MIT */ /* eslint-disable no-unused-vars */ var getOwnPropertySymbols = Object.getOwnPropertySymbols; var hasOwnProperty = Object.prototype.hasOwnProperty; var propIsEnumerable = Object.prototype.propertyIsEnumerable; function toObject(val) { if (val === null || val === undefined) { throw new TypeError('Object.assign cannot be called with null or undefined'); } return Object(val); } function shouldUseNative() { try { if (!Object.assign) { return false; } // Detect buggy property enumeration order in older V8 versions. // https://bugs.chromium.org/p/v8/issues/detail?id=4118 var test1 = new String('abc'); // eslint-disable-line no-new-wrappers test1[5] = 'de'; if (Object.getOwnPropertyNames(test1)[0] === '5') { return false; } // https://bugs.chromium.org/p/v8/issues/detail?id=3056 var test2 = {}; for (var i = 0; i < 10; i++) { test2['_' + String.fromCharCode(i)] = i; } var order2 = Object.getOwnPropertyNames(test2).map(function (n) { return test2[n]; }); if (order2.join('') !== '0123456789') { return false; } // https://bugs.chromium.org/p/v8/issues/detail?id=3056 var test3 = {}; 'abcdefghijklmnopqrst'.split('').forEach(function (letter) { test3[letter] = letter; }); if (Object.keys(Object.assign({}, test3)).join('') !== 'abcdefghijklmnopqrst') { return false; } return true; } catch (err) { // We don't expect any of the above to throw, but better to be safe. return false; } } var objectAssign = shouldUseNative() ? Object.assign : function (target, source) { var from; var to = toObject(target); var symbols; for (var s = 1; s < arguments.length; s++) { from = Object(arguments[s]); for (var key in from) { if (hasOwnProperty.call(from, key)) { to[key] = from[key]; } } if (getOwnPropertySymbols) { symbols = getOwnPropertySymbols(from); for (var i = 0; i < symbols.length; i++) { if (propIsEnumerable.call(from, symbols[i])) { to[symbols[i]] = from[symbols[i]]; } } } } return to; }; function dingTalkPolyfill () { return { openNewPage: function openNewPage(_ref) { var url = _ref.url; window.dd.biz.util.openLink({ url: url }); }, get version() { return window.dd.version; } }; } var ua = window.navigator.userAgent; function dynamicLoadJs(url, callback) { var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.type = 'text/javascript'; script.src = url; if (typeof callback == 'function') { script.onload = script.onreadystatechange = function () { if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") { callback(); script.onload = script.onreadystatechange = null; } }; } head.appendChild(script); } var platform = { get iPhoneX() { return (/iphone/gi.test(ua) && screen.height == 812 && screen.width == 375 ); }, get iPhoneXR() { return (/iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896 ); }, get iPhoneXSMax() { return (/iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896 ); }, get os() { if (ua.match(/(Android);?[\s\/]+([\d.]+)?/)) { return 'android'; } var ipad = ua.match(/(iPad).*OS\s([\d_]+)/); var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/); var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/); if (ipad || iphone || ipod) return 'ios'; return ''; }, get container() { if (ua.toLowerCase().match(/upaasapp/i)) return 'italent'; if (ua.toLowerCase().match(/tita/i)) return 'tita'; if (ua.match(/DingTalk/i)) return 'dingTalk'; if (ua.match(/MicroMessenger/i)) return 'wechart'; if (ua.match(/wxwork/i)) return 'wxwork'; return 'browser'; }, get OS() { if (ua.match(/(Android);?[\s\/]+([\d.]+)?/)) { return 'android'; } var ipad = ua.match(/(iPad).*OS\s([\d_]+)/); var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/); var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/); if (ipad || iphone || ipod) return 'ios'; return ''; }, get ShellName() { if (ua.toLowerCase().match(/upaasapp/i)) return 'italent'; if (ua.toLowerCase().match(/tita/i)) return 'tita'; if (ua.match(/DingTalk/i)) return 'dingTalk'; if (ua.match(/MicroMessenger/i)) return 'wechart'; if (ua.match(/wxwork/i)) return 'wxwork'; return 'browser'; }, get web() { if (/mobile|android|iphone|ipad|phone/i.test(ua) && !platform.bsAppAndroid && !platform.bsAppIOS) { return true; } return false; }, get ios() { return platform.os === 'ios'; }, get android() { return platform.os === 'android'; }, get weixin() { return !!ua.match(/MicroMessenger/i); }, get weixinWork() { return !!ua.match(/wxwork/i); }, get bsAppIOS() { return !!ua.toLowerCase().match(/beisen/i) && platform.ios && platform.apiCloud; }, get beisenApp() { return platform.bsAppIOS || platform.bsAppAndroid; }, get bsAppAndroid() { return !!ua.toLowerCase().match(/beisen/i) && platform.android; }, get apiCloud() { return !!ua.toLowerCase().match(/apicloud/i); }, get titaApp() { return !!ua.toLowerCase().match(/tita/i); }, get titav2App() { return !!ua.match(/Titav2/i); }, get upaasApp() { return !!ua.toLowerCase().match(/upaasapp/i); }, get dingTalk() { return !!ua.match(/DingTalk/i); }, get bayer() { return !!ua.match(/Bayer/i); }, get iTalentIOS() { return platform.ios && platform.apiCloud && platform.upaasApp; }, get iTalentAndroid() { return platform.android && platform.upaasApp; }, get iTalentWeb() { return false; }, get api() { return platform.bsAppIOS ? window.api || window.top.api || {} : window.BSJSBridge; }, waitApi: function waitApi() { var t = void 0; return new Promise(function (reslove, reject) { if (!!ua.match(/DingTalk/i)) { return dynamicLoadJs('//g.alicdn.com/dingding/dingtalk-jsapi/2.3.0/dingtalk.open.js', function () { window.api = dingTalkPolyfill(window.dd); return reslove({}); }); } if (platform.bsAppAndroid) { if (window.BSJSBridge || window.parent.BSJSBridge) { return reslove({ api: window.BSJSBridge || window.parent.BSJSBridge }); } t = setInterval(function () { if (window.BSJSBridge || window.parent.BSJSBridge) { clearInterval(t); return reslove({ api: window.BSJSBridge || window.parent.BSJSBridge }); } }, 16); } else if (platform.bsAppIOS) { if (window.api || window.parent.api) { return reslove({ api: window.api || window.parent.api }); } t = setInterval(function () { if (window.api || window.parent.api) { clearInterval(t); return reslove({ api: window.api || window.parent.api }); } }, 16); } else { return reslove({}); } }); }, waitApiV2: function waitApiV2() { var t = void 0; return new Promise(function (reslove, reject) { if (platform.dingTalk) { return dynamicLoadJs('//g.alicdn.com/dingding/dingtalk-jsapi/2.3.0/dingtalk.open.js', function () { window.api = dingTalkPolyfill(window.dd); return reslove({}); }); } if (platform.bsAppAndroid) { if (window.bsapi || window.parent.bsapi) { return reslove({ api: window.bsapi || window.parent.bsapi }); } t = setInterval(function () { if (window.bsapi || window.parent.bsapi) { clearInterval(t); return reslove({ api: window.bsapi || window.parent.bsapi }); } }, 16); } else if (platform.bsAppIOS) { if (window.api || window.parent.api) { return reslove({ api: window.api || window.parent.api }); } t = setInterval(function () { if (window.api || window.parent.api) { clearInterval(t); return reslove({ api: window.api || window.parent.api }); } }, 16); } }); }, select: function select() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var mobile = params.mobile, androidApp = params.androidApp, iosApp = params.iosApp, beisenApp = params.beisenApp, weixin = params.weixin, weixinWork = params.weixinWork, dingTalk = params.dingTalk; var cb = void 0; if (platform.bsAppIOS && iosApp) { cb = iosApp; } else if (platform.bsAppAndroid && androidApp) { cb = androidApp; } else if (platform.dingTalk && dingTalk) { cb = dingTalk; } else if (platform.weixin && weixin) { cb = weixin; } else if (platform.weixinWork && weixinWork) { cb = weixinWork; } else { cb = mobile; } if (platform.beisenApp && beisenApp) { cb = beisenApp; } if (typeof cb === 'function') { return cb(); } } }; function Emitter(obj) { if (obj) return mixin(obj); } function mixin(obj) { for (var key in Emitter.prototype) { obj[key] = Emitter.prototype[key]; } return obj; } Emitter.prototype.on = Emitter.prototype.addEventListener = function (event, fn) { this._callbacks = this._callbacks || {}; (this._callbacks['$' + event] = this._callbacks['$' + event] || []).push(fn); this._callbacks['$' + event] = this._callbacks['$' + event].slice(this._callbacks['$' + event].length - 1); return this; }; Emitter.prototype.once = function (event, fn) { function on() { this.off(event, on); fn.apply(this, arguments); } on.fn = fn; this.on(event, on); return this; }; Emitter.prototype.off = Emitter.prototype.removeListener = Emitter.prototype.removeAllListeners = Emitter.prototype.removeEventListener = function (event, fn) { this._callbacks = this._callbacks || {}; if (0 == arguments.length) { this._callbacks = {}; return this; } var callbacks = this._callbacks['$' + event]; if (!callbacks) return this; if (1 == arguments.length) { delete this._callbacks['$' + event]; return this; } var cb; for (var i = 0; i < callbacks.length; i++) { cb = callbacks[i]; if (cb === fn || cb.fn === fn) { callbacks.splice(i, 1); break; } } return this; }; Emitter.prototype.emit = function (event) { this._callbacks = this._callbacks || {}; var args = [].slice.call(arguments, 1), callbacks = this._callbacks['$' + event]; if (callbacks) { callbacks = callbacks.slice(0); for (var i = 0, len = callbacks.length; i < len; ++i) { callbacks[i].apply(this, args); } } return this; }; Emitter.prototype.listeners = function (event) { this._callbacks = this._callbacks || {}; return this._callbacks['$' + event] || []; }; Emitter.prototype.hasListeners = function (event) { return !!this.listeners(event).length; }; var getEmitterInstance = (function () { var emitter = window.__moleAppemitter || window.top.__moleAppemitter || new Emitter(); window.__moleAppemitter = emitter; return emitter; }); function trigger(params) { var _ref = params || {}, name = _ref.name, extra = _ref.extra; function __commFunc() { var emitter = getEmitterInstance(); emitter.emit(name, extra); } platform.select({ mobile: function mobile() { __commFunc(); }, iosApp: function iosApp() { window.api.sendEvent({ name: name, extra: extra }); }, androidApp: function androidApp() { if (platform.titaApp || platform.titav2App) { return __commFunc(); } if (window.bsapi && typeof window.bsapi.sendEvent === 'function') { return window.bsapi.sendEvent({ name: name, extra: extra, param: extra }); } } }); } function getApi() { return platform.select({ iosApp: function iosApp() { return window.api || window.parent.api; }, androidApp: function androidApp() { return window.BSJSBridge || window.parent.BSJSBridge; } }); } function notify() { var param = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var cb = arguments[1]; getApi().callHandler('bs-droid-event', { param: param }, function (ret, err) { return cb && cb(window.JSON.parse(ret), err); }); } function on(params, cb) { var _ref = params || {}, name = _ref.name, extra = _ref.extra; function __commFunc() { var emitter = getEmitterInstance(); emitter.on(name, function (args) { var ret = { value: args, extra: extra }; cb && cb(ret); }); } platform.select({ mobile: function mobile() { __commFunc(); }, iosApp: function iosApp() { if (!window.api && !window.top.api) { return; } window.api.addEventListener({ name: name, extra: extra }, cb); }, androidApp: function androidApp() { if (name === 'keyback') { return notify({ action: 99, isKeyBackControl: true }, cb); } if (window.bsapi && window.bsapi.addEventListener) { window.bsapi.addEventListener({ name: name }, cb); } else { __commFunc(); } } }); } var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function setTitle(options, cb) { var params = objectAssign({}, options); var titleItem = params.titleItem; platform.getApi().then(function () { var pageId = platform.ios && window.api.winName; if (!titleItem.type) { titleItem.cat = 'titleItem'; } titleItem.pageId = pageId; platform.select({ iosApp: function iosApp() { on({ name: 'monitorNavigationEvent' }, function (ret) { var _ret$value = ret.value, cat = _ret$value.cat, control = _ret$value.control; if (pageId === ret.value.pageId && control) { cb && cb({ cat: cat, data: _extends({}, ret.value, { winName: undefined }) }); } }); trigger({ name: 'configNavigation', extra: { titleItem: titleItem, winName: pageId, pageId: pageId, reset: false } }); }, androidApp: function androidApp() { on({ name: 'event_23' }, function (ret) { var dto = JSON.parse(ret.value); if (dto.length) { dto = dto[0]; } if (dto.cat === 'titleItem' && dto.control) { return cb && cb({ cat: dto.cat, data: _extends({}, dto) }); } }); function callHandler() { window.BSJSBridge.callHandler('bs-get-header', { param: params }); } callHandler(); } }); }); } return setTitle; })));