123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750 |
- /** ***** BEGIN LICENSE BLOCK *****
- * |------------------------------------------------------------------------------|
- * | O2OA 活力办公 创意无限 o2.js |
- * |------------------------------------------------------------------------------|
- * | Distributed under the AGPL license: |
- * |------------------------------------------------------------------------------|
- * | Copyright © 2018, o2oa.net, o2server.io O2 Team |
- * | All rights reserved. |
- * |------------------------------------------------------------------------------|
- *
- * This file is part of O2OA.
- *
- * O2OA is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * O2OA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with O2OA. If not, see <https://www.gnu.org/licenses/>.
- *
- * ***** END LICENSE BLOCK ******/
- /* load o2 Core
- * |------------------------------------------------------------------------------|
- * |addReady: o2.addReady(fn), |
- * |------------------------------------------------------------------------------|
- * |load: o2.load(urls, callback, reload) |
- * |loadCss: o2.loadCss(urls, dom, callback, reload, doc) |
- * |------------------------------------------------------------------------------|
- * |typeOf: o2.typeOf(o) |
- * |------------------------------------------------------------------------------|
- * |uuid: o2.uuid() |
- * |------------------------------------------------------------------------------|
- */
- //Element.firstElementChild Polyfill
- // (function(constructor) {
- // if (constructor &&
- // constructor.prototype &&
- // constructor.prototype.firstElementChild == null) {
- // Object.defineProperty(constructor.prototype, 'firstElementChild', {
- // get: function() {
- // var node, nodes = this.childNodes, i = 0;
- // while (node = nodes[i++]) {
- // if (node.nodeType === 1) {
- // return node;
- // }
- // }
- // return null;
- // }
- // });
- // }
- // })(window.Node || window.Element);
- if (window.Promise && !Promise.any){
- Promise.any = function(promises){
- if (!promises || !promises.length) return new Promise();
- return new Promise(function(resolve){
- var value = null;
- var resolved = false;
- var checkValue = function(){
- if (resolved) resolve(value);
- };
- for (var i=0; i<promises.length; i++){
- promises[i],then(function(v){
- value == v;
- resolved = true;
- checkValue();
- });
- }
- });
- }
- }
- if (!window.o2) {
- (function () {
- var _language = ""
- try {
- _language = localStorage.getItem("o2.language");
- } catch (e) {};
- var _href = window.location.href;
- var _debug = (_href.indexOf("debugger") !== -1);
- var _par = _href.substr(_href.lastIndexOf("?") + 1, _href.length);
- // var supportedLanguages = ["zh-cn", "en", "es", "ko", "zh-tw", "zh-hk", "ja"];
- var _lp = _language || navigator.language || "zh-cn";
- //if (!_lp) _lp = "zh-cn";
- if (_par) {
- var _parList = _par.split("&");
- for (var i = 0; i < _parList.length; i++) {
- var _v = _parList[i];
- var _kv = _v.split("=");
- if (_kv[0].toLowerCase() === "lg") _lp = _kv[1];
- if (_kv[0].toLowerCase() === "lp") _lp = _kv[1];
- }
- }
- /**
- * @summary 平台全局对象,在前端(浏览器/移动端H5页面)可用。<br/>
- * @namespace o2
- * @o2cn 平台全局对象
- * @o2category web
- */
- this.o2 = window.o2 || {};
- /**
- * @summary 平台版本信息。
- * @member {Object} version
- * @memberOf o2
- * @o2syntax
- * //获取版本号
- * var v = o2.version.v;
- */
- this.o2.version = {
- "v": "o2oa",
- "build": "2022.09.13",
- "info": "O2OA 活力办公 创意无限. Copyright © 2022, o2oa.net O2 Team All rights reserved."
- };
- /**
- * @summary 平台运行环境。
- * @member {Object} session
- * @memberOf o2
- * @property {Boolean} isDebugger 是否是调试模式
- * @property {Boolean} isMobile 是否是移动端环境
- * @o2syntax
- * var debuggerMode = o2.session.isDebugger;
- * var ismobile = o2.session.isMobile;
- */
- if (!this.o2.session) this.o2.session = {
- "isDebugger": _debug,
- "path": "../o2_core/o2"
- };
- /**
- * @summary 语言环境名称。
- * @member {String} language
- * @memberOf o2
- * @o2syntax
- * var lp = o2.language;
- */
- this.o2.languageName = _lp;
- _lp = _lp.toLocaleLowerCase();
- // if (supportedLanguages.indexOf(_lp) == -1){
- // _lp = _lp.substring(0, _lp.indexOf('-'));
- // }
- // if (supportedLanguages.indexOf(_lp) == -1) _lp = "zh-cn";
- this.o2.language = _lp;
- this.o2.splitStr = /\s*(?:,|;)\s*/;
- this.wrdp = this.o2;
- var debug = function (reload) {
- if (!o2.session.isMobile) {
- window.location.assign(_href + ((_href.indexOf("?") == -1) ? "?" : "&") + "debugger");
- } else {
- if (!o2.session.isDebugger) {
- o2.session.isDebugger = true;
- if (o2.session.isMobile || layout.mobile) o2.load("../o2_lib/eruda/eruda.js");
- }
- }
- };
- /**
- * @summary 使平台进入调试模式。
- * @function debug
- * @memberOf o2
- * @o2syntax
- * o2.debug();
- */
- this.o2.debug = debug;
- this.o2.runningRequestsList = [];
- var o2 = this.o2;
- var requestSend = XMLHttpRequest.prototype.send;
- var requestOpen = XMLHttpRequest.prototype.open;
- XMLHttpRequest.prototype.send = function(){
- var request = this;
- o2.runningRequestsList.push(request);
- request.addEventListener("loadend", function(){
- o2.runningRequestsList.splice(o2.runningRequestsList.indexOf(request, 1));
- });
- requestSend.apply(this, arguments);
- }
- XMLHttpRequest.prototype.open = function(){
- var request = this;
- request.requestOptions = Array.from(arguments);
- requestOpen.apply(this, arguments);
- }
- var _attempt = function () {
- for (var i = 0, l = arguments.length; i < l; i++) {
- try {
- arguments[i]();
- return arguments[i];
- } catch (e) {
- }
- }
- return null;
- };
- var _typeOf = function (item) {
- if (item == null) return 'null';
- if (item.$family != null) return item.$family();
- if (item.constructor == window.Array) return "array";
- if (item.nodeName) {
- if (item.nodeType == 1) return 'element';
- if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
- } else if (typeof item.length == 'number') {
- if (item.callee) return 'arguments';
- }
- return typeof item;
- };
- /**
- * @summary 判断一个任意参数的类型。
- * @function typeOf
- * @memberOf o2
- * @param {Object} [obj] 要检查的对象
- * @return {String} 对象的类型,返回值:
- * <pre><code class="language-js">'element' - 如果obj是一个DOM Element对象.
- * 'elements' - 如果obj是一个Elements实例.
- * 'textnode' - 如果obj是一个DOM text节点.
- * 'whitespace' - 如果obj是一个DOM whitespace 节点.
- * 'arguments' - 如果obj是一个arguments对象.
- * 'array' - 如果obj是一个array数组.
- * 'object' - 如果obj是一个object对象.
- * 'string' - 如果obj是一个string.
- * 'number' - 如果obj是一个数字number.
- * 'date' - 如果obj是一个日期date.
- * 'boolean' - 如果obj是一个布尔值boolean.
- * 'function' - 如果obj是一个function.
- * 'regexp' - 如果obj是一个正则表达式.
- * 'collection' - 如果obj是一个原生HTML elements collection, 如childNodes or getElementsByTagName获取的对象.
- * 'window' - 如果obj是window对象.
- * 'document' - 如果obj是document对象.
- * 'domevent' - 如果obj是一个event.
- * 'null' - 如果obj是undefined, null, NaN 或者 none.
- * </code></pre>
- * @o2syntax
- * o2.typeOf(obj);
- * @example
- * var myString = 'hello';
- * o2.typeOf(myString); // returns "string"
- */
- this.o2.typeOf = _typeOf;
- var _addListener = function (dom, type, fn) {
- if (type == 'unload') {
- var old = fn, self = this;
- fn = function () {
- _removeListener(dom, 'unload', fn);
- old();
- };
- }
- if (dom.addEventListener) dom.addEventListener(type, fn, !!arguments[2]);
- else dom.attachEvent('on' + type, fn);
- };
- var _removeListener = function (dom, type, fn) {
- if (dom.removeEventListener) dom.removeEventListener(type, fn, !!arguments[2]);
- else dom.detachEvent('on' + type, fn);
- };
- //http request class
- var _request = (function () {
- var XMLHTTP = function () {
- return new XMLHttpRequest();
- };
- var MSXML2 = function () {
- return new ActiveXObject('MSXML2.XMLHTTP');
- };
- var MSXML = function () {
- return new ActiveXObject('Microsoft.XMLHTTP');
- };
- return _attempt(XMLHTTP, MSXML2, MSXML);
- })();
- this.o2.request = _request;
- var _returnBase = function (number, base) {
- return (number).toString(base).toUpperCase();
- };
- var _getIntegerBits = function (val, start, end) {
- var base16 = _returnBase(val, 16);
- var quadArray = new Array();
- var quadString = '';
- var i = 0;
- for (i = 0; i < base16.length; i++) {
- quadArray.push(base16.substring(i, i + 1));
- }
- for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
- if (!quadArray[i] || quadArray[i] == '')
- quadString += '0';
- else
- quadString += quadArray[i];
- }
- return quadString;
- };
- var _rand = function (max) {
- return Math.floor(Math.random() * (max + 1));
- };
- this.o2.addListener = _addListener;
- this.o2.removeListener = _removeListener;
- //uuid
- var _uuid = function () {
- var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
- var dc = new Date();
- var t = dc.getTime() - dg.getTime();
- var tl = _getIntegerBits(t, 0, 31);
- var tm = _getIntegerBits(t, 32, 47);
- var thv = _getIntegerBits(t, 48, 59) + '1';
- var csar = _getIntegerBits(_rand(4095), 0, 7);
- var csl = _getIntegerBits(_rand(4095), 0, 7);
- var n = _getIntegerBits(_rand(8191), 0, 7)
- + _getIntegerBits(_rand(8191), 8, 15)
- + _getIntegerBits(_rand(8191), 0, 7)
- + _getIntegerBits(_rand(8191), 8, 15)
- + _getIntegerBits(_rand(8191), 0, 15);
- return tl + tm + thv + csar + csl + n;
- };
- /**
- * @summary 生成一个唯一的uuid。
- * @function uuid
- * @memberOf o2
- * @o2syntax
- * var id = o2.uuid();
- */
- this.o2.uuid = _uuid;
- var _runCallback = function (callback, key, par, bind, promise_cb) {
- var b = bind || callback;
- if (!key) key = "success";
- var cb;
- if (callback) {
- var type = o2.typeOf(callback).toLowerCase();
- if (key.toLowerCase() === "success" && type === "function") {
- cb = callback;
- } else {
- var name = ("on-" + key).camelCase();
- cb = (callback[name]) ? callback[name] : ((callback[key]) ? callback[key] : null);
- }
- }
- if (cb) return cb.apply(b, par);
- };
- this.o2.runCallback = _runCallback;
- if (window.CustomEvent) this.o2.customEventLoad = new CustomEvent("o2load");
- //load js, css, html adn all.
- var _getAllOptions = function (options) {
- var doc = (options && options.doc) || document;
- if (!doc.unid) doc.unid = _uuid();
- var type = (options && options.type) || "text/javascript";
- return {
- "noCache": !!(options && options.nocache),
- "reload": !!(options && options.reload),
- "sequence": !!(options && options.sequence),
- "type": type,
- "doc": doc,
- "dom": (options && options.dom) || document.body,
- "module": (options && options.module) || null,
- "noConflict": (options && options.noConflict) || false,
- "url": !!(options && options.url),
- "bind": (options && options.bind) || null,
- "evalScripts": (options && options.evalScripts) || false,
- "baseUrl": (options && options.baseUrl) ? options.baseUrl : "",
- "position": (options && options.position) || "beforeend" //'beforebegin' 'afterbegin' 'beforeend' 'afterend'debugger
- }
- };
- var _getCssOptions = function (options) {
- var doc = (options && options.doc) || document;
- if (!doc.unid) doc.unid = _uuid();
- return {
- "url": !!(options && options.url),
- "noCache": !!(options && options.nocache),
- "reload": !!(options && options.reload),
- "sequence": !!(options && options.sequence),
- "doc": doc,
- "dom": (options && options.dom) || null
- }
- };
- var _getJsOptions = function (options) {
- var doc = (options && options.doc) || document;
- if (!doc.unid) doc.unid = _uuid();
- var type = (options && options.type) || "text/javascript";
- return {
- "noCache": !!(options && options.nocache),
- "reload": !!(options && options.reload),
- "sequence": (!(options && options.sequence == false)),
- "type": type,
- "baseUrl": (options && options.baseUrl) ? options.baseUrl : "",
- "doc": doc
- }
- };
- var _getHtmlOptions = function (options) {
- var doc = (options && options.doc) || document;
- if (!doc.unid) doc.unid = _uuid();
- return {
- "noCache": !!(options && options.nocache),
- "reload": !!(options && options.reload),
- "sequence": !!(options && options.sequence),
- "doc": doc,
- "dom": (options && options.dom) || null,
- "module": (options && options.module) || null,
- "noConflict": (options && options.noConflict) || false,
- "bind": (options && options.bind) || null,
- "evalScripts": (options && options.evalScripts) || false,
- "baseUrl": (options && options.baseUrl) ? options.baseUrl : "",
- "position": (options && options.position) || "beforeend" //'beforebegin' 'afterbegin' 'beforeend' 'afterend'
- }
- };
- _filterUrl = function (url) {
- if (o2.base) {
- if (url.indexOf(":") === -1) {
- var s = url.substring(0, url.indexOf("/") + 1);
- var r = url.substring(url.indexOf("/") + 1, url.length);
- if ("../" === s || "./" === s || "/" === s) {
- return s + o2.base + r;
- } else {
- return o2.base + url
- }
- }
- }
- if (window.layout && layout.config && layout.config.urlMapping) {
- for (var k in layout.config.urlMapping) {
- var regex = new RegExp(k);
- if (regex.test(url)) {
- return url.replace(regex, layout.config.urlMapping[k]);
- }
- }
- }
- return url;
- };
- /**
- * @summary 解析平台内的url,如果配置了反向代理的路径转发,平台内的url需要通过filterUrl解析后,才能得到正确的url。
- * @see {@link https://www.o2oa.net/search.html?q=urlmapping|基于nginx快速集群部署-上下文分发}
- * @function filterUrl
- * @memberOf o2
- * @param {String} [url] 要解析的url
- * @return {String} 解析后的url
- * @o2syntax
- * var url = o2.filterUrl(url);
- * @example
- * <caption>
- * 当我们配置了按路径转发后,在portal.json中配置了urlMapping如:
- * <pre><code class="language-js">"urlMapping": {
- * "qmx.o2oa.net:20020": "qmx.o2oa.net/dev/app",
- * "qmx.o2oa.net:20030": "qmx.o2oa.net/dev/center"
- * },</code></pre>
- * 在获取平台内部的url时,如附件的下载地址,需要通过filterUrl解析。
- * </caption>
- * var attachmentUrl = "http://qmx.o2oa.net:20020/x_processplatform_assemble_surface/jaxrs/attachment/{attid}/work/{workid}";
- * var url = o2.filterUrl(attachmentUrl);
- * //return "http://qmx.o2oa.net/dev/app/x_processplatform_assemble_surface/jaxrs/attachment/{attid}/work/{workid}"
- */
- this.o2.filterUrl = _filterUrl;
- var _xhr_get = function (url, success, failure, completed, sync) {
- var xhr = new _request();
- url = _filterUrl(url);
- xhr.open("GET", url, !sync);
- var _checkCssLoaded = function (_, err) {
- if (!(xhr.readyState == 4)) return;
- if (err) {
- if (completed) completed(xhr);
- return;
- }
- _removeListener(xhr, 'readystatechange', _checkCssLoaded);
- _removeListener(xhr, 'load', _checkCssLoaded);
- _removeListener(xhr, 'error', _checkCssErrorLoaded);
- if (err) {
- if (failure) failure(xhr);
- return
- }
- var status = xhr.status;
- status = (status == 1223) ? 204 : status;
- if ((status >= 200 && status < 300))
- if (success) success(xhr);
- else if ((status >= 300 && status < 400))
- if (failure) failure(xhr);
- else
- failure(xhr);
- if (completed) completed(xhr);
- };
- var _checkCssErrorLoaded = function (err) {
- _checkCssLoaded(err)
- };
- if ("load" in xhr) _addListener(xhr, "load", _checkCssLoaded);
- if ("error" in xhr) _addListener(xhr, "load", _checkCssErrorLoaded);
- _addListener(xhr, "readystatechange", _checkCssLoaded);
- xhr.send();
- };
- this.o2.xhr_get = _xhr_get;
- var _loadSequence = function (ms, cb, op, n, thisLoaded, loadSingle, uuid, fun) {
- loadSingle(ms[n], function (module) {
- if (module) thisLoaded.push(module);
- n++;
- if (fun) fun(module);
- if (n === ms.length) {
- if (cb) cb(thisLoaded);
- } else {
- _loadSequence(ms, cb, op, n, thisLoaded, loadSingle, uuid, fun);
- }
- }, op, uuid);
- };
- var _loadDisarray = function (ms, cb, op, thisLoaded, loadSingle, uuid, fun) {
- var count = 0;
- for (var i = 0; i < ms.length; i++) {
- loadSingle(ms[i], function (module) {
- if (module) thisLoaded.push(module);
- count++;
- if (fun) fun(module);
- if (count === ms.length) if (cb) cb(thisLoaded);
- }, op, uuid);
- }
- };
- //load js
- //use framework url
- var _frameworks = {
- "o2.core": ["../o2_core/o2/o2.core.js"],
- "o2.more": ["../o2_core/o2/o2.more.js"],
- "ie_adapter": ["../o2_core/o2/ie_adapter.js"],
- "jquery": ["../o2_lib/jquery/jquery.min.js"],
- "mootools": ["../o2_lib/mootools/mootools-1.6.0_all.js"],
- "ckeditor": ["../o2_lib/htmleditor/ckeditor4161/ckeditor.js"],
- "ckeditor5": ["../o2_lib/htmleditor/ckeditor5-12-1-0/ckeditor.js"],
- "raphael": ["../o2_lib/raphael/raphael.js"],
- "d3": ["../o2_lib/d3/d3.min.js"],
- "ace": ["../o2_lib/ace/src-min-noconflict/ace.js", "../o2_lib/ace/src-min-noconflict/ext-language_tools.js"],
- //"ace": ["../o2_lib/ace/src-noconflict/ace.js","../o2_lib/ace/src-noconflict/ext-language_tools.js"],
- "monaco": ["../o2_lib/vs/loader.js"],
- "JSBeautifier": ["../o2_lib/JSBeautifier/beautify.js"],
- "JSBeautifier_css": ["../o2_lib/JSBeautifier/beautify-css.js"],
- "JSBeautifier_html": ["../o2_lib/JSBeautifier/beautify-html.js"],
- "JSONTemplate": ["../o2_lib/mootools/plugin/Template.js"],
- "kity": ["../o2_lib/kityminder/kity/kity.js"],
- "kityminder": ["../o2_lib/kityminder/core/dist/kityminder.core.js"],
- "vue": ["../o2_lib/vue/vue.pro.js"],
- "vue_develop": ["../o2_lib/vue/vue.js"],
- "elementui": ["../o2_lib/vue/element/index.js"]
- };
- var _loaded = {};
- var _loadedCss = {};
- var _loadedHtml = {};
- var _loadCssRunning = {};
- var _loadCssQueue = [];
- var _loadingModules = {};
- var _checkUrl = function (url, base) {
- var urlStr = new URI(url, {base: base}).toString();
- return urlStr;
- }
- var _loadSingle = function (module, callback, op) {
- var url = module;
- if (op.baseUrl) url = _checkUrl(url, op.baseUrl);
- var uuid = _uuid();
- if (op.noCache) url = (url.indexOf("?") !== -1) ? url + "&v=" + uuid : addr_uri + "?v=" + uuid;
- var key = encodeURIComponent(url + op.doc.unid);
- if (!op.reload) if (_loaded[key]) {
- Promise.resolve(_loaded[key]).then(function(o){
- if (callback) callback(o);
- });
- //if (callback) callback();
- return;
- }
- if (_loadingModules[key] && !op.reload) {
- if (!_loadingModules[key].callbacks) _loadingModules[key].callbacks = [];
- _loadingModules[key].callbacks.push(callback);
- } else {
- _loadingModules[key] = {callbacks: [callback]};
- var head = (op.doc.head || op.doc.getElementsByTagName("head")[0] || op.doc.documentElement);
- var s = op.doc.createElement('script');
- s.type = op.type || "text/javascript";
- head.appendChild(s);
- s.id = uuid;
- s.src = this.o2.filterUrl(url);
- var _checkScriptLoaded = function (_, isAbort, err) {
- if (isAbort || !s.readyState || s.readyState === "loaded" || s.readyState === "complete") {
- var scriptObj = {"module": module, "id": uuid, "script": s, "doc": op.doc};
- if (!err) _loaded[key] = scriptObj;
- _removeListener(s, 'readystatechange', _checkScriptLoaded);
- _removeListener(s, 'load', _checkScriptLoaded);
- _removeListener(s, 'error', _checkScriptErrorLoaded);
- if (!isAbort || err) {
- if (err) {
- if (s) head.removeChild(s);
- while (_loadingModules[key].callbacks.length) {
- (_loadingModules[key].callbacks.shift())();
- }
- delete _loadingModules[key];
- //if (callback)callback();
- } else {
- //head.removeChild(s);
- while (_loadingModules[key].callbacks.length) {
- (_loadingModules[key].callbacks.shift())(scriptObj);
- }
- delete _loadingModules[key];
- //if (callback)callback(scriptObj);
- }
- }
- }
- };
- var _checkScriptErrorLoaded = function (e, err) {
- console.log("Error: load javascript module: " + module);
- _checkScriptLoaded(e, true, "error");
- };
- if ('onreadystatechange' in s) _addListener(s, 'readystatechange', _checkScriptLoaded);
- _addListener(s, 'load', _checkScriptLoaded);
- _addListener(s, 'error', _checkScriptErrorLoaded);
- }
- };
- var _load = function (urls, options, callback) {
- if (window.document && !window.importScripts) {
- var ms = (_typeOf(urls) === "array") ? urls : [urls];
- var op = (_typeOf(options) === "object") ? _getJsOptions(options) : _getJsOptions(null);
- var cbk = (_typeOf(options) === "function") ? options : callback;
- var cb = cbk;
- if (typeof define === 'function' && define.amd) {
- define.amd = false;
- cb = (cbk) ? function () {
- define.amd = true;
- cbk();
- } : function () {
- define.amd = true;
- }
- }
- var modules = [];
- for (var i = 0; i < ms.length; i++) {
- var url = ms[i];
- var module = _frameworks[url] || url;
- if (_typeOf(module) === "array") {
- modules = modules.concat(module)
- } else {
- modules.push(module)
- }
- }
- var thisLoaded = [];
- if (op.sequence) {
- _loadSequence(modules, cb, op, 0, thisLoaded, _loadSingle);
- } else {
- _loadDisarray(modules, cb, op, thisLoaded, _loadSingle);
- }
- } else {
- if (window.importScripts) {
- var ms = (_typeOf(urls) === "array") ? urls : [urls];
- ms.each(function (url) {
- window.importScripts(o2.filterUrl(url));
- });
- var cbk = (_typeOf(options) === "function") ? options : callback;
- if (cbk) cbk();
- }
- }
- };
- /**
- * @summary 引入外部javascript文件。
- * @function load
- * @memberOf o2
- * @param {String|Array} [urls] 要载入的js文件url,或要载入多个js问价的urls数组。
- * @param {Object|Function} [options|callback] 载入js文件的配置参数,或者载入成功后的回调。
- * <pre><code class="language-js">options参数格式如下:
- * {
- * "noCache": 是否使用缓存,默认true,
- * "reload": 如果相同路径的js文件已经加载了,是否重新载入,默认为:false
- * "sequence": 当urls参数为数组时,多个脚本文件是否按数组顺序依次载入,默认为false
- * "type": 载入脚本的类型,默认为"text/javascript"
- * "baseUrl": 要载入脚本的url的base路径,默认""
- * "doc": 要在哪个document对象中载入脚本文件,默认为当前document
- * }
- * </code></pre>
- * @param {Function} [callback] 可选参数,载入成功后的回调方法。
- * @o2syntax
- * o2.load(urls, options, callback);
- * @example
- * //载入jsfile1.js和js/jsfile2.js两个文件,它们是按顺序载入的
- * o2.load(["js/jsfile1.js", "js/jsfile2.js"], function(){
- * //js文件已经载入
- * });
- *
- * //载入jsfile1.js和js/jsfile2.js两个文件,它们是同时载入的
- * //并且无论是否已经加载过,都需要重新加载,并且要按顺序加载
- * o2.load(["js/jsfile1.js", "js/jsfile2.js"], {"reload": true, "sequence": true}, function(){
- * //js文件已经载入
- * });
- */
- this.o2.load = _load;
- //load css
- var _loadSingleCss = function (module, callback, op, uuid) {
- var url = module;
- var uid = _uuid();
- if (op.noCache) url = (url.indexOf("?") !== -1) ? url + "&v=" + uid : url + "?v=" + uid;
- var key = encodeURIComponent(url + op.doc.unid);
- if (_loadCssRunning[key]) {
- _loadCssQueue.push(function () {
- _loadSingleCss(module, callback, op, uuid);
- });
- return;
- }
- var completed = function () {
- if (_loadCssRunning[key]) {
- _loadCssRunning[key] = false;
- delete _loadCssRunning[key];
- }
- if (_loadCssQueue && _loadCssQueue.length) {
- (_loadCssQueue.shift())();
- }
- };
- if (_loadedCss[key]) uuid = _loadedCss[key]["class"];
- if (op.dom) _parseDom(op.dom, function (node) {
- if (node.className.indexOf(uuid) == -1) node.className += ((node.className) ? " " + uuid : uuid);
- }, op.doc);
- if (_loadedCss[key]) if (!op.reload) {
- Promise.resolve(_loadedCss[key]).then(function(o){
- if (callback) callback(o);
- });
- //if (callback) callback(_loadedCss[key]);
- completed();
- return;
- }
- if (op.url){
- var style = op.doc.createElement("link");
- style.setAttribute("rel", "stylesheet");
- style.setAttribute("type", "text/css");
- style.setAttribute("id", uuid);
- style.setAttribute("href", url);
- if (!op.notInject) {
- var head = (op.doc.head || op.doc.getElementsByTagName("head")[0] || op.doc.documentElement);
- head.appendChild(style);
- }
- var styleObj = {"module": module, "id": uid, "style": style, "doc": op.doc, "class": uuid};
- if (callback) callback(styleObj);
- completed();
- }else{
- var success = function (xhr) {
- var cssText = xhr.responseText;
- try {
- if (cssText) {
- op.uuid = uuid;
- var style = _loadCssText(cssText, op);
- }
- style.id = uid;
- var styleObj = {"module": module, "id": uid, "style": style, "doc": op.doc, "class": uuid};
- _loadedCss[key] = styleObj;
- if (callback) callback(styleObj);
- } catch (e) {
- if (callback) callback();
- return;
- }
- };
- var failure = function (xhr) {
- console.log("Error: load css module: " + module);
- if (callback) callback();
- };
- _loadCssRunning[key] = true;
- _xhr_get(url, success, failure, completed);
- }
- };
- var _parseDomString = function (dom, fn, sourceDoc) {
- var doc = sourceDoc || document;
- var list = doc.querySelectorAll(dom);
- if (list.length) for (var i = 0; i < list.length; i++) _parseDomElement(list[i], fn);
- };
- var _parseDomElement = function (dom, fn) {
- if (fn) fn(dom);
- };
- var _parseDom = function (dom, fn, sourceDoc) {
- var domType = _typeOf(dom);
- if (domType === "string") _parseDomString(dom, fn, sourceDoc);
- if (domType === "element") _parseDomElement(dom, fn);
- if (domType === "array") for (var i = 0; i < dom.length; i++) _parseDom(dom[i], fn, sourceDoc);
- };
- var _loadCss = function (modules, options, callback) {
- var ms = (_typeOf(modules) === "array") ? modules : [modules];
- var op = (_typeOf(options) === "object") ? _getCssOptions(options) : _getCssOptions(null);
- var cb = (_typeOf(options) === "function") ? options : callback;
- var uuid = "css" + _uuid();
- var thisLoaded = [];
- if (op.sequence) {
- _loadSequence(ms, cb, op, 0, thisLoaded, _loadSingleCss, uuid);
- } else {
- _loadDisarray(ms, cb, op, thisLoaded, _loadSingleCss, uuid);
- }
- };
- var _removeCss = function (modules, doc) {
- var thisDoc = doc || document;
- var ms = (_typeOf(modules) === "array") ? modules : [modules];
- for (var i = 0; i < ms.length; i++) {
- var module = ms[i];
- var k = encodeURIComponent(module + (thisDoc.unid || ""));
- var removeCss = _loadedCss[k];
- if (!removeCss) for (key in _loadedCss) {
- if (_loadedCss[key].id == module) {
- removeCss = _loadedCss[key];
- k = key;
- break;
- }
- }
- if (removeCss) {
- delete _loadedCss[k];
- var styleNode = removeCss.doc.getElementById(removeCss.id);
- if (styleNode) styleNode.parentNode.removeChild(styleNode);
- removeCss = null;
- }
- }
- };
- var _loadCssText = function (cssText, options, callback) {
- var op = (_typeOf(options) === "object") ? _getCssOptions(options) : _getCssOptions(null);
- var cb = (_typeOf(options) === "function") ? options : callback;
- var uuid = options.uuid || "css" + _uuid();
- if (cssText) {
- if (op.dom) _parseDom(op.dom, function (node) {
- if (node.className.indexOf(uuid) == -1) node.className += ((node.className) ? " " + uuid : uuid);
- }, op.doc);
- cssText = cssText.replace(/\/\*(\s|\S)*?\*\//g, "");
- if (op.bind) cssText = cssText.bindJson(op.bind);
- if (op.dom) {
- var rex = new RegExp("(.+)(?=[\\r\\n]*\\{)", "g");
- var match;
- var prefix = "." + uuid + " ";
- while ((match = rex.exec(cssText)) !== null) {
- // var rule = prefix + match[0];
- // cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
- // rex.lastIndex = rex.lastIndex + prefix.length;
- var rulesStr = match[0];
- var startWith = rulesStr.substring(0, 1);
- if (startWith === "@" || startWith === ":" || rulesStr.indexOf("%") !== -1) {
- // var begin = 0;
- // var end = 0;
- }else if (rulesStr.trim()==='from' || rulesStr.trim()==='to'){
- //nothing
- } else {
- if (rulesStr.indexOf(",") != -1) {
- //var rules = rulesStr.split(/\s*,\s*/g);
- var rules = rulesStr.split(/,/g);
- rules = rules.map(function (r) {
- return prefix + r;
- });
- var rule = rules.join(",");
- cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
- rex.lastIndex = rex.lastIndex + (prefix.length * rules.length);
- } else {
- var rule = prefix + match[0];
- cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
- rex.lastIndex = rex.lastIndex + prefix.length;
- }
- }
- }
- }
- var style = op.doc.createElement("style");
- style.setAttribute("type", "text/css");
- style.setAttribute("id", uuid);
- if (!op.notInject) {
- var head = (op.doc.head || op.doc.getElementsByTagName("head")[0] || op.doc.documentElement);
- head.appendChild(style);
- }
- if (style.styleSheet) {
- var setFunc = function () {
- style.styleSheet.cssText = cssText;
- };
- if (style.styleSheet.disabled) {
- setTimeout(setFunc, 10);
- } else {
- setFunc();
- }
- } else {
- var cssTextNode = op.doc.createTextNode(cssText);
- style.appendChild(cssTextNode);
- }
- }
- if (callback) callback(style, uuid);
- return style;
- };
- /**
- * @summary 引入外部css资源。
- * @function loadCss
- * @memberOf o2
- * @param {String|Array} [urls] 要载入的css文件url,或要载入多个css文件的urls数组。
- * @param {Object|Function} [options|callback] 载入css文件的配置参数,或者载入成功后的回调。
- * <pre><code class="language-js">options参数格式如下:
- * {
- * "noCache": 是否使用缓存,默认true,
- * "reload": 如果相同路径的css文件已经引入了,是否重新载入,默认为:false
- * "sequence": 当urls参数为数组时,多个css文件是否按数组顺序依次载入,默认为false
- * "dom": dom element对象,表示css在这个element生效,默认是null,表示对整个document生效
- * }
- * </code></pre>
- * @param {Function} [callback] 可选参数,载入成功后的回调方法。
- * @o2syntax
- * o2.loadCss(urls, options, callback);
- * @o2syntax
- * Element.loadCss(urls, options, callback);
- * @example
- * //载入style1.css和style2.css两个文件,作用于document
- * o2.loadCss(["../css/style1.css", "../css/style2.css"], function(){
- * //css文件已经载入
- * });
- *
- * //载入style1.css和style2.css两个文件,作用于id为content的dom对象
- * o2.loadCss(["../css/style1.css", "../css/style2.css"], {"dom": document.getELementById("content")}, function(){
- * //css文件已经载入
- * });
- * //在Dom对象上载入style1.css和style2.css两个css
- * var node = document.getElementById("mydiv");
- * node.loadCss(["../css/style1.css", "../css/style2.css"], function(){
- * //css文件已经载入
- * });
- */
- this.o2.loadCss = _loadCss;
- /**
- * @summary 引入文本css资源。
- * @function loadCssText
- * @memberOf o2
- * @param {String} [cssText] 要载入的css文本内容。
- * @param {Object|Function} [options|callback] 载入css文件的配置参数,或者载入成功后的回调。
- * <pre><code class="language-js">options参数格式如下:
- * {
- * "dom": dom element对象,表示css在这个element生效,默认是null,表示对整个document生效
- * }
- * </code></pre>
- * @param {Function} [callback] 可选参数,载入成功后的回调方法。
- * @o2syntax
- * o2.loadCssText(cssText, options, callback);
- * @o2syntax
- * Element.loadCssText(cssText, options, callback);
- * @see o2.loadCss
- * @example
- * //引入css文本,作用于id为content的dom对象
- * var csstext = ".myclass{color:#ff0000}"
- * o2.loadCssText(csstext, {"dom": document.getELementById("content")}, function(){
- * //css已经载入
- * });
- * //引入css文本,作用于id为content的dom对象
- * var csstext = ".myclass{color:#ff0000}"
- * var node = document.getELementById("content");
- * node.loadCssText(csstext, function(){
- * //css已经载入
- * });
- */
- this.o2.loadCssText = _loadCssText;
- if (window.Element) Element.prototype.loadCss = function (modules, options, callback) {
- var op = (_typeOf(options) === "object") ? options : {};
- var cb = (_typeOf(options) === "function") ? options : callback;
- op.dom = this;
- _loadCss(modules, op, cb);
- };
- if (window.Element) Element.prototype.loadCssText = function (cssText, options, callback) {
- var op = (_typeOf(options) === "object") ? options : {};
- var cb = (_typeOf(options) === "function") ? options : callback;
- op.dom = this;
- return _loadCssText(cssText, op, cb);
- };
- /**
- * @summary 移除通过o2.loadCss方法引入css资源。
- * @function removeCss
- * @memberOf o2
- * @param {String|Array} [urls] 要移除的的css文本url,必须与引入时所使用的url相同。
- * @o2syntax
- * o2.removeCss(urls);
- * @example
- * //载入style1.css和style2.css两个文件,作用于id为content的dom对象
- * o2.load(["../css/style1.css", "../css/style2.css"], {"dom": document.getELementById("content")}, function(){
- * //css文件已经载入
- * });
- *
- * //移除style1.css和style2.css两个文件
- * //引入时使用了"../css/style1.css"字符串作为路径,移除时也要使用相同的字符串
- * o2.removeCss(["../css/style1.css", "../css/style2.css"])
- */
- this.o2.removeCss = _removeCss;
- //load html
- _loadSingleHtml = function (module, callback, op) {
- var url = module;
- var uid = _uuid();
- if (op.noCache) url = (url.indexOf("?") !== -1) ? url + "&v=" + uid : url + "?v=" + uid;
- var key = encodeURIComponent(url + op.doc.unid);
- if (!op.reload) if (_loadedHtml[key]) {
- Promise.resolve(_loadedHtml[key]).then(function(html){
- if (callback) callback(html);
- });
- //if (callback) callback(_loadedHtml[key]);
- return;
- }
- var success = function (xhr) {
- var htmlObj = {"module": module, "id": uid, "data": xhr.responseText, "doc": op.doc};
- _loadedHtml[key] = htmlObj;
- if (callback) callback(htmlObj);
- };
- var failure = function () {
- console.log("Error: load html module: " + module);
- if (callback) callback();
- };
- _xhr_get(url, success, failure);
- };
- var _injectHtml = function (op, data, baseUrl) {
- if (op.bind) data = data.bindJson(op.bind);
- if (op.dom) _parseDom(op.dom, function (node) {
- var scriptText;
- var scriptSrc;
- var text = data.stripScriptSrcs(function (script) {
- scriptSrc = script;
- });
- text = text.stripScripts(function (script) {
- scriptText = script;
- });
- if (op.baseUrl){
- var reg = /(?:href|src)\s*=\s*"([^"]*)"/gi;
- var m = reg.exec(text);
- while (m) {
- var l = m[0].length;
- var u = new URI(m[1], {base: baseUrl}).toString();
- var r = m[0].replace(m[1], u);
- var i = r.length - l;
- var left = text.substring(0, m.index);
- var right = text.substring(m.index + l, text.length);
- text = left + r + right;
- reg.lastIndex = reg.lastIndex + i;
- m = reg.exec(text);
- }
- }
- if (op.module) {
- _parseModule(node, text, op);
- //node.insertAdjacentHTML(op.position, data);
- } else {
- node.insertAdjacentHTML(op.position, text);
- }
- if (op.evalScripts) {
- if (scriptSrc) {
- var scriptSrcs = scriptSrc.split(/\n/g).trim();
- if (scriptSrcs && scriptSrcs.length) {
- o2.load(scriptSrcs, {baseUrl: baseUrl, reload:true}, function () {
- });
- if (op.evalScripts && scriptText) Browser.exec(scriptText);
- } else {
- if (op.evalScripts && scriptText) Browser.exec(scriptText);
- }
- }
- }
- }, op.doc);
- };
- var _parseModule = function (node, data, op) {
- var dom = op.noConflict ? document.createElement("div") : node;
- if (op.noConflict) {
- dom.insertAdjacentHTML("afterbegin", data);
- } else {
- dom.insertAdjacentHTML(op.position, data);
- }
- var bindDataId = "";
- var bindDataNode = dom.querySelector("[data-o2-binddata]");
- if (bindDataNode){
- bindDataId = bindDataNode.dataset["o2Binddata"];
- bindDataNode.destroy();
- }
- var els = dom.querySelectorAll("[data-o2-element],[data-o2-events]");
- for (var i = 0; i < els.length; i++) {
- var el = els.item(i);
- var name = el.getAttribute("data-o2-element");
- if (name) _bindToModule(op.module, el, name.toString());
- if (el.hasAttribute("data-o2-events")) {
- var events = el.getAttribute("data-o2-events").toString();
- if (events) _bindToEvents(op.module, el, events, bindDataId);
- el.removeAttribute("data-o2-events");
- }
- }
- if (op.noConflict) {
- var n = dom.firstElementChild;
- var newNode = node.insertAdjacentElement(op.position, n);
- nextNode = dom.firstElementChild;
- while (nextNode) {
- newNode = newNode.insertAdjacentElement("afterend", nextNode);
- nextNode = dom.firstElementChild;
- }
- dom.destroy();
- }
- };
- var _bindToEvents = function (m, node, events, bindDataId) {
- var p = node.getParent("div[data-o2-binddataid]");
- var data = null;
- if (p){
- data = _parseDataCache[p.dataset["o2Binddataid"]];
- //_parseDataCache[p.dataset["o2Binddataid"]] = null;
- //delete _parseDataCache[p.dataset["o2Binddataid"]];
- }else{
- if (bindDataId) data = (_parseDataCache[bindDataId] || null);
- //_parseDataCache[bindDataId] = null;
- //delete _parseDataCache[bindDataId];
- }
- // var data = (p) ? _parseDataCache[p.dataset["o2Binddataid"]] : (_parseDataCache["bind"] || null);
- var eventList = events.split(/\s*;\s*/);
- eventList.forEach(function (ev) {
- var evs = ev.split(/\s*:\s*/);
- if (evs.length > 1) {
- var event = evs.shift();
- var method = evs.shift();
- // if (event==="o2load"){
- //
- // if (m[method]) m[method].apply(m, evs.concat([new PointerEvent("o2load"), data]));
- // }else{
- node.addEventListener(event, function (e) {
- if (m[method]) m[method].apply(m, evs.concat([e, data]));
- }, false);
- // }
- }
- });
- // try {
- node.dispatchEvent(o2.customEventLoad);
- // }catch(e){
- // debugger;
- // console.error(e)
- // }
- }
- var _bindToModule = function (m, node, name) {
- // if (m[name]){
- // if (o2.typeOf(m[name])!=="array"){
- // var tmp = m[name];
- // m[name] = [];
- // m[name].push(tmp);
- // }
- // m[name].push(node);
- // }else{
- m[name] = node;
- // }
- };
- var _loadHtml = function (modules, options, callback) {
- var ms = (_typeOf(modules) === "array") ? modules : [modules];
- var op = (_typeOf(options) === "object") ? _getHtmlOptions(options) : _getHtmlOptions(null);
- var cb = (_typeOf(options) === "function") ? options : callback;
- var thisLoaded = [];
- if (op.sequence) {
- _loadSequence(ms, cb, op, 0, thisLoaded, _loadSingleHtml, null, function (html) {
- if (html) _injectHtml(op, html.data, html.module);
- });
- } else {
- _loadDisarray(ms, cb, op, thisLoaded, _loadSingleHtml, null, function (html) {
- if (html) _injectHtml(op, html.data, html.module);
- });
- }
- };
- /**
- * @summary 引入外部html模板资源,并将html内容渲染到指定dom对象的某个位置。
- * @function loadHtml
- * @memberOf o2
- * @param {String|Array} [urls] 要载入的html文件url,或要载入多个html文件的urls数组。
- * @param {Object|Function} [options|callback] 载入html文件的配置参数,或者载入成功后的回调。
- * <pre><code class="language-js">options参数格式如下:
- * {
- * "noCache": 是否使用缓存,默认true,
- * "reload": 如果相同路径的html文件已经引入了,是否重新载入,默认为:false
- * "sequence": 当urls参数为数组时,多个html文件是否按数组顺序依次载入,默认为false
- * "dom": 引入html后,要将html内容渲染到的目标dom对象(具体位置由position参数确定),
- * "position": 渲染到的目标dom对象的位置,可以是以下值:'beforebegin' 'afterbegin' 'beforeend'(默认) 'afterend'
- * "module": Object,与此html模板关联的对象。(在下面的例子中详细介绍)
- * "bind": Json,与此html模板关联的Json对象。(在下面的例子中详细介绍)
- * "evalScripts": html模板中通过<script>引入或内嵌的javascript,是否要执行。默认为false
- * "baseUrl": html模板中引用连接的baseUrl,默认为空
- * }
- * </code></pre>
- * @param {Function} [callback] 可选参数,载入成功后的回调方法。
- * @o2syntax
- * o2.loadHtml(urls, options, callback);
- * @o2syntax
- * Element.loadHtml(urls, options, callback);
- * @example
- * <caption><b>样例1: </b>引入一个html模板文件,并插入到id为content的dom对象的最后</caption>
- * //引入一个html模板文件,并插入到id为content的dom对象的最后
- * var node = document.getELementById("content");
- * o2.loadHtml("../html/template.html", {"dom": node}, function(){
- * //html文件已经载入
- * });
- *
- * //或者使用Element.loadHtml方法
- * var node = document.getELementById("content");
- * node.loadHtml("../html/template.html", function(){
- * //html文件已经载入
- * });
- *
- * @example
- * <caption>
- * <b>样例2: </b>本例中我们使用一个html模板来渲染展现已有的数据。我们将一个json对象绑定到要载入的模板,通过{{$.xxx}}来展现json数据。<br>
- * html模板内容如下:
- * <pre><code class="language-js"><div>{{$.title}}</div>
- * <div>{{$.description}}</div></code></pre>
- * 然后通过以下代码来载入html模板:
- * </caption>
- * var json = {
- * "title": "这是标题",
- * "description": "描述内容"
- * };
- * var node = document.getELementById("content");
- * o2.loadHtml("../html/template.html", {"dom": node, "bind": json}, function(){
- * //html文件已经载入
- * //载入后,node对象的html如下:
- * //<div>
- * // ......
- * // <div>这是标题</div>;
- * // <div>描述内容</div>
- * //</div>
- * });
- * @example
- * <caption>
- * <b>样例3: </b>本例中我们除了使用一个html模板来渲染展现已有的json数据,还给已有模块绑定上html模板中的一个dom对象,并监听html模板中的指定元素的事件。<br>
- * html模板内容如下:
- * <pre><code class="language-js"><div>{{$.title}}</div>
- * <div>{{$.description}}</div>
- * <div data-o2-element="myElement"></div>
- * <button data-o2-events="click:clickMe:{{$.info}};mouseover:overMe;mouseout:outMe">绑定了事件的按钮</button>
- * </code></pre>
- * 然后通过以下代码来载入html模板:
- * </caption>
- * //json数据
- * var json = {
- * "title": "这是标题",
- * "description": "描述内容",
- * "info": "按钮点击后的信息"
- * };
- * //业务模块
- * var module = {
- * //当button按钮被点击时,会调用此方法
- * clickMe: function(info, e){
- * this.myElement.insertAdjacentText("afterbegin", "button clicked " + info);
- * },
- * //当鼠标移动到button按钮时,会调用此方法
- * overMe: function(e){
- * console.log("button over");
- * console.log(e); //MouseEvent对象
- * },
- * //当鼠标移出button按钮时,会调用此方法
- * outMe: function(e){
- * console.log("button out");
- * console.log(e); //MouseEvent对象
- * }
- * };
- *
- * var node = document.getELementById("content");
- * node.loadHtml("../html/template.html", {"bind": json, "module": module}, function(){
- * //html文件已经载入
- * //载入后,node对象的html如下:
- * //<div>
- * // ......
- * // <div>这是标题</div>;
- * // <div>描述内容</div>
- * // <div data-o2-element="myElement"></div>
- * // <button>绑定了事件的按钮</button>
- * //</div>
- * console.log(module.myElement); //Dom对象:<div data-o2-element="myElement"></div>
- * //html模板中的div对象,被绑定到了module对象的myElement属性上
- *
- * });
- * @example
- * <caption>
- * <b>样例4: </b>本例中演示了html模板中each和if的用法。<br>
- * html模板内容如下:
- * <pre><code class="language-js"><div>
- * {{each $.items}}
- * <div>{{$.title}}</div>
- * <div>{{$.description}}</div>
- * {{if $.title=="这是标题2"}}
- * <div>这是标题2的个性化内容</div>
- * {{end if}}
- * <button data-o2-events="click:clickMe:{{$.info}}">绑定了事件的按钮</button>
- * <hr>
- * {{end each}}
- * </div></code></pre>
- * 然后通过以下代码来载入html模板:
- * </caption>
- * //json数据
- * var json = {
- * "items": [{
- * "title": "这是标题1",
- * "description": "描述内容1",
- * "info": "按钮点击后的信息1"
- * },{
- * "title": "这是标题2",
- * "description": "描述内容2",
- * "info": "按钮点击后的信息2"
- * },{
- * "title": "这是标题3",
- * "description": "描述内容3",
- * "info": "按钮点击后的信息3"
- * }]
- * };
- * //业务模块
- * var module = {
- * //当button按钮被点击时,会调用此方法
- * clickMe: function(info, e){
- * alert(info);
- * }
- * };
- * var node = document.getELementById("content");
- * node.loadHtml("../html/template.html", {"bind": json, "module": module}, function(){
- * //html文件已经载入
- * //载入后,node对象的html如下:
- * //<div>
- * // <div>这是标题1</div>;
- * // <div>描述内容1</div>
- * // <button>绑定了事件的按钮</button>
- * // <hr>
- * //</div>
- * //<div>
- * // <div>这是标题2</div>;
- * // <div>描述内容2</div>
- * // <div>描这是标题2的个性化内容</div>
- * // <button>绑定了事件的按钮</button>
- * // <hr>
- * //</div>
- * //<div>
- * // <div>这是标题3</div>;
- * // <div>描述内容3</div>
- * // <button>绑定了事件的按钮</button>
- * // <hr>
- * //</div>
- * //当点击按钮时,会alert对应的items的info数据
- * });
- */
- this.o2.loadHtml = _loadHtml;
- if (window.Element) Element.prototype.loadHtml = function (modules, options, callback) {
- var op = (_typeOf(options) === "object") ? options : {};
- var cb = (_typeOf(options) === "function") ? options : callback;
- op.dom = this;
- _loadHtml(modules, op, cb);
- };
- /**
- * @summary 解析html文本内容,并将html内容渲染到指定dom对象的某个位置,与loadHtml相同,只是传入html内容,而不是获取html的url。
- * @function loadHtmlText
- * @memberOf o2
- * @param {String} [html] 要解析的html文本内容。
- * @param {Object} [options] 载入html文件的配置参数。
- * <pre><code class="language-js">options参数格式如下:
- * {
- * "noCache": 是否使用缓存,默认true,
- * "reload": 如果相同路径的html文件已经引入了,是否重新载入,默认为:false
- * "sequence": 当urls参数为数组时,多个html文件是否按数组顺序依次载入,默认为false
- * "dom": 引入html后,要将html内容渲染到的目标dom对象(具体位置由position参数确定),
- * "position": 渲染到的目标dom对象的位置,可以是以下值:'beforebegin' 'afterbegin' 'beforeend'(默认) 'afterend'
- * "module": Object,与此html模板关联的对象。(在下面的例子中详细介绍)
- * "bind": Json,与此html模板关联的Json对象。(在下面的例子中详细介绍)
- * "evalScripts": html模板中通过<script>引入或内嵌的javascript,是否要执行。默认为false
- * "baseUrl": html模板中引用连接的baseUrl,默认为空
- * }
- * </code></pre>
- * @o2syntax
- * o2.loadHtmlText(html, options);
- * @o2syntax
- * Element.loadHtmlText(html, options);
- * @see o2.loadHtml
- * @example
- * var html = "<div>{{$.title}}</div>"
- * var json = {"title": "标题"};
- * var node = document.getELementById("content");
- * o2.loadHtmlText(html, {"dom": node, "bind": json});
- *
- * //获
- * node.loadHtmlText(html, {"bind": json});
- */
- this.o2.loadHtmlText = this.o2.injectHtml = function (html, options) {
- var op = (_typeOf(options) === "object") ? _getHtmlOptions(options) : _getHtmlOptions(null);
- _injectHtml(op, html);
- };
- if (window.Element) Element.prototype.loadHtmlText = Element.prototype.injectHtml = function (html, options) {
- var op = (_typeOf(options) === "object") ? options : {};
- op.dom = this;
- op.position = (options && options.position) || "beforeend"
- _injectHtml(op, html);
- };
- //load all
- _loadAll = function (modules, options, callback) {
- //var ms = (_typeOf(modules)==="array") ? modules : [modules];
- var op = (_typeOf(options) === "object") ? _getAllOptions(options) : _getAllOptions(null);
- var cb = (_typeOf(options) === "function") ? options : callback;
- var ms, htmls, styles, sctipts;
- var _htmlLoaded = (!modules.html), _cssLoaded = (!modules.css), _jsLoaded = (!modules.js);
- var _checkloaded = function () {
- if (_htmlLoaded && _cssLoaded && _jsLoaded) if (cb) cb(htmls, styles, sctipts);
- };
- if (modules.html) {
- _loadHtml(modules.html, op, function (h) {
- htmls = h;
- _htmlLoaded = true;
- _checkloaded();
- });
- }
- if (modules.css) {
- _loadCss(modules.css, op, function (s) {
- styles = s;
- _cssLoaded = true;
- _checkloaded();
- });
- }
- if (modules.js) {
- _load(modules.js, op, function (s) {
- sctipts = s;
- _jsLoaded = true;
- _checkloaded();
- });
- }
- };
- /**
- * @summary 同时载入js文件、css文件和html模板文件,相当于同时调用了o2.load, o2.loadCss 和 o2.loadHtml。
- * @function loadAll
- * @memberOf o2
- * @param {Object} [modules] 要解析的html文本内容。
- * <pre><code class="language-js">modules参数格式如下:
- * {
- * "js": {String|Array} 要载入的js文件url,或要载入多个js文件的urls数组。
- * "css": {String|Array} 要载入的css文件url,或要载入多个css文件的urls数组。
- * "html": {String|Array} 要载入的html文件url,或要载入多个html文件的urls数组。
- * }
- * </code></pre>
- * @param {Object} [options] 载入html文件的配置参数。
- * <pre><code class="language-js">options参数格式如下:
- * {
- * "noCache": 是否使用缓存,默认true,
- * "reload": 如果相同路径的html文件已经引入了,是否重新载入,默认为:false
- * "sequence": 当urls参数为数组时,多个html文件是否按数组顺序依次载入,默认为false
- * "dom": 引入html后,要将html内容渲染到的目标dom对象(具体位置由position参数确定),
- * "position": 渲染到的目标dom对象的位置,可以是以下值:'beforebegin' 'afterbegin' 'beforeend'(默认) 'afterend'
- * "module": Object,与此html模板关联的对象。(在下面的例子中详细介绍)
- * "bind": Json,与此html模板关联的Json对象。(在下面的例子中详细介绍)
- * "evalScripts": html模板中通过<script>引入或内嵌的javascript,是否要执行。默认为false
- * "baseUrl": html模板中引用连接的baseUrl,默认为空
- * }
- * </code></pre>
- * @param {Function} [callback] 可选参数,载入成功后的回调方法。
- * @o2syntax
- * o2.loadAll(modules, options, callback);
- * @o2syntax
- * Element.loadAll(modules, options, callback);
- * @see o2.load
- * @see o2.loadCss
- * @see o2.loadHtml
- * @example
- * var html = "<div>{{$.title}}</div>"
- * var json = {"title": "标题"};
- * var node = document.getELementById("content");
- * o2.loadAll({
- * "js": ["file1.js", "file2.js"],
- * "css": ["style.css"],
- * "html": "template.html",
- * }, {"dom": node, "bind": json}, function(){
- * //载入完成后的回调
- * });
- */
- this.o2.loadAll = _loadAll;
- if (window.Element) Element.prototype.loadAll = function (modules, options, callback) {
- var op = (_typeOf(options) === "object") ? options : {};
- var cb = (_typeOf(options) === "function") ? options : callback;
- op.dom = this;
- _loadAll(modules, op, cb);
- };
- var _getIfBlockEnd = function (v) {
- var rex = /(\{\{if\s+)|(\{\{\s*end if\s*\}\})/gmi;
- var rexEnd = /\{\{\s*end if\s*\}\}/gmi;
- var subs = 1;
- while ((match = rex.exec(v)) !== null) {
- var fullMatch = match[0];
- if (fullMatch.search(rexEnd) !== -1) {
- subs--;
- if (subs == 0) break;
- } else {
- subs++
- }
- }
- if (match) return {"codeIndex": match.index, "lastIndex": rex.lastIndex};
- return {"codeIndex": v.length - 1, "lastIndex": v.length - 1};
- }
- var _getEachBlockEnd = function (v) {
- var rex = /(\{\{each\s+)|(\{\{\s*end each\s*\}\})/gmi;
- var rexEnd = /\{\{\s*end each\s*\}\}/gmi;
- var subs = 1;
- while ((match = rex.exec(v)) !== null) {
- var fullMatch = match[0];
- if (fullMatch.search(rexEnd) !== -1) {
- subs--;
- if (subs == 0) break;
- } else {
- subs++;
- }
- }
- if (match) return {"codeIndex": match.index, "lastIndex": rex.lastIndex};
- return {"codeIndex": v.length - 1, "lastIndex": v.length - 1};
- }
- var _parseDataCache = {};
- var _parseHtml = function (str, json, i) {
- var v = str;
- var r = (Math.random() * 1000000).toInt().toString();
- while (_parseDataCache[r]) r = (Math.random() * 1000000).toInt().toString();
- if (i || i===0) {
- _parseDataCache[r] = json;
- v = "<div data-o2-binddataid='" + r + "'>" + str + "</div>";
- }else{
- var regex = new RegExp("\\<[\\s\\S]+\\>");
- if (regex.test(str)){
- if (json.data){
- _parseDataCache[r] = json.data;
- v = str+"<div style='display: none' data-o2-binddata='" + r + "'></div>"
- }
- }
- }
- var rex = /(\{\{\s*)[\s\S]*?(\s*\}\})/gmi;
- var match;
- while ((match = rex.exec(v)) !== null) {
- var fullMatch = match[0];
- var offset = 0;
- //if statement begin
- if (fullMatch.search(/\{\{if\s+/i) !== -1) {
- //找到对应的end if
- var condition = fullMatch.replace(/^\{\{if\s*/i, "");
- condition = condition.replace(/\s*\}\}$/i, "");
- var flag = _jsonText(json, condition, "boolean");
- var tmpStr = v.substring(rex.lastIndex, v.length);
- var endIfIndex = _getIfBlockEnd(tmpStr);
- if (flag) { //if 为 true
- var parseStr = _parseHtml(tmpStr.substring(0, endIfIndex.codeIndex), json);
- var vLeft = v.substring(0, match.index);
- var vRight = v.substring(rex.lastIndex + endIfIndex.lastIndex, v.length);
- v = vLeft + parseStr + vRight;
- offset = parseStr.length - fullMatch.length;
- } else {
- v = v.substring(0, match.index) + v.substring(rex.lastIndex + endIfIndex.lastIndex, v.length);
- offset = 0 - fullMatch.length;
- }
- } else if (fullMatch.search(/\{\{each\s+/) !== -1) { //each statement
- var itemString = fullMatch.replace(/^\{\{each\s*/, "");
- itemString = itemString.replace(/\s*\}\}$/, "");
- var eachValue = _jsonText(json, itemString, "object");
- var tmpEachStr = v.substring(rex.lastIndex, v.length);
- var endEachIndex = _getEachBlockEnd(tmpEachStr);
- var parseEachStr = tmpEachStr.substring(0, endEachIndex.codeIndex);
- var eachResult = "";
- if (eachValue && _typeOf(eachValue) === "array") {
- for (var i = 0; i < eachValue.length; i++) {
- eachValue[i]._ = json;
- eachResult += _parseHtml(parseEachStr, eachValue[i], i);
- }
- var eLeft = v.substring(0, match.index);
- var eRight = v.substring(rex.lastIndex + endEachIndex.lastIndex, v.length);
- v = eLeft + eachResult + eRight;
- offset = eachResult.length - fullMatch.length;
- } else {
- v = v.substring(0, match.index) + v.substring(rex.lastIndex + endEachIndex.lastIndex, v.length);
- offset = 0 - fullMatch.length;
- }
- } else { //text statement
- var text = fullMatch.replace(/^\{\{\s*/, "");
- text = text.replace(/\}\}\s*$/, "");
- var value = _jsonText(json, text);
- offset = value.length - fullMatch.length;
- v = v.substring(0, match.index) + value + v.substring(rex.lastIndex, v.length);
- }
- rex.lastIndex = rex.lastIndex + offset;
- }
- return v;
- };
- var _jsonText = function (json, text, type) {
- try {
- var $ = json;
- var f = eval("(function($){\n return " + text + ";\n})");
- returnValue = f.apply(json, [$]);
- if (returnValue === undefined) returnValue = "";
- if (type === "boolean") return (!!returnValue);
- if (type === "object") return returnValue;
- returnValue = returnValue.toString();
- returnValue = returnValue.replace(/\&/g, "&");
- returnValue = returnValue.replace(/>/g, ">");
- returnValue = returnValue.replace(/</g, "<");
- returnValue = returnValue.replace(/\"/g, """);
- return returnValue || "";
- } catch (e) {
- if (type === "boolean") return false;
- if (type === "object") return null;
- return "";
- }
- };
- o2.bindJson = function (str, json) {
- return _parseHtml(str, json);
- };
- String.prototype.bindJson = function (json) {
- return _parseHtml(this, json);
- };
- var _createComponent = function(o, res){
- o.multitask = false;
- };
- var _loadComponent = function(o, res){
- o.loading = new Promise(function(resolve){
- o2.loadAll(res, {evalScripts:true, url: true}, function(){ resolve(); });
- });
- };
- o2.component = function(name, res){
- o2.xApplication = o2.xApplication || {};
- var names = name.split(".");
- var o = o2.xApplication;
- names.forEach(function(n){
- o = o[n] = o[n] || {};
- });
- if (o2.typeOf(res)==="object"){
- _loadComponent(o, res)
- }else{
- _createComponent(o, res);
- }
- }
- })();
- /** ***** BEGIN LICENSE BLOCK *****
- * |------------------------------------------------------------------------------|
- * | O2OA 活力办公 创意无限 o2.core.js |
- * |------------------------------------------------------------------------------|
- * | Distributed under the AGPL license: |
- * |------------------------------------------------------------------------------|
- * | Copyright © 2018, o2oa.net, o2server.io O2 Team |
- * | All rights reserved. |
- * |------------------------------------------------------------------------------|
- *
- * This file is part of O2OA.
- *
- * O2OA is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * O2OA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Foobar. If not, see <https://www.gnu.org/licenses/>.
- *
- * ***** END LICENSE BLOCK ******/
- (function () {
- var _Class = {
- create: function (options) {
- // var newClass = function() {
- // this.initialize.apply(this, arguments);
- // };
- return _copyPrototype(function () {
- return this.initialize.apply(this, arguments) || this;
- }, options);
- //return newClass;
- }
- };
- var _copyPrototype = function (currentNS, props) {
- if (!props) {
- return currentNS;
- }
- if (!currentNS) {
- return currentNS;
- }
- if ((typeof currentNS).toLowerCase() === "object") {
- for (var prop in props) {
- currentNS[prop] = props[prop];
- }
- }
- if ((typeof currentNS).toLowerCase() === "function") {
- for (var propfun in props) {
- currentNS.prototype[propfun] = props[propfun];
- }
- }
- return currentNS;
- };
- var _loaded = {};
- var _requireJs = function (url, callback, async, compression, module) {
- var key = encodeURIComponent(url);
- if (_loaded[key]) {
- o2.runCallback(callback, "success", [module]);
- return "";
- }
- var jsPath = (compression || !this.o2.session.isDebugger) ? url.replace(/\.js/, ".min.js") : url;
- jsPath = (jsPath.indexOf("?") !== -1) ? jsPath + "&v=" + this.o2.version.v : jsPath + "?v=" + this.o2.version.v;
- if (window.importScripts) {
- window.importScripts(o2.filterUrl(jsPath));
- o2.runCallback(callback, "success", [module]);
- } else {
- var xhr = new Request({
- url: o2.filterUrl(jsPath), async: async, method: "get",
- onSuccess: function () {
- //try{
- _loaded[key] = true;
- o2.runCallback(callback, "success", [module]);
- //}catch (e){
- // o2.runCallback(callback, "failure", [e]);
- //}
- },
- onFailure: function (r) {
- var rex = /lp\/.+\.js/;
- if (rex.test(url)) {
- var zhcnUrl = url.replace(rex, "lp/zh-cn.js");
- if (zhcnUrl !== url) {
- _requireJs(zhcnUrl, callback, async, compression, module)
- } else {
- o2.runCallback(callback, "failure", [r]);
- }
- } else {
- o2.runCallback(callback, "failure", [r]);
- }
- }
- });
- xhr.send();
- }
- };
- var _requireSingle = function (module, callback, async, compression) {
- if (o2.typeOf(module) === "array") {
- _requireAppSingle(module, callback, async, compression);
- } else {
- module = module.replace("MWF.", "o2.");
- var levels = module.split(".");
- if (levels[levels.length - 1] === "*") levels[levels.length - 1] = "package";
- levels.shift();
- var o = o2;
- var i = 0;
- while (o && i < levels.length) {
- o = o[levels[i]];
- i++
- }
- if (!o) {
- var jsPath = this.o2.session.path;
- jsPath += "/" + levels.join("/") + ".js";
- var loadAsync = (async !== false);
- _requireJs(jsPath, callback, loadAsync, compression, module);
- } else {
- o2.runCallback(callback, "success", [module]);
- }
- }
- };
- var _requireSequence = function (fun, module, thisLoaded, thisErrorLoaded, callback, async, compression) {
- var m = module.shift();
- fun(m, {
- "onSuccess": function (m) {
- thisLoaded.push(m);
- o2.runCallback(callback, "every", [m]);
- if (module.length) {
- _requireSequence(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- } else {
- if (thisErrorLoaded.length) {
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- } else {
- o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
- }
- }
- },
- "onFailure": function () {
- thisErrorLoaded.push(module[i]);
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- }
- }, async, compression);
- };
- var _requireDisarray = function (fun, module, thisLoaded, thisErrorLoaded, callback, async, compression) {
- for (var i = 0; i < module.length; i++) {
- fun(module[i], {
- "onSuccess": function (m) {
- thisLoaded.push(m);
- o2.runCallback(callback, "every", [m]);
- if ((thisLoaded.length + thisErrorLoaded.length) === module.length) {
- if (thisErrorLoaded.length) {
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- } else {
- o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
- }
- }
- },
- "onFailure": function () {
- thisErrorLoaded.push(module[i]);
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- }
- }, async, compression);
- }
- };
- var _require = function (module, callback, async, sequence, compression) {
- var type = typeOf(module);
- if (type === "array") {
- var sql = !!sequence;
- var thisLoaded = [];
- var thisErrorLoaded = [];
- if (sql) {
- _requireSequence(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- } else {
- _requireDisarray(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- }
- }
- if (type === "string") {
- _requireSingle(module, callback, async, compression);
- }
- };
- var _requireAppSingle = function (modules, callback, async, compression) {
- var module = modules[0];
- var clazz = modules[1];
- var levels = module.split(".");
- var o = o2.xApplication;
- var i = 0;
- while (o && i < levels.length) {
- o = o[levels[i]];
- i++
- }
- if (o) o = o[clazz || "Main"];
- if (!o) {
- //levels.shift();
- var root = "x_component_" + levels.join("_");
- var clazzName = clazz || "Main";
- var path = "../" + root + "/" + clazzName.replace(/\./g, "/") + ".js";
- var loadAsync = (async !== false);
- _requireJs(path, callback, loadAsync, compression);
- } else {
- o2.runCallback(callback, "success");
- }
- };
- var _requireApp = function (module, clazz, callback, async, sequence, compression) {
- var type = typeOf(module);
- if (type === "array") {
- var sql = !!sequence;
- var thisLoaded = [];
- var thisErrorLoaded = [];
- if (sql) {
- _requireSequence(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- } else {
- _requireDisarray(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- }
- }
- if (type === "string") {
- var modules = [module, clazz];
- _requireAppSingle(modules, callback, async, compression);
- }
- };
- JSON = window.JSON || {};
- var _json = JSON;
- _json.get = function (url, callback, async, nocache) {
- var loadAsync = (async !== false);
- var noJsonCache = (nocache === true);
- if (url.indexOf("config.json") > -1){
- noJsonCache = true;
- }
- url = (url.indexOf("?") !== -1) ? url + "&v=" + o2.version.v : url + "?v=" + o2.version.v;
- var json = null;
- var res = new Request.JSON({
- url: o2.filterUrl(url),
- secure: false,
- method: "get",
- noCache: noJsonCache,
- async: loadAsync,
- withCredentials: true,
- onSuccess: function (responseJSON, responseText) {
- json = responseJSON;
- if (typeOf(callback).toLowerCase() === 'function') {
- callback(responseJSON, responseText);
- } else {
- o2.runCallback(callback, "success", [responseJSON, responseText]);
- }
- }.bind(this),
- onFailure: function (xhr) {
- o2.runCallback(callback, "requestFailure", [xhr]);
- }.bind(this),
- onError: function (text, error) {
- o2.runCallback(callback, "error", [text, error]);
- }.bind(this)
- });
- res.send();
- return json;
- };
- _json.getJsonp = function (url, callback, async, callbackKey) {
- var loadAsync = (async !== false);
- var callbackKeyWord = callbackKey || "callback";
- url = (url.indexOf("?") !== -1) ? url + "&v=" + o2.version.v : url + "?v=" + o2.version.v;
- var res = new Request.JSONP({
- url: o2.filterUrl(url),
- secure: false,
- method: "get",
- noCache: true,
- async: loadAsync,
- callbackKey: callbackKeyWord,
- onSuccess: function (responseJSON, responseText) {
- o2.runCallback(callback, "success", [responseJSON, responseText]);
- }.bind(this),
- onFailure: function (xhr) {
- o2.runCallback(callback, "requestFailure", [xhr]);
- }.bind(this),
- onError: function (text, error) {
- o2.runCallback(callback, "error", [text, error]);
- }.bind(this)
- });
- res.send();
- };
- var _loadLP = function (name) {
- var jsPath = o2.session.path;
- jsPath = jsPath + "/lp/" + name + ".js";
- var r = new Request({
- url: o2.filterUrl(jsPath),
- async: false,
- method: "get",
- onSuccess: function (responseText) {
- try {
- Browser.exec(responseText);
- } catch (e) {
- }
- },
- onFailure: function (xhr) {
- if (name != "zh-cn") {
- _loadLP("zh-cn");
- } else {
- throw "loadLP Error: " + xhr.responseText;
- }
- }
- });
- r.send();
- };
- var _cacheUrls = (Browser.name == "ie") ? [] : [
- /jaxrs\/form\/workorworkcompleted\/.+/ig,
- /jaxrs\/form\/.+/ig,
- /jaxrs\/script\/.+\/app\/.+\/imported/ig,
- /jaxrs\/script\/portal\/.+\/name\/.+\/imported/ig,
- /jaxrs\/script\/.+\/application\/.+\/imported/ig,
- /jaxrs\/page\/.+\/portal\/.+/ig,
- /jaxrs\/document\/.+/ig,
- /jaxrs\/applicationdict\/.+/ig,
- /jaxrs\/custom\/.+/ig,
- /jaxrs\/definition\/idea.+/ig,
- /jaxrs\/distribute\/assemble\/source\/.+/ig,
- ];
- // _restful_bak = function(method, address, data, callback, async, withCredentials, cache){
- // var loadAsync = (async !== false);
- // var credentials = (withCredentials !== false);
- // address = (address.indexOf("?")!==-1) ? address+"&v="+o2.version.v : address+"?v="+o2.version.v;
- // //var noCache = cache===false;
- // var noCache = !cache;
- //
- //
- // //if (Browser.name == "ie")
- // if (_cacheUrls.length){
- // for (var i=0; i<_cacheUrls.length; i++){
- // _cacheUrls[i].lastIndex = 0;
- // if (_cacheUrls[i].test(address)){
- // noCache = false;
- // break;
- // }
- // }
- // }
- // //var noCache = false;
- // var res = new Request.JSON({
- // url: o2.filterUrl(address),
- // secure: false,
- // method: method,
- // emulation: false,
- // noCache: noCache,
- // async: loadAsync,
- // withCredentials: credentials,
- // onSuccess: function(responseJSON, responseText){
- // // var xToken = this.getHeader("authorization");
- // // if (!xToken) xToken = this.getHeader("x-token");
- // var xToken = this.getHeader("x-token");
- // if (xToken){
- // if (window.layout){
- // if (!layout.session) layout.session = {};
- // layout.session.token = xToken;
- // }
- // }
- // o2.runCallback(callback, "success", [responseJSON]);
- // },
- // onFailure: function(xhr){
- // o2.runCallback(callback, "requestFailure", [xhr]);
- // }.bind(this),
- // onError: function(text, error){
- // o2.runCallback(callback, "error", [text, error]);
- // }.bind(this)
- // });
- //
- // res.setHeader("Content-Type", "application/json; charset=utf-8");
- // res.setHeader("Accept", "text/html,application/json,*/*");
- // if (window.layout) {
- // if (layout["debugger"]){
- // res.setHeader("x-debugger", "true");
- // }
- // if (layout.session && layout.session.user){
- // if (layout.session.user.token) {
- // res.setHeader("x-token", layout.session.user.token);
- // res.setHeader("authorization", layout.session.user.token);
- // }
- // }
- // }
- // //Content-Type application/x-www-form-urlencoded; charset=utf-8
- // res.send(data);
- // return res;
- // };
- var _resGetQueue = {};
- var _checkRestful = function(address, callback){
- if (_resGetQueue[address]){
- var resPromise = _resGetQueue[address];
- var p = new Promise(function(resolve, reject){
- resPromise.then(function(){
- resolve(resPromise.json);
- }, function(){
- reject(resPromise.err);
- });
- });
- if (_resGetQueue[address].res) p.res = _resGetQueue[address].res;
- if (_resGetQueue[address].actionWorker) p.actionWorker = _resGetQueue[address].actionWorker;
- return p;
- }
- return false
- }
- _restful = function (method, address, data, callback, async, withCredentials, cache) {
- var p = null;
- if (method.toLowerCase()==="get" && async!==false){
- p = _checkRestful(address, callback);
- //if (p) return p;
- }
- var _addr = address;
- var loadAsync = (async !== false);
- var credentials = (withCredentials !== false);
- address = (address.indexOf("?") !== -1) ? address + "&v=" + o2.version.v : address + "?v=" + o2.version.v;
- //var noCache = cache===false;
- var noCache = !cache;
- //if (Browser.name == "ie")
- if (_cacheUrls.length) {
- for (var i = 0; i < _cacheUrls.length; i++) {
- _cacheUrls[i].lastIndex = 0;
- if (_cacheUrls[i].test(address)) {
- noCache = false;
- break;
- }
- }
- }
- var useWebWorker = (window.layout && layout.config && layout.config.useWebWorker);
- //var noCache = false;
- if (!loadAsync || !useWebWorker) {
- var res;
- if (!p) p = new Promise(function (resolve, reject) {
- res = new Request.JSON({
- url: o2.filterUrl(address),
- secure: false,
- method: method,
- emulation: false,
- noCache: noCache,
- async: loadAsync,
- withCredentials: credentials,
- onSuccess: function (responseJSON, responseText) {
- // var xToken = this.getHeader("authorization");
- // if (!xToken) xToken = this.getHeader("x-token");
- _resGetQueue[_addr] = null;
- delete _resGetQueue[_addr];
- var xToken = this.getHeader(o2.tokenName);
- if (xToken) {
- if (window.layout) {
- if (!layout.session) layout.session = {};
- layout.session.token = xToken;
- }
- if (layout.config && layout.config.sessionStorageEnable && window.sessionStorage) window.sessionStorage.setItem("o2LayoutSessionToken", xToken);
- }
- if (!loadAsync){
- var r = o2.runCallback(callback, "success", [responseJSON], null);
- resolve(r || responseJSON);
- }else{
- resolve(responseJSON);
- }
- //resolve(responseJSON);
- //return o2.runCallback(callback, "success", [responseJSON],null, resolve);
- },
- onFailure: function (xhr) {
- _resGetQueue[_addr] = null;
- delete _resGetQueue[_addr];
- if (!loadAsync){
- var r = o2.runCallback(callback, "failure", [xhr, "", ""], null);
- reject((r) ? r : {"xhr": xhr, "text": "", "error": "error"});
- }else{
- reject({"xhr": xhr, "text": "", "error": "error"});
- }
- }.bind(this),
- onError: function (text, error) {
- _resGetQueue[_addr] = null;
- delete _resGetQueue[_addr];
- if (!loadAsync){
- var r = o2.runCallback(callback, "failure", [text, error], null);
- reject((r) ? r : {"xhr": xhr, "text": text, "error": "error"});
- }else{
- reject({"xhr": xhr, "text": text, "error": "error"});
- }
- }.bind(this),
- onComplete: function(){
- _resGetQueue[_addr] = null;
- delete _resGetQueue[_addr];
- },
- onCancel: function(){
- _resGetQueue[_addr] = null;
- delete _resGetQueue[_addr];
- }
- });
- res.setHeader("Content-Type", "application/json; charset=utf-8");
- res.setHeader("Accept", "text/html,application/json,*/*");
- res.setHeader("Accept-Language", o2.languageName);
- if (window.layout) {
- if (layout["debugger"]) {
- res.setHeader("x-debugger", "true");
- }
- var token = (layout.config && layout.config.sessionStorageEnable && window.sessionStorage) ? window.sessionStorage.getItem("o2LayoutSessionToken") : "";
- if (!token) {
- if (layout.session && (layout.session.user || layout.session.token)) {
- token = layout.session.token;
- if (!token && layout.session.user && layout.session.user.token) token = layout.session.user.token;
- }
- }
- if (token) {
- //res.setHeader(o2.tokenName, token);
- res.setHeader("Authorization", token);
- }
- }
- res.send(data);
- }.bind(this)).catch(function (err) {
- throw err;
- });
- // .then(function (responseJSON) {
- //
- // _resGetQueue[address].events.each(function(e){
- // var r = o2.runCallback(e.callback, "success", [responseJSON], null);
- // if (e.promise){
- // e.promise
- // }
- // });
- //
- // return responseJSON;
- // }, function(err){
- // var r = o2.runCallback(callback, "failure", [xhr, "", ""], null);
- // return r || err;
- // }).catch(function (err) {
- // throw err;
- // //return Promise.reject(err);
- // });
- var oReturn = p;
- //oReturn.res = res;
- var resPromise = Promise.resolve(oReturn).then(function(json){
- if (!loadAsync) return json;
- resPromise.json = json;
- var r = o2.runCallback(callback, "success", [json], null);
- if (r) return r;
- }, function(err){
- if (!loadAsync) return err;
- resPromise.err = err;
- var r = o2.runCallback(callback, "failure", [err.xhr, err.text, err.error], null);
- if (r) return Promise.reject(r);
- return Promise.reject(err);
- }).catch(function (err) { throw err;});
- resPromise.res = res;
- if (loadAsync) _resGetQueue[_addr] = resPromise;
- return resPromise;
- } else {
- var workerMessage = {
- method: method,
- noCache: noCache,
- loadAsync: loadAsync,
- credentials: credentials,
- address: o2.filterUrl(address),
- body: data,
- debug: (window.layout && layout["debugger"]),
- token: (window.layout && layout.session && layout.session.user) ? layout.session.user.token : "",
- tokenName: o2.tokenName
- }
- var actionWorker = new Worker("../o2_core/o2/actionWorker.js");
- if (!p) p = new Promise(function (s, f) {
- actionWorker.onmessage = function (e) {
- _resGetQueue[_addr] = null;
- delete _resGetQueue[_addr];
- result = e.data;
- if (result.type === "done") {
- var xToken = result.data.xToken;
- if (xToken) {
- if (window.layout) {
- if (!layout.session) layout.session = {};
- layout.session.token = xToken;
- }
- }
- s(result.data);
- //o2.runCallback(callback, "success", [result.data], null, s);
- } else {
- f(result.data);
- //o2.runCallback(callback, "failure", [result.data], null, f);
- }
- actionWorker.terminate();
- }
- actionWorker.postMessage(workerMessage);
- }.bind(this));
- // p = p.then(function (data) {
- // return o2.runCallback(callback, "success", [data], null);
- // }, function (data) {
- // return o2.runCallback(callback, "failure", [data], null);
- // });
- //var oReturn = (callback.success && callback.success.addResolve) ? callback.success : callback;
- var oReturn = p;
- //oReturn.actionWorker = actionWorker;
- var resPromise = Promise.resolve(oReturn).then(function(json){
- resPromise.json = json;
- var r = o2.runCallback(callback, "success", [json], null);
- if (r) return r;
- }, function(err){
- resPromise.err = err;
- var r = o2.runCallback(callback, "failure", [err], null);
- if (r) return r;
- }).catch(function (err) { throw err;});
- resPromise.actionWorker = actionWorker;
- _resGetQueue[_addr] = resPromise;
- return resPromise;
- //return oReturn;
- //return callback;
- }
- //return res;
- };
- var _release = function (o) {
- var type = typeOf(o);
- switch (type) {
- case "object":
- for (var k in o) {
- //if (o[k] && o[k].destroy) o[k].destroy();
- o[k] = null;
- }
- break;
- case "array":
- for (var i = 0; i < o.length; i++) {
- _release(o[i]);
- if (o[i]) o[i] = null;
- }
- break;
- }
- };
- var _defineProperties = Object.defineProperties || function (obj, properties) {
- function convertToDescriptor(desc) {
- function hasProperty(obj, prop) {
- return Object.prototype.hasOwnProperty.call(obj, prop);
- }
- function isCallable(v) {
- // NB: modify as necessary if other values than functions are callable.
- return typeof v === "function";
- }
- if (typeof desc !== "object" || desc === null)
- throw new TypeError("bad desc");
- var d = {};
- if (hasProperty(desc, "enumerable"))
- d.enumerable = !!desc.enumerable;
- if (hasProperty(desc, "configurable"))
- d.configurable = !!desc.configurable;
- if (hasProperty(desc, "value"))
- d.value = desc.value;
- if (hasProperty(desc, "writable"))
- d.writable = !!desc.writable;
- if (hasProperty(desc, "get")) {
- var g = desc.get;
- if (!isCallable(g) && typeof g !== "undefined")
- throw new TypeError("bad get");
- d.get = g;
- }
- if (hasProperty(desc, "set")) {
- var s = desc.set;
- if (!isCallable(s) && typeof s !== "undefined")
- throw new TypeError("bad set");
- d.set = s;
- }
- if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
- throw new TypeError("identity-confused descriptor");
- return d;
- }
- if (typeof obj !== "object" || obj === null)
- throw new TypeError("bad obj");
- properties = Object(properties);
- var keys = Object.keys(properties);
- var descs = [];
- for (var j = 0; j < keys.length; j++)
- descs.push([keys[j], convertToDescriptor(properties[keys[j]])]);
- for (var i = 0; i < descs.length; i++) {
- if (Object.defineProperty && (Browser.name == "ie" && Browser.version != 8)) {
- Object.defineProperty(obj, descs[i][0], descs[i][1]);
- } else {
- if (descs[i][1].value) obj[descs[i][0]] = descs[i][1].value;
- if (descs[i][1].get) obj["get" + descs[i][0].capitalize()] = descs[i][1].get;
- if (descs[i][1].set) obj["set" + descs[i][0].capitalize()] = descs[i][1].set;
- }
- }
- return obj;
- };
- if (!Array.prototype.findIndex) {
- Object.defineProperty(Array.prototype, 'findIndex', {
- value: function (predicate) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- var o = Object(this);
- var len = o.length >>> 0;
- if (typeof predicate !== 'function') {
- throw new TypeError('predicate must be a function');
- }
- var thisArg = arguments[1];
- var k = 0;
- while (k < len) {
- var kValue = o[k];
- if (predicate.call(thisArg, kValue, k, o)) {
- return k;
- }
- k++;
- }
- return -1;
- }
- });
- }
- if (!Array.prototype.find) {
- Object.defineProperty(Array.prototype, 'find', {
- value: function (predicate) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- var o = Object(this);
- var len = o.length >>> 0;
- if (typeof predicate !== 'function') {
- throw new TypeError('predicate must be a function');
- }
- var thisArg = arguments[1];
- var k = 0;
- while (k < len) {
- var kValue = o[k];
- if (predicate.call(thisArg, kValue, k, o)) {
- return kValue;
- }
- k++;
- }
- return undefined;
- }
- });
- }
- var _txt = function (v) {
- if (typeof v !== "string") return v;
- var t = v.replace(/\</g, "<");
- t = t.replace(/\>/g, ">");
- return t;
- };
- var _dtxt = function (v) {
- if (typeof v !== "string") return v;
- var t = v.replace(/</g, "<");
- t = t.replace(/>/g, ">");
- return t;
- };
- this.o2.Class = _Class;
- this.o2.require = _require;
- this.o2.requireApp = _requireApp;
- this.o2.JSON = _json;
- this.o2.loadLP = _loadLP;
- this.o2.restful = _restful;
- this.o2.release = _release;
- this.o2.defineProperties = _defineProperties;
- this.o2.txt = _txt;
- this.o2.dtxt = _dtxt;
- Object.repeatArray = function (o, count) {
- var arr = [];
- for (var i = 0; i < count; i++) {
- arr.push(o)
- }
- return arr;
- }
- /**
- * @summary 从服务器获取当前时间。
- * @function getDateFromServer
- * @memberOf o2
- * @param {Boolean|Function} [async|callback] 可选,如果传入true或一个Function:表示异步调用此方法,传入的function为回调方法。如果省略此参数或传入false,则为同步方法
- * @return {Date|Promise} 同步调用时,返回获取到的时间Date;异步调用时,返回Promise。
- * @o2syntax
- * o2.getDateFromServer(async);
- * @o2syntax
- * Date.getFromServer(async);
- * @example
- * //同步获取服务器时间
- * var d = o2.getDateFromServer();
- * //或者
- * var d = Date.getFromServer();
- *
- * //通过回调方法异步获取服务器时间
- * o2.getDateFromServer(function(d){
- * console.log(d); //从服务器获取的当前时间
- * });
- * //或者
- * Date.getFromServer(function(d){
- * console.log(d); //从服务器获取的当前时间
- * });
- *
- * //通过Promise异步获取服务器时间
- * o2.getDateFromServer(true).then((d)=>{
- * console.log(d); //从服务器获取的当前时间
- * });
- * //或者
- * Date.getFromServer(true).then((d)=>{
- * console.log(d); //从服务器获取的当前时间
- * });
- */
- this.o2.getDateFromServer = Date.getFromServer = function (async) {
- var d;
- // var cb = ((async && o2.typeOf(async) == "function") ? async : null) || function (json) {
- // //var cb = function(json){
- // d = Date.parse(json.data.serverTime);
- // return d;
- // };
- var cb = function(json){
- d = Date.parse(json.data.serverTime);
- return d;
- }
- var promise = o2.Actions.get("x_program_center").echo(cb, null, !!async);
- if (async && o2.typeOf(async) == "function"){
- return promise.then(async);
- }
- return (!!async) ? promise : d;
- };
- var _promiseAll = function (p) {
- if (o2.typeOf(p) == "array") {
- if (p.some(function (e) {
- return (e && o2.typeOf(e.then) == "function")
- })) {
- return Promise.all(p);
- } else {
- return {
- "then": function (s) {
- if (s) {
- var r = s(p);
- return (r && r.then && o2.typeOf(r.then) == "function") ? r : this;
- }
- return this;
- }
- };
- }
- } else {
- if (p && o2.typeOf(p.then) == "function") {
- return Promise.resolve(p);
- } else {
- return {
- "then": function (s) {
- if (s) {
- var r = s(p);
- return (r && r.then && o2.typeOf(r.then) == "function") ? r : this;
- }
- return this;
- }
- };
- //return new Promise(function(s){s(p); return this;});
- }
- }
- // var method = (o2.typeOf(p)=="array") ? "all" : "resolve";
- // return Promise[method](p);
- }
- o2.promiseAll = _promiseAll;
- })();
- o2.defer = function(fn, timer, bind, pars){
- if (fn.timerId) clearTimeout(fn.timerId);
- fn.timerId = setTimeout(function(){
- fn.timerId = null;
- fn.apply((bind || this), pars);
- }, timer);
- }
- o2.core = true;
- /** ***** BEGIN LICENSE BLOCK *****
- * |------------------------------------------------------------------------------|
- * | O2OA 活力办公 创意无限 o2.more.js |
- * |------------------------------------------------------------------------------|
- * | Distributed under the AGPL license: |
- * |------------------------------------------------------------------------------|
- * | Copyright © 2018, o2oa.net, o2server.io O2 Team |
- * | All rights reserved. |
- * |------------------------------------------------------------------------------|
- *
- * This file is part of O2OA.
- *
- * O2OA is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * O2OA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Foobar. If not, see <https://www.gnu.org/licenses/>.
- *
- * ***** END LICENSE BLOCK ******/
- (function () {
- o2.getCenterPosition = function (el, width, height) {
- var elPositon = $(el).getPosition();
- var elSize = $(el).getSize();
- var node = $("layout");
- var size = (node) ? $(node).getSize() : $(document.body).getSize();
- var top = (elPositon.y + elSize.y) / 2 - (height / 2);
- var left = (elPositon.x + elSize.x) / 2 - (width / 2);
- if ((left + width) > size.x) {
- left = size.x - width - 10;
- }
- if ((top + height) > size.y) {
- top = size.y - height - 10;
- }
- return {"x": left, "y": top};
- };
- o2.getMarkSize = function (node) {
- var size;
- if (!node) {
- size = $(document.body).getSize();
- var winSize = $(window).getSize();
- var height = size.y;
- var width = size.x;
- if (height < winSize.y) height = winSize.y;
- if (width < winSize.x) width = winSize.x;
- return {x: size.x, y: height};
- } else {
- size = $(node).getSize();
- return {x: size.x, y: size.y};
- }
- };
- o2.json = function (jsonString, fun) {
- var obj = JSON.decode(jsonString);
- var p = fun.split(".");
- var tmp = obj;
- p.each(function (item) {
- if (item.indexOf("[") !== -1) {
- var x = item.split("[");
- var i = parseInt(x[1].substr(0, x[1].indexOf("]")));
- tmp = tmp[x[0]][i];
- } else {
- tmp = tmp[item];
- }
- });
- return tmp;
- };
- o2.getHTMLTemplate = function (url, callback, async) {
- var loadAsync = (async !== false);
- var res = new Request.HTML({
- url: url,
- async: loadAsync,
- method: "get",
- onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
- o2.runCallback(callback, "success", [responseTree, responseElements, responseHTML, responseJavaScript]);
- }.bind(this),
- onFailure: function (xhr) {
- o2.runCallback(callback, "requestFailure", [xhr]);
- }
- });
- res.send();
- };
- o2.getRequestText = function (url, callback, async) {
- var loadAsync = (async !== false);
- url = (url.indexOf("?") !== -1) ? url + "&v=" + o2.version.v : url + "?v=" + o2.version.v;
- var res = new Request({
- url: url,
- async: loadAsync,
- method: "get",
- onSuccess: function (responseText, responseXML) {
- o2.runCallback(callback, "success", [responseText, responseXML]);
- }.bind(this),
- onFailure: function (xhr) {
- o2.runCallback(callback, "requestFailure", [xhr]);
- }
- });
- res.send();
- };
- o2.encodeJsonString = function (str) {
- var tmp = [str];
- var dataStr = (JSON.encode(tmp));
- return dataStr.substr(2, dataStr.length - 4);
- };
- o2.decodeJsonString = function (str) {
- var tmp = "[\"" + str + "\"]";
- var dataObj = (JSON.decode(tmp));
- return dataObj[0];
- };
- o2.getTextSize = function (text, styles) {
- var tmpSpan = new Element("span", {
- "text": text,
- "styles": styles
- }).inject($(document.body));
- var size = tmpSpan.getSize();
- tmpSpan.destroy();
- return size;
- };
- o2.getCenter = function (size, target, offset) {
- if (!target) target = document.body;
- var targetSize = target.getSize();
- var targetPosition = target.getPosition(offset);
- var targetScroll = target.getScroll();
- var x = targetSize.x / 2;
- var y = targetSize.y / 2;
- x = x - (size.x / 2);
- y = y - (size.y / 2);
- x = x + targetPosition.x;
- y = y + targetPosition.y;
- x = x + targetScroll.x;
- y = y + targetScroll.y;
- return {"x": x, "y": y};
- };
- o2.getEPointer = function (e) {
- var x = 0;
- var y = 0;
- if (typeOf(e) == "element") {
- var position = e.getPosition(this.content);
- x = position.x;
- y = position.y;
- } else {
- if (Browser.name == "firefox") {
- x = parseFloat(e.event.clientX || e.event.x);
- y = parseFloat(e.event.clientY || e.event.y);
- } else {
- x = parseFloat(e.event.x);
- y = parseFloat(e.event.y);
- }
- if (e.target) {
- var position = e.target.getPosition(this.content);
- x = position.x;
- y = position.y;
- }
- // }
- }
- return {"x": x, "y": y};
- };
- o2.getParent = function (node, tag) {
- var pNode = node.parentElement;
- while (pNode && pNode.tagName.toString().toLowerCase() !== tag.toString().toLowerCase()) {
- pNode = pNode.parentElement;
- }
- return pNode;
- };
- o2.getOffset = function (evt) {
- if (Browser.name === "firefox") {
- return {
- "offsetX": evt.layerX,
- "offsetY": evt.layerY
- };
- } else {
- return {
- "offsetX": evt.offsetX,
- "offsetY": evt.offsetY
- }
- }
- };
- /**
- * @summary 对页面进行缩放。
- * @function zoom
- * @memberOf o2
- * @param {Number} [scale] 缩放的比例。1表示原始大小
- * @o2syntax
- * o2.zoom(scale);
- * @example
- * //将页面放大到150%大小
- * o2.zoom(1.5);
- */
- o2.zoom = function (scale) {
- if (!layout) layout = {};
- if (layout && !layout.userLayout) layout.userLayout = {};
- layout.userLayout.scale = scale;
- // var s = (1 / layout.userLayout.scale) * 100;
- // var p = s + "%";
- // document.id(document.documentElement).setStyles({
- // "transform": "scale(" + layout.userLayout.scale + ")",
- // "transform-origin": "0 0",
- // "width": p,
- // "height": p
- // });
- document.body.style.zoom = scale;
- if (layout.desktop){
- if (layout.desktop.resizeHeight) layout.desktop.resizeHeight();
- }
- };
- if (String.implement) String.implement({
- "getAllIndexOf": function (str) {
- var idxs = [];
- var idx = this.indexOf(str);
- while (idx !== -1) {
- idxs.push(idx);
- idx = this.indexOf(str, idx + 1);
- }
- return idxs;
- }
- });
- if (Array.implement) Array.implement({
- "trim": function () {
- var arr = [];
- this.each(function (v) {
- if (v) arr.push(v);
- });
- return arr;
- },
- "isIntersect": function (arr) {
- return this.some(function (item) {
- return (arr.indexOf(item) !== -1);
- })
- },
- "add": function(newKey, newValue, overwrite){
- if (arguments.length<2){
- this[this.length] = newKey;
- //this.push(newKey);
- }else{
- if (o2.typeOf(newKey)=="number"){
- if (newKey<this.length){
- if (overwrite) this[newKey] = newValue;
- }else if (newKey==this.length){
- //this.push(newValue);
- this[this.length] = newValue;
- }
- }
- }
- }
- });
- if (!Array.prototype.find) {
- if (Array.implement) Array.implement({
- "find": this.find || function (callback, thisArg) {
- for (var i = 0; i < this.length; i++) {
- if (callback.apply((thisArg || this), this[i], i, this)) {
- return this[i];
- }
- }
- return undefined;
- }
- });
- }
- var styleString = Element.getComputedStyle;
- function styleNumber(element, style) {
- return styleString(element, style).toInt() || 0;
- }
- function topBorder(element) {
- return styleNumber(element, 'border-top-width');
- }
- function leftBorder(element) {
- return styleNumber(element, 'border-left-width');
- }
- function isBody(element) {
- return (/^(?:body|html)$/i).test(element.tagName);
- }
- var heightComponents = ['height', 'paddingTop', 'paddingBottom', 'borderTopWidth', 'borderBottomWidth'],
- widthComponents = ['width', 'paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'];
- var svgCalculateSize = function (el) {
- var gCS = window.getComputedStyle(el),
- bounds = {x: 0, y: 0};
- heightComponents.each(function (css) {
- bounds.y += parseFloat(gCS[css]);
- });
- widthComponents.each(function (css) {
- bounds.x += parseFloat(gCS[css]);
- });
- return bounds;
- };
- [Document, Window].invoke('implement', {
- getSize: function () {
- var doc = this.getDocument();
- doc = ((!doc.compatMode || doc.compatMode == 'CSS1Compat') && (!layout || !layout.userLayout || !layout.userLayout.scale || layout.userLayout.scale == 1)) ? doc.html : doc.body;
- return {x: doc.clientWidth, y: doc.clientHeight};
- },
- });
- if (window.Element && Element.implement) Element.implement({
- "isIntoView": function () {
- // var pNode = this.getParent();
- // while (pNode && ((pNode.getScrollSize().y-(pNode.getComputedSize().height+1)<=0) || pNode.getStyle("overflow")==="visible")) pNode = pNode.getParent();
- //
- var pNode = this.getParentSrcollNode();
- if (!pNode) pNode = document.body;
- var size = pNode.getSize();
- var srcoll = pNode.getScroll();
- var p = (pNode == window) ? {"x": 0, "y": 0} : this.getPosition(pNode);
- var nodeSize = this.getSize();
- //return (p.x-srcoll.x>=0 && p.y-srcoll.y>=0) && (p.x+nodeSize.x<size.x+srcoll.x && p.y+nodeSize.y<size.y+srcoll.y);
- return (p.x - srcoll.x >= 0 && p.y >= 0) && (p.x + nodeSize.x < size.x + srcoll.x && p.y + nodeSize.y < size.y)
- },
- "appendHTML": function (html, where) {
- if (this.insertAdjacentHTML) {
- var whereText = "beforeEnd";
- if (where === "before") whereText = "beforeBegin";
- if (where === "after") whereText = "afterEnd";
- if (where === "bottom") whereText = "beforeEnd";
- if (where === "top") whereText = "afterBegin";
- this.insertAdjacentHTML(whereText, html);
- } else {
- if (where === "bottom") this.innerHTML = this.innerHTML + html;
- if (where === "top") this.innerHTML = html + this.innerHTML;
- }
- },
- "positionTo": function (x, y) {
- var left = x.toFloat();
- var top = y.toFloat();
- var offsetNode = this.getOffsetParent();
- if (offsetNode) {
- var offsetPosition = offsetNode.getPosition();
- left = left - offsetPosition.x;
- top = top - offsetPosition.y;
- }
- this.setStyles({"top": top, "left": left});
- return this;
- },
- "getBorder": function () {
- var positions = ["top", "left", "right", "bottom"];
- var styles = ["color", "style", "width"];
- var obj = {};
- positions.each(function (position) {
- styles.each(function (style) {
- var key = "border-" + position + "-" + style;
- obj[key] = this.getStyle(key);
- }.bind(this));
- }.bind(this));
- return obj;
- },
- "isOutside": function (e) {
- var elementCoords = this.getCoordinates();
- var targetCoords = this.getCoordinates();
- if (((e.page.x < elementCoords.left || e.page.x > (elementCoords.left + elementCoords.width)) ||
- (e.page.y < elementCoords.top || e.page.y > (elementCoords.top + elementCoords.height))) &&
- ((e.page.x < targetCoords.left || e.page.x > (targetCoords.left + targetCoords.width)) ||
- (e.page.y < targetCoords.top || e.page.y > (targetCoords.top + targetCoords.height)))) return true;
- return false;
- },
- "getAbsolutePosition": function () {
- var styleLeft = 0;
- var styleTop = 0;
- var node = this;
- styleLeft = node.offsetLeft;
- styleTop = node.offsetTop;
- node = node.parentElement;
- while (node && node.tagName.toString().toLowerCase() !== "body") {
- styleLeft += node.offsetLeft;
- styleTop += node.offsetTop;
- node = node.offsetParent;
- }
- return {x: styleLeft, y: styleTop};
- },
- "tweenScroll": function (to, time) {
- if (!this.tweenScrollQueue) {
- this.tweenScrollQueue = [];
- }
- if (this.tweenScrollQueue.length) {
- this.tweenScrollQueue.push(to);
- } else {
- this.tweenScrollQueue.push(to);
- this.doTweenScrollQueue(time);
- }
- },
- "doTweenScrollQueue": function (time) {
- if (this.tweenScrollQueue.length) {
- var i = this.tweenScrollQueue.length;
- var to = this.tweenScrollQueue[this.tweenScrollQueue.length - 1];
- var scroll = this.getScroll();
- var dy = to - scroll.y;
- var step = dy / time;
- var count = 0;
- var move = 0;
- var id = window.setInterval(function () {
- this.scrollTo(0, scroll.y + count * step);
- count++;
- if (count > time) {
- window.clearInterval(id);
- for (var x = 1; x <= i; x++) this.tweenScrollQueue.shift();
- if (this.tweenScrollQueue.length) this.doTweenScrollQueue(time);
- }
- }.bind(this), 1);
- }
- },
- "isPointIn": function (px, py, offX, offY, el) {
- if (!offX) offX = 0;
- if (!offY) offY = 0;
- var position = this.getPosition(el);
- var size = this.getSize();
- return (position.x - offX <= px && position.x + size.x + offX >= px && position.y - offY <= py && position.y + size.y + offY >= py);
- },
- "isInPointInRect": function (sx, sy, ex, ey) {
- var position = this.getPosition();
- var size = this.getSize();
- var p1 = {"x": position.x, "y": position.y};
- var p2 = {"x": position.x + size.x, "y": position.y};
- var p3 = {"x": position.x + size.x, "y": position.y + size.y};
- var p4 = {"x": position.x, "y": position.y + size.y};
- var sp = {"x": Math.min(sx, ex), "y": Math.min(sy, ey)};
- var ep = {"x": Math.max(sx, ex), "y": Math.max(sy, ey)};
- if (p1.x >= sp.x && p1.y >= sp.y && p1.x <= ep.x && p1.y <= ep.y) return true;
- if (p2.x >= sp.x && p2.y >= sp.y && p2.x <= ep.x && p2.y <= ep.y) return true;
- if (p3.x >= sp.x && p3.y >= sp.y && p3.x <= ep.x && p3.y <= ep.y) return true;
- if (p4.x >= sp.x && p4.y >= sp.y && p4.x <= ep.x && p4.y <= ep.y) return true;
- if (p3.x >= sp.x && p3.y >= sp.y && p1.x <= sp.x && p1.y <= sp.y) return true;
- if (p3.x >= ep.x && p3.y >= ep.y && p1.x <= ep.x && p1.y <= ep.y) return true;
- if (p1.x <= sp.x && p2.x >= sp.x && p1.y >= sp.y && p4.y <= ep.y) return true;
- if (p1.y <= sp.y && p4.y >= sp.y && p1.x >= sp.x && p2.x <= ep.x) return true;
- return false;
- },
- "isOverlap": function (node) {
- var p = node.getPosition();
- var s = node.getSize();
- return this.isInPointInRect(p.x, p.y, p.x + s.x, p.y + s.y);
- },
- "getUsefulSize": function () {
- var size = this.getSize();
- var borderLeft = this.getStyle("border-left").toInt();
- var borderBottom = this.getStyle("border-bottom").toInt();
- var borderTop = this.getStyle("border-top").toInt();
- var borderRight = this.getStyle("border-right").toInt();
- var paddingLeft = this.getStyle("padding-left").toInt();
- var paddingBottom = this.getStyle("padding-bottom").toInt();
- var paddingTop = this.getStyle("padding-top").toInt();
- var paddingRight = this.getStyle("padding-right").toInt();
- var x = size.x - paddingLeft - paddingRight;
- var y = size.y - paddingTop - paddingBottom;
- return {"x": x, "y": y};
- },
- "clearStyles": function (isChild) {
- this.removeProperty("style");
- if (isChild) {
- var subNode = this.getFirst();
- while (subNode) {
- subNode.clearStyles(isChild);
- subNode = subNode.getNext();
- }
- }
- },
- "maskIf": function (styles, click) {
- var style = {
- "background-color": "#666666",
- "opacity": 0.4,
- "z-index": 100
- };
- if (styles) {
- style = Object.merge(style, styles);
- }
- var position = this.getPosition(this.getOffsetParent());
- this.mask({
- "destroyOnHide": true,
- "style": style,
- "useIframeShim": true,
- "iframeShimOptions": {"browsers": true},
- "onShow": function () {
- this.shim.shim.setStyles({
- "opacity": 0,
- "top": "" + position.y + "px",
- "left": "" + position.x + "px"
- });
- },
- "onClick": click
- });
- },
- "scrollIn": function (where) {
- var wh = (where) ? where.toString().toLowerCase() : "center";
- if (Browser.name == "ie" || Browser.name == "safari") {
- var scrollNode = this.getParentSrcollNode();
- var scrollFx = new Fx.Scroll(scrollNode);
- var scroll = scrollNode.getScroll();
- var size = scrollNode.getSize();
- var thisSize = this.getComputedSize();
- var p = this.getPosition(scrollNode);
- if (wh == "start") {
- var top = 0;
- scrollFx.start(scroll.x, p.y - top + scroll.y);
- } else if (wh == "end") {
- var bottom = size.y - thisSize.totalHeight;
- scrollFx.start(scroll.x, p.y - bottom + scroll.y);
- } else {
- var center = size.y / 2 - thisSize.totalHeight / 2;
- scrollFx.start(scroll.x, p.y - center + scroll.y);
- }
- } else {
- if (wh !== "start" && wh !== "end") wh = "center"
- this.scrollIntoView({"behavior": "smooth", "block": wh, "inline": "nearest"});
- }
- },
- scrollToNode: function (el, where) {
- var scrollSize = this.getScrollSize();
- if (!scrollSize.y) return true;
- var wh = (where) ? where.toString().toLowerCase() : "bottom";
- var node = $(el);
- var size = node.getComputedSize();
- var p = node.getPosition(this);
- var thisSize = this.getComputedSize();
- var scroll = this.getScroll();
- if (wh === "top") {
- var n = (p.y - thisSize.computedTop);
- if (n < 0) this.scrollTo(scroll.x, scroll.y + n);
- n = (size.totalHeight + p.y - thisSize.computedTop) - thisSize.height;
- if (n > 0) this.scrollTo(scroll.x, scroll.y + n);
- } else {
- var n = (size.totalHeight + p.y - thisSize.computedTop) - thisSize.height;
- if (n > 0) this.scrollTo(scroll.x, scroll.y + n);
- n = p.y - thisSize.computedTop;
- if (n < 0) this.scrollTo(scroll.x, scroll.y + n);
- }
- },
- "getInnerStyles": function () {
- var styles = {};
- style = this.get("style");
- if (style) {
- var styleArr = style.split(/\s*\;\s*/g);
- styleArr.each(function (s) {
- if (s) {
- var sarr = s.split(/\s*\:\s*/g);
- styles[sarr[0]] = (sarr.length > 1) ? sarr[1] : ""
- }
- }.bind(this));
- }
- return styles;
- },
- "getInnerProperties": function () {
- var properties = {};
- if (this.attributes.length) {
- for (var i = 0; i < this.attributes.length; i++) {
- properties[this.attributes[i].nodeName] = this.attributes[i].nodeValue;
- }
- }
- return properties;
- },
- "getZIndex": function () {
- var n = this;
- var i = 0;
- while (n) {
- if (n.getStyle("position") === "absolute") {
- var idx = n.getStyle("z-index");
- i = (idx && idx.toFloat() > i) ? idx.toFloat() + 1 : 0;
- break;
- }
- n = n.getParent();
- }
- return i;
- },
- "getParentSrcollNode": function () {
- var node = this.getParent();
- while (node && (node.getScrollSize().y - 2 <= node.getSize().y || (node.getStyle("overflow") !== "auto" && node.getStyle("overflow-y") !== "auto"))) {
- node = node.getParent();
- }
- return node || null;
- },
- "getEdgeHeight": function (notMargin) {
- var h = 0;
- h += (this.getStyle("border-top-width").toFloat() || 0) + (this.getStyle("border-bottom-width").toFloat() || 0);
- h += (this.getStyle("padding-top").toFloat() || 0) + (this.getStyle("padding-bottom").toFloat() || 0);
- if (!notMargin) h += (this.getStyle("margin-top").toFloat() || 0) + (this.getStyle("margin-bottom").toFloat() || 0);
- return h;
- },
- "getEdgeWidth": function (notMargin) {
- var h = 0;
- h += (this.getStyle("border-left-width").toFloat() || 0) + (this.getStyle("border-right-width").toFloat() || 0);
- h += (this.getStyle("padding-left").toFloat() || 0) + (this.getStyle("padding-right").toFloat() || 0);
- if (!notMargin) h += (this.getStyle("margin-left").toFloat() || 0) + (this.getStyle("margin-right").toFloat() || 0);
- return h;
- },
- "getSize": function () {
- if ((/^(?:body|html)$/i).test(this.tagName)) return this.getWindow().getSize();
- if (!window.getComputedStyle) return {x: this.offsetWidth, y: this.offsetHeight};
- if (this.get('tag') == 'svg') return svgCalculateSize(this);
- try {
- if (!layout || !layout.userLayout || !layout.userLayout.scale || layout.userLayout.scale == 1) {
- var bounds = this.getBoundingClientRect();
- return {x: bounds.width, y: bounds.height};
- } else {
- return {"x": this.offsetWidth.toFloat(), "y": this.offsetHeight.toFloat()};
- }
- } catch (e) {
- return {x: 0, y: 0};
- }
- },
- "getScaleOffsets": function () {
- var hasGetBoundingClientRect = this.getBoundingClientRect;
- //<1.4compat>
- //hasGetBoundingClientRect = hasGetBoundingClientRect && !Browser.Platform.ios;
- //</1.4compat>
- if (hasGetBoundingClientRect) {
- var bound = this.getBoundingClientRect();
- var boundLeft = bound.left;
- var boundTop = bound.top;
- if (!layout || !layout.userLayout || !layout.userLayout.scale || layout.userLayout.scale == 1) {
- } else {
- // boundLeft = boundLeft / layout.userLayout.scale;
- // boundTop = boundTop / layout.userLayout.scale;
- }
- var html = document.id(this.getDocument().documentElement);
- var htmlScroll = html.getScroll();
- var elemScrolls = this.getScrolls();
- var isFixed = (Element.getComputedStyle(this, 'position') == 'fixed');
- return {
- x: boundLeft.toFloat() + elemScrolls.x + ((isFixed) ? 0 : htmlScroll.x) - html.clientLeft,
- y: boundTop.toFloat() + elemScrolls.y + ((isFixed) ? 0 : htmlScroll.y) - html.clientTop
- };
- }
- var element = this, position = {x: 0, y: 0};
- if (isBody(this)) return position;
- while (element && !isBody(element)) {
- position.x += element.offsetLeft;
- position.y += element.offsetTop;
- //<1.4compat>
- if (Browser.firefox) {
- if (!borderBox(element)) {
- position.x += leftBorder(element);
- position.y += topBorder(element);
- }
- var parent = element.parentNode;
- if (parent && styleString(parent, 'overflow') != 'visible') {
- position.x += leftBorder(parent);
- position.y += topBorder(parent);
- }
- } else if (element != this && Browser.safari) {
- position.x += leftBorder(element);
- position.y += topBorder(element);
- }
- //</1.4compat>
- element = element.offsetParent;
- }
- //<1.4compat>
- if (Browser.firefox && !borderBox(this)) {
- position.x -= leftBorder(this);
- position.y -= topBorder(this);
- }
- //</1.4compat>
- return position;
- },
- getPosition: function (relative) {
- var offset = this.getScaleOffsets(),
- scroll = this.getScrolls();
- var position = {
- x: offset.x - scroll.x,
- y: offset.y - scroll.y
- };
- if (relative && (relative = document.id(relative))) {
- var relativePosition = relative.getPosition();
- return {
- x: position.x - relativePosition.x - leftBorder(relative),
- y: position.y - relativePosition.y - topBorder(relative)
- };
- }
- return position;
- }
- });
- Object.copy = function (from, to) {
- Object.each(from, function (value, key) {
- switch (typeOf(value)) {
- case "object":
- if (!to[key]) to[key] = {};
- Object.copy(value, to[key]);
- break;
- default:
- to[key] = value;
- }
- });
- };
- if (window.JSON) JSON.format = JSON.encode;
- if (window.Slick) {
- Slick.definePseudo('src', function (value) {
- return Element.get(this, "src").indexOf(value) !== -1;
- });
- Slick.definePseudo('srcarr', function (value) {
- var vList = value.split(",");
- var src = Element.get(this, "src");
- var flag = false;
- for (var i = 0; i < vList.length; i++) {
- if (src.indexOf(vList[i]) !== -1) {
- flag = true;
- break;
- }
- }
- return flag;
- });
- Slick.definePseudo('ahref', function (value) {
- var href = Element.get(this, "href");
- if (!href) href = "";
- href = href.toString().toLowerCase();
- return (href.indexOf(value) !== -1);
- });
- Slick.definePseudo('rowspanBefore', function (line) {
- var tr = MWF.getParent(this, "tr");
- var rowspan = this.get("rowspan").toInt() || 1;
- var currentRowIndex = tr.rowIndex.toInt();
- return rowspan > 1 && currentRowIndex < line.toInt() && currentRowIndex + rowspan - 1 >= line;
- });
- Slick.definePseudo('rowspan', function () {
- var rowspan = this.get("rowspan").toInt() || 1;
- return rowspan > 1;
- });
- Slick.definePseudo('colspanBefore', function (col) {
- var tr = MWF.getParent(this, "tr");
- var colspan = this.get("colspan").toInt() || 1;
- var currentColIndex = this.cellIndex.toInt();
- return colspan > 1 && currentColIndex < col.toInt() && currentColIndex + colspan - 1 >= col.toInt();
- });
- Slick.definePseudo('colspan', function () {
- var colspan = this.get("colspan").toInt() || 1;
- return colspan > 1;
- });
- }
- // 第三方扩展用的函数
- o2.thirdparty = o2.thirdparty || {};
-
- // 是否手机端
- o2.thirdparty.isMobile = function(){
- return /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i.test(navigator.userAgent.toLowerCase());
- }
- function alwaysFalse() {
- return false;
- }
-
- if (o2.thirdparty.isMobile()) {
- // 手机端钉钉
- o2.thirdparty.isDingdingMobile = function() {
- var isDingding = /dingtalk/i.test(navigator.userAgent.toLowerCase())
- return isDingding && o2.thirdparty.isMobile();
- }
- //手机端企业微信
- o2.thirdparty.isQywxMobile = function() {
- var isComWx = /wxwork/i.test(navigator.userAgent); // 是否企业微信
- return (isComWx && o2.thirdparty.isMobile());
- }
- //手机端微信
- o2.thirdparty.isWeiXinMobile = function() {
- var isWx = /micromessenger/i.test(navigator.userAgent); // 是否微信
- return (isWx && o2.thirdparty.isMobile());
- }
- o2.thirdparty.isDingdingPC = alwaysFalse;
- o2.thirdparty.isQywxPC = alwaysFalse;
- o2.thirdparty.isWeiXinPC = alwaysFalse;
- } else {
- o2.thirdparty.isDingdingMobile = alwaysFalse;
- o2.thirdparty.isQywxMobile = alwaysFalse;
- o2.thirdparty.isWeiXinMobile = alwaysFalse;
- // pc端钉钉
- o2.thirdparty.isDingdingPC = function() {
- var isDingding = /dingtalk/i.test(navigator.userAgent.toLowerCase())
- return isDingding && !o2.thirdparty.isMobile();
- }
- //PC端企业微信
- o2.thirdparty.isQywxPC = function() {
- var isComWx = /wxwork/i.test(navigator.userAgent); // 是否企业微信
- return (isComWx && !o2.thirdparty.isMobile());
- }
- //PC端微信
- o2.thirdparty.isWeiXinPC = function() {
- var isWx = /micromessenger/i.test(navigator.userAgent); // 是否微信
- return (isWx && !o2.thirdparty.isMobile());
- }
- }
-
-
- o2.common = o2.common || {};
- o2.common.encodeHtml = function (str) {
- str = str.toString();
- str = str.replace(/\&/g, "&");
- str = str.replace(/>/g, ">");
- str = str.replace(/</g, "<");
- return str.replace(/\"/g, """);
- };
- o2.common.getResponseTextPost = function (path, body, contentType) {
- var returnText = "";
- var options = {
- url: path,
- async: false,
- data: body,
- method: "post",
- onSuccess: function (esponseTree, responseElements, responseHTML, responseJavaScript) {
- returnText = responseHTML;
- }
- };
- var r = new Request.HTML(options);
- r.send();
- return returnText;
- };
- o2.common.getResponseText = function (path) {
- var returnText = "";
- var options = {
- url: path,
- async: false,
- method: "get",
- onSuccess: function (esponseTree, responseElements, responseHTML, responseJavaScript) {
- returnText = responseHTML;
- }
- };
- var r = new Request.HTML(options);
- r.send();
- return returnText;
- };
- o2.common.toDate = function (str) {
- var tmpArr = str.split(" ");
- if (!tmpArr[1]) tmpArr.push("0:0:0");
- var dateArr = tmpArr[0].split("-");
- var timeArr = tmpArr[1].split(":");
- return new Date(dateArr[0], parseInt(dateArr[1]) - 1, dateArr[2], timeArr[0], timeArr[1], timeArr[2]);
- };
- o2.common.toDate = function (str) {
- var tmpArr = str.split(" ");
- if (!tmpArr[1]) tmpArr.push("0:0:0");
- var dateArr = tmpArr[0].split("-");
- var timeArr = tmpArr[1].split(":");
- return new Date(dateArr[0], parseInt(dateArr[1]) - 1, dateArr[2], timeArr[0], timeArr[1], timeArr[2]);
- };
- o2.grayscale = function (src, width, height, callback) {
- try {
- var canvas = document.createElement('canvas');
- var ctx = canvas.getContext('2d');
- var imgObj = new Image();
- imgObj.src = src;
- canvas.width = width || imgObj.width;
- canvas.height = height || imgObj.height;
- ctx.drawImage(imgObj, 0, 0);
- var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
- for (var y = 0; y < imgPixels.height; y++) {
- for (var x = 0; x < imgPixels.width; x++) {
- var i = (y * 4) * imgPixels.width + x * 4;
- var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
- imgPixels.data[i] = avg;
- imgPixels.data[i + 1] = avg;
- imgPixels.data[i + 2] = avg;
- }
- }
- ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
- var src1 = canvas.toDataURL();
- //var blob = canvas.toBlob();
- canvas.destroy();
- return {"status": "success", "src": src1};
- } catch (e) {
- return {"status": "error", "src": src}
- }
- };
- o2.eventPosition = function (e) {
- var x = 0;
- var y = 0;
- if (Browser.name == "firefox") {
- x = parseFloat(e.event.clientX || e.event.x);
- y = parseFloat(e.event.clientY || e.event.y);
- } else {
- x = parseFloat(e.event.x);
- y = parseFloat(e.event.y);
- }
- return {"x": x, "y": y};
- };
- o2.dlgPosition = function (e, content, width, height) {
- var size = content.getSize();
- var x = 0;
- var y = 0;
- var fromx = 0;
- var fromy = 0;
- if (typeOf(e) == "element") {
- var position = e.getPosition(content);
- fromx = position.x;
- fromy = position.y;
- } else {
- if (Browser.name == "firefox") {
- fromx = parseFloat(e.clientX || e.x);
- fromy = parseFloat(e.clientY || e.y);
- } else {
- fromx = parseFloat(e.x);
- fromy = parseFloat(e.y);
- }
- if (e.target) {
- var position = e.target.getPosition(content);
- fromx = position.x;
- fromy = position.y;
- }
- }
- if (fromx + parseFloat(width) > size.x) {
- //fromx = fromx + 20;
- x = fromx - parseFloat(width);
- }else{
- fromx = x;
- //if (x < 0) x = 20;
- }
- if (fromy + parseFloat(height) > size.y) {
- y = fromy - parseFloat(height);
- //y = y - 20;
- }else{
- y = fromy;
- // if (y < 0) y = 0;
- // y = y + 20;
- }
- return {
- "x": x,
- "y": y,
- "fromx": fromx,
- "fromy": fromy
- }
- };
- if (window.Browser) {
- if (Browser.name === "ie" && Browser.version < 9) {
- Browser.ieuns = true;
- } else if (Browser.name === "ie" && Browser.version < 10) {
- Browser.iecomp = true;
- }
- if (Browser.iecomp) {
- o2.load("ie_adapter", null, false);
- o2.session.isDebugger = true;
- }
- o2.session.isMobile = (["mac", "win", "linux"].indexOf(Browser.Platform.name) === -1);
- }
- })();
- o2.more = true;
- //o2.addReady
- (function () {
- //dom ready
- var _dom;
- if (window.document) {
- _dom = {
- ready: false,
- loaded: false,
- checks: [],
- shouldPoll: false,
- timer: null,
- testElement: document.createElement('div'),
- readys: [],
- domready: function () {
- clearTimeout(_dom.timer);
- if (_dom.ready) return;
- _dom.loaded = _dom.ready = true;
- o2.removeListener(document, 'DOMContentLoaded', _dom.checkReady);
- o2.removeListener(document, 'readystatechange', _dom.check);
- _dom.onReady();
- },
- check: function () {
- for (var i = _dom.checks.length; i--;) if (_dom.checks[i]() && window.MooTools && o2.core && o2.more) {
- _dom.domready();
- return true;
- }
- return false;
- },
- poll: function () {
- clearTimeout(_dom.timer);
- if (!_dom.check()) _dom.timer = setTimeout(_dom.poll, 10);
- },
- /*<ltIE8>*/
- // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
- // testElement.doScroll() throws when the DOM is not ready, only in the top window
- doScrollWorks: function () {
- try {
- _dom.testElement.doScroll();
- return true;
- } catch (e) {
- }
- return false;
- },
- /*</ltIE8>*/
- onReady: function () {
- for (var i = 0; i < _dom.readys.length; i++) {
- this.readys[i].apply(window);
- }
- },
- addReady: function (fn) {
- if (_dom.loaded) {
- if (fn) fn.apply(window);
- } else {
- if (fn) _dom.readys.push(fn);
- }
- return _dom;
- },
- checkReady: function () {
- _dom.checks.push(function () {
- return true
- });
- _dom.check();
- }
- };
- o2.addListener(document, 'DOMContentLoaded', _dom.checkReady);
- /*<ltIE8>*/
- // If doScroll works already, it can't be used to determine domready
- // e.g. in an iframe
- if (_dom.testElement.doScroll && !_dom.doScrollWorks()) {
- _dom.checks.push(_dom.doScrollWorks);
- _dom.shouldPoll = true;
- }
- /*</ltIE8>*/
- if (document.readyState) _dom.checks.push(function () {
- var state = document.readyState;
- return (state == 'loaded' || state == 'complete');
- });
- if ('onreadystatechange' in document) o2.addListener(document, 'readystatechange', _dom.check);
- else _dom.shouldPoll = true;
- if (_dom.shouldPoll) _dom.poll();
- } else {
- _dom = {
- ready: false,
- loaded: false,
- checks: [],
- shouldPoll: false,
- timer: null,
- readys: [],
- domready: function () {
- clearTimeout(_dom.timer);
- if (_dom.ready) return;
- _dom.loaded = _dom.ready = true;
- _dom.onReady();
- },
- check: function () {
- if (window.MooTools && o2.core && o2.more) {
- _dom.domready();
- return true;
- }
- return false;
- },
- onReady: function () {
- for (var i = 0; i < _dom.readys.length; i++) {
- this.readys[i].apply(window);
- }
- },
- addReady: function (fn) {
- if (_dom.loaded) {
- if (fn) fn.apply(window);
- } else {
- if (fn) _dom.readys.push(fn);
- }
- return _dom;
- },
- checkReady: function () {
- _dom.checks.push(function () {
- return true
- });
- _dom.check();
- }
- };
- }
- var _loadO2 = function () {
- (!o2.core) ? this.o2.load("o2.core", _dom.check) : _dom.check();
- (!o2.more) ? this.o2.load("o2.more", _dom.check) : _dom.check();
- };
- if (!window.MooTools) {
- this.o2.load("mootools", function () {
- _loadO2();
- _dom.check();
- });
- } else {
- _loadO2();
- }
- this.o2.addReady = function (fn) {
- _dom.addReady.call(_dom, fn);
- };
- })();
- //compatible
- COMMON = {
- "DOM": {},
- "setContentPath": function (path) {
- COMMON.contentPath = path;
- },
- "JSON": o2.JSON,
- "Browser": window.Browser,
- "Class": o2.Class,
- "XML": o2.xml,
- "AjaxModule": {
- "load": function (urls, callback, async, reload) {
- o2.load(urls, callback, reload, document);
- },
- "loadDom": function (urls, callback, async, reload) {
- o2.load(urls, callback, reload, document);
- },
- "loadCss": function (urls, callback, async, reload, sourceDoc) {
- o2.loadCss(urls, document.body, callback, reload, sourceDoc);
- }
- },
- "Request": Request,
- "typeOf": o2.typeOf
- };
- if (COMMON.Browser) COMMON.Browser.Platform.isMobile = o2.session.isMobile;
- COMMON.DOM.addReady = o2.addReady;
- MWF = o2;
- MWF.getJSON = o2.JSON.get;
- MWF.getJSONP = o2.JSON.getJsonp;
- MWF.defaultPath = o2.session.path;
- }
|