51 Star 230 Fork 39

藤之内/Vue-rap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
rap.2.1.0.js 65.01 KB
一键复制 编辑 原始数据 按行查看 历史
藤之内 提交于 2022-06-07 02:45 . rename rap.1.3.2.js to rap.2.1.0.js.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
/* vue-rap v2.1 | (c) 2018 by tengzhinei */
(function webpackUniversalModuleDefinition(root, factory) {
if (typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if (typeof define === 'function' && define.amd)
define([], factory);
else if (typeof exports === 'object')
exports["Rap"] = factory();
else
root["Rap"] = factory();
})(this, function () {
function P(call) {
function create() {
var success = null;
var success_arg = null;
var result = 0;
var error = null;
var error_arg = null;
var next = null;
var nextCall = function () {
try {
var data = success(success_arg);
if (data && typeof data.then == "function") {
//返回结果是 promise//需要等待
data.then(function (res) {
next.resolve(res);
}).catch(function (e) {
next.reject(e);
});
} else {
next.resolve(data);
}
} catch (e) {
next.reject(e);
}
};
return {
resolve: function (res) {
if (result == 2 || result == 1) {
return this;
}
success_arg = res;
result = 1;
if (success) {
nextCall();
}
return this;
},
reject: function (res) {
if (result == 2 || result == 1) {
return this;
}
error_arg = res;
result = 2;
var reject_ok = false;
if (error) {
reject_ok = error(error_arg);
}
if (!reject_ok && next) {
next.reject(res);
}
return this;
},
then: function (call) {
next = P();
success = call;
if (result == 1) {
nextCall();
} else if (result == 2) {
if (error) {
error(error_arg);
} else {
next.reject(error_arg);
}
}
next.parent = this;
return next;
},
catch: function (e) {
next = P();
this.then(function () {
next.resolve();
});
error = e;
if (result == 2) {
error(error_arg);
}
next.parent = this;
return next;
}
};
}
var p = create();
if (call) {
p = p.resolve().then(call);
}
return p;
}
function rapGet(url) {
var p = P();
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();//W3C
} else {
request = new ActiveXObject('MicroSoft.XMLHTTP');//IE
}
request.open('get', url);
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
p.resolve(request.responseText);
}
if (request.readyState == 4 && (request.status >= 400)) {
p.reject(request);
}
};
request.onerror = function (e) {
p.reject(e);
};
try {
request.send();
} catch (e) {
p.reject(e);
}
return p;
}
var viewLines = {};
var routers = {};
var RapConfig = {script: [], css: []};
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
var IndexedDBCache = {
open: function () {
var p = P();
if (this.db) {
p.resolve();
return;
}
var me = this;
var request = indexedDB.open('rap-cache', 10);
request.onerror = function (e) {
p.reject();
};
request.onupgradeneeded = function (e) {
var db = e.target.result;
if (!db.objectStoreNames.contains('rap_cache')) {
db.createObjectStore('rap_cache', {keyPath: "key"});
}
};
request.onsuccess = function (e) {
me.db = e.target.result;
p.resolve();
};
return p;
}, set: function (key, content) {
var transaction = this.db.transaction(['rap_cache'], 'readwrite');
var store = transaction.objectStore('rap_cache');
store.put({
key: key,
content: content,
v:Rap.RapAppVersion
});
}, get: function (key) {
var p = P();
if (Rap.cacheContent && Rap.cacheContent[key]) {
p.resolve(Rap.cacheContent[key]);
console.log(key + "已打包");
return p;
}
var transaction = this.db.transaction(['rap_cache'], 'readwrite');
var store = transaction.objectStore('rap_cache');
var request = store.get(key);
request.onsuccess = function (e) {
var item = e.target.result;
if (item&&item.v===Rap.RapAppVersion) {
Cache.remove(key);
p.resolve(item.content);
} else {
p.resolve('');
}
};
return p;
}, remove: function (key) {
var transaction = this.db.transaction(['rap_cache'], 'readwrite');
var store = transaction.objectStore('rap_cache');
store.delete(key);
}, clear: function () {
var transaction = this.db.transaction(['rap_cache'], 'readwrite');
var store = transaction.objectStore('rap_cache');
store.clear();
localStorageCache.clear();
}, each: function (call) {
var p = P();
var transaction = this.db.transaction(['rap_cache'], 'readwrite');
var store = transaction.objectStore('rap_cache');
var request = store.getAllKeys();
request.onsuccess = function (e) {
var item = e.target.result;
var i = -1;
function next() {
i++;
if (i == item.length) {
p.resolve();
return;
}
var key = item[i];
IndexedDBCache.get(key).then(function (content) {
var m = call(key, content);
if (m && m.then) {
m.then(function () {
next();
})
} else {
next();
}
})
}
next();
};
return p;
}, pack: function (base) {
var me = this;
var p = P();
var transaction = this.db.transaction(['rap_cache'], 'readwrite');
var store = transaction.objectStore('rap_cache');
var request = store.getAllKeys();
request.onsuccess = function (e) {
var keys = e.target.result;
var i = -1;
var js = {};
function next() {
i++;
if (i === keys.length) {
var content = JSON.stringify(js);
p.resolve(content);
return;
}
var key = keys[i];
if(key.indexOf(Rap.history_base+"|")!==0){
next();
return;
}
var o_key=key.substr(Rap.history_base_index);
if(base){
var pre=['ly/','/ly/','/static/ly/libs/','/'+Rap.history_base+"/ly/"]
var is_pack=false;
for(var j=0;j<pre.length;j++){
if(o_key.indexOf(pre[j])===0){
is_pack=true;
}
}
if(!is_pack){
next();
return;
}
}
me.get(key).then(function (content) {
if(o_key.indexOf('/'+Rap.history_base+"/ly/")===0){
o_key =o_key.replace('/'+Rap.history_base+"/ly/",'/rap_history_base/ly/');
}
js[o_key] = content;
next();
});
};
next();
};
return p;
}
};
var localStorageCache = {
get: function (key) {
return P(function () {
key = 'rap|' + key;
return localStorage.getItem(key);
});
},
set: function (key, value) {
return P(function () {
key = 'rap|' + key;
try {
localStorage.setItem(key, value);
} catch (e) {
//内容已满删除非必要缓存
var length = localStorage.length;
var js = "";
for (var i = 0; i < length; i++) {
var k = localStorage.key(i);
if (key.indexOf('.rap') == key.length - 4 || key.indexOf('.ver') == key.length - 4) {
localStorage.removeItem(k);
} else if (key.indexOf('.js') > -1 && RapConfig.script.indexOf(k) < 0) {
localStorage.removeItem(k);
} else if (key.indexOf('.css') > -1 && RapConfig.css.indexOf(k) < 0) {
localStorage.removeItem(k);
}
}
localStorage.setItem(key, value);
}
});
}, remove: function (key) {
return P(function () {
key = 'rap|' + key;
localStorage.removeItem(key);
});
}, clear: function () {
return P(function () {
var length = localStorage.length;
for (var i = 0; i < length; i++) {
var k = localStorage.key(i);
if (k.indexOf('rap|') == 0) {
localStorage.removeItem(k);
}
}
});
}, count: function () {
return localStorage.length;
}, key: function (index) {
return localStorage.key(index);
}, pack: function () {
var length = localStorage.length;
var js = "";
for (var i = 0; i < length; i++) {
var key = localStorage.key(i);
if (key.indexOf('.rap') == key.length - 4) {
var content = localStorage.get(key);
var json = JSON.parse(content);
var script = json.script;
delete json['script'];
var name = key.substr(0, key.length - 4).split('/').join('_');
js += "var url='" + json.base + "';var modName='" + name + "';var config=" + JSON.stringify(json) + ";(function(url,name,config){\n" + script + ";\nRap.$create(url,name,config);\n})(url,modName,config);\n";
}
}
js = "(function () {" + js + "})();";
return P(function () {
return js;
});
}
};
var Cache = {
instance: indexedDB ? IndexedDBCache : localStorageCache,
getKey: function (key) {
if (Rap.router_model == 'history') {
return Rap.history_base + '|' + key;
}
return location.pathname + '|' + key;
},
open: function () {
if (this.instance.open) {
return this.instance.open();
}
return P().resolve();
}, set: function (key, value) {
key = this.getKey(key);
return this.instance.set(key, value);
}, get: function (key) {
key = this.getKey(key);
return this.instance.get(key);
}, remove: function (key) {
key = this.getKey(key);
return this.instance.remove(key);
}, clear: function () {
return this.instance.clear();
}, pack: function () {
return this.instance.pack();
}
};
function urlJoin(base, url) {
if (url.indexOf('/') == 0) {
return url;
}
var p = base.split("/");
if (url.indexOf('.') == -1) {
p.pop();
return p.join('/') + '/' + url;
}
// p.pop();
p.pop();
var pre = [];
while (url.indexOf('../') == 0) {
url = url.replace("../", "");
pre.push(p.pop())
}
return p.join("/") + "/" + url;
}
var require_data = {};
var require_loading = {};
var require_loading_P = {};
var require_init = {};
function loadRequire(url, rely, config, layout) {
if (!Rap.isReady) {
require_init[url] = {rely: rely, config: config, layout: layout};
return;
}
require_loading[url] = true;
var p = Rap.loadMod(layout);
for (var i = 0; i < rely.length; i++) {
(function () {
var r = rely[i];
if (r.indexOf('.js') > 0) {
var get_url = urlJoin(url, rely[i]);
var cache_key = get_url;
if(get_url.indexOf('?')===-1){
get_url+="?";
}
get_url += '&v=' + Rap.RapAppVersion;
if (require_data[get_url]) {
return 1;
} else if (require_loading[get_url] || require_init[get_url]) {
if (!require_loading_P[get_url]) {
require_loading_P[get_url] = [];
}
var wait = P();
require_loading_P[get_url].push(wait);
p = p.then(function () {
return wait;
});
} else {
require_loading[get_url] = true;
var link = get_url;
var load = null;
if (r.indexOf('.json') > 0) {
load = rapGet(link).then(function (content) {
var exports = JSON.parse(content);
require_data[get_url] = exports;
require_loading[get_url] = false;
var loading = require_loading_P[get_url];
if (loading) {
for (var i = 0; i < loading.length; i++) {
var wait = loading[i];
wait.resolve();
}
}
});
} else {
if (Rap.router_model == 'history') {
link = '/' + Rap.history_base + link;
}
load = Cache.get(cache_key).then(function (content) {
var call = function (data) {
if (Rap.debug) {
data += "//@ sourceURL=" + link;
}
var define = eval(data);
if (define && define.then) {
return define.then(function (exports) {
require_data[get_url] = exports;
require_loading[get_url] = false;
var loading = require_loading_P[get_url];
if (loading) {
for (var i = 0; i < loading.length; i++) {
var wait = loading[i];
wait.resolve();
}
}
});
}
};
if (!Rap.debug && content) {
return call(content);
} else {
return rapGet(link).then(function (content) {
Cache.set(cache_key, content);
return call(content);
});
}
});
}
p = p.then(function () {
return load;
});
}
} else {
var load = Rap.loadMod(urlJoin(url, r));
p = p.then(function () {
return load;
});
}
})();
}
return p.then(function () {
var exps = [];
for (var i = 0; i < rely.length; i++) {
var r = urlJoin(url, rely[i]);
if (r.indexOf('.js') > 0) {
exps.push(require_data[r]);
}
}
if (typeof config === 'function') {
config = config.apply(null, exps);
}
if (config) {
require_data[url] = config;
}
require_loading[url] = false;
var loading = require_loading_P[url];
if (loading) {
for (var i = 0; i < loading.length; i++) {
var wait = loading[i];
wait.resolve();
}
}
return config;
});
}
var head = document.getElementsByTagName("head")[0];
function evalScript(modUrl, url, modName, config) {
var p = P();
var script = config.script;
// script = "(function(url,name,config,p){\n if(Rap.debug){console.log('模块加载: '+modName);} var $this=null;Rap.$IS_CREATE_COMP=true; " + script + ";Rap.$IS_CREATE_COMP=false;\nRap.$create(url,name,config,function(that){$this=that;}).then(function(){p.resolve();});\n})(modUrl,modName,config,p)\n";
script = "(function(url,name,config,p){\n if(Rap.debug){} var $this=null;Rap.$IS_CREATE_COMP=true; " + script + ";Rap.$IS_CREATE_COMP=false;\nRap.$create(url,name,config,function(that){$this=that;}).then(function(){p.resolve();});\n})(modUrl,modName,config,p)\n";
if (Rap.debug) {
script += "//@ sourceURL=" + modUrl + ".js";
}
try {
eval(script);
} catch (e) {
throw e;
}
return p;
}
function evalJS(url, script) {
if (Rap.debug) {
script += "//@ sourceURL=" + url;
}
eval(script);
}
function addCss(id, content) {
var el = document.getElementById(id);
if (!content || !content.trim())return;
if (el) {
var num = parseInt(el.getAttribute('num')) + 1;
el.setAttribute('num', num + '');
return;
}
var style = document.createElement('style');
style.type = 'text/css';
style.id = id;
style.setAttribute('num', '1');
style.innerHTML = content;
head.appendChild(style);
}
var layoutAndRely = [];
var compVersion = {};
var scripts_loaded = [];
function v_link_click(event) {
var el = event.currentTarget;
var link = el.getAttribute("rap-link");
if (!link) {
console.log(el);
}
var replace = el.getAttribute("rap-replace");
var keep = el.getAttribute("rap-keep");
var back = el.getAttribute("rap-back");
if (back === 'true') {
Rap.back();
return;
}
var arg = el.getAttribute("rap-arg");
Rap.go(link, replace === 'true', keep === 'true');
}
function v_jump_click(event){
var el = event.currentTarget;
var link = el.getAttribute("rap-link");
if (!link) {
console.log(el);
}
var replace = el.getAttribute("rap-replace");
var keep = el.getAttribute("rap-keep");
var back = el.getAttribute("rap-back");
if (back === 'true') {
Rap.back();
return;
}
Rap.goTo(link, replace === 'true', keep === 'true');
}
var RapReady = null;
var InsertScript = {
loaded: {},
loading: {},
insert: function (src) {
var p = P();
if (this.loaded[src]) {
p.resolve();
return p;
}
if (this.loading[src]) {
this.loading[src].push(p);
return p;
}
this.loading[src] = [];
this.loading[src].push(p);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
var me = this;
script.onload = script.onreadystatechange = function () {
if (script.readyState && script.readyState != 'loaded' && script.readyState != 'complete') return;
script.onreadystatechange = script.onload = null;
me.loaded[src] = true;
var loading = me.loading[src];
var length = loading.length;
for (var i = 0; i < length; i++) {
var p = loading[i];
p.resolve();
}
me.loading[src] = [];
};
document.getElementsByTagName("head")[0].appendChild(script);
return p;
}
};
var onNetError = function () {
};
var SCROLL_Y = 0;
var IS_GO = false;
var default_keep_include = [];
var rap_ready = false;
var weixin_refresh_time = '';
var weixin_refresh_back = false;
var KeepInstance = null;
var cssVar = {
};
var Rap = {
//css var(--变量替换)
cssVar: function (name, value) {
cssVar[name] = value;
},
$IS_CREATE_COMP: false,
is_weixin: false,
is_android: false,
router_model: 'hash',
history_base: 'admin',
history_base_index: 0,
routerUrl: function () {
if (Rap.router_model == 'hash') {
return window.location.hash;
} else {
return location.pathname.substring(Rap.history_base_index) + location.search;
}
},
config: function (config) {
//缓存方案配置
if (config.cache) {
if (config.cache == 'localStorage') {
Cache.instance = localStorageCache;
} else if (config.cache == 'indexedDB') {
if (indexedDB) {
Cache.instance = indexedDB;
}
} else {
Cache.instance = config.cache;
}
}
//路由方案配置
if (config.router_model) {
Rap.router_model = config.router_model;
}
if (config.history_base) {
Rap.history_base = config.history_base;
}
if (Rap.router_model == 'history') {
Rap.history_base_index = Rap.history_base.length + 1;
}
//keep_alive 方案配置
if (config.keep_include) {
for (var i = 0; i < config.keep_include.length; i++) {
default_keep_include.push(config.keep_include[i].replace('/', '_'));
}
}
Rap.debug = config.debug;
RapConfig.css = config.css;
RapConfig.script = config.script;
if (!config.default_page) {
document.write("请配置默认页面default_page");
return this;
}
Rap.default_page = config.default_page;
if (config.filePostfix) {
Rap.filePostfix = config.filePostfix;
}
if (config.onNetError) {
onNetError = config.onNetError;
}
var p = Cache.open().then(function () {
var load_all = Rap.appVersion(config.app_version);
var np = P().resolve();
if (!load_all && !Rap.debug && config.pack) {
//加载打包文件
np = np.then(function () {
return Rap.get(config.pack);
}).then(function (rs) {
var data = JSON.parse(rs);
Rap.cacheContent = data;
for (var key in data) {
var content = data[key];
if(key.indexOf('/rap_history_base/')===0){
key=key.replace('/rap_history_base/','/'+Rap.history_base+"/");
}
key=Rap.history_base+"|"+key;
Cache.instance.set(key, content);
}
});
}
np = np.then(function () {
var loadJs = Rap.loadScript(config.script);
return Rap.loadCss(config.css).then(function () {
return loadJs;
});
});
return np;
}).then(function () {
if (!window.Vue) {
Cache.clear();
if (localStorage.getItem('debug_reload') != 'true') {
localStorage.setItem('debug_reload', 'true');
location.reload();
}
return;
}
Vue.options.components.KeepAlive.created = function () {
this.cache = Object.create(null);
this.keys = [];
KeepInstance = this;
};
localStorage.removeItem('debug_reload');
return Rap.install(Vue);
}).then(function () {
rap_ready = true;
if (RapReady) {
RapReady();
}
}).catch(function (e) {
console.log(e);
});
p.ready = p.then;
return p;
},
onNetError: function (listener) {
onNetError = listener;
},
ready: function (fun) {
var p = P();
var next = p.then(fun);
RapReady = function () {
p.resolve();
};
if (rap_ready) {
RapReady();
}
return next;
},
app: function (app) {
if (!app) {
app = {}
}
if (!app.el) {
app.el = "#app";
}
if (!app.mixins) {
app.mixins = [];
}
app.mixins.push(Rap.MainView);
var App = new Vue(app);
Rap.onRouterChange();
return App;
},
keep: function (page) {
Rap.go(page, false, true);
},
go: function (to, replace, keep) {
console.log("该方法已过期请使用navigateTo和redirectTo,注意他们之前的不同");
if (to.indexOf('/') != 0) {
to = "/" + to;
}
this.goTo(to, replace, keep);
},
redirectTo:function (to, keep) {
this.goTo(to, true, keep);
},
navigateTo:function (to,keep) {
this.goTo(to, false, keep);
},
goTo: function (to, replace, keep) {
if (to.indexOf('http://') === 0 || to.indexOf('https://') === 0) {
window.open(to);
return;
}
var page = Rap.global_router.page;
page = urlJoin(page, to);
if (replace == null) {
replace = false;
}
if (APPMainView.$children.length > 0) {
var currentView = APPMainView.$children[APPMainView.$children.length - 1];
var name = currentView.$options._componentTag;
if (default_keep_include.indexOf(name) > -1) {
keep = true;
}
if (keep && APPMainView.keep_include.indexOf(name) == -1) {
APPMainView.keep_include.push(name);
if (KeepInstance) {
KeepInstance._render();
}
}
}
IS_GO = true;
var search = location.search;
if (!search)search = "?";
if (replace) {
SCROLL_Y = window.scrollY;
if (Rap.router_model == 'hash') {
history.replaceState(null, page, search + "#" + page);
Rap.onRouterChange();
} else {
if (page.indexOf('/') != 0) {
page = '/' + page;
}
history.replaceState(null, page, '/' + Rap.history_base + page);
Rap.onRouterChange();
if (Rap.is_weixin) {
//微信ios浏览器路径不变化的坑
weixin_refresh_time = '#weixin_refresh_time=' + new Date().getTime();
location.hash = weixin_refresh_time;
}
}
}
else {
SCROLL_Y = window.scrollY;
if (Rap.router_model == 'hash') {
location.href = search + "#" + page;
} else {
if (page.indexOf('/') != 0) {
page = '/' + page;
}
history.pushState(null, page, '/' + Rap.history_base + page);
Rap.onRouterChange();
if (Rap.is_weixin) {
//微信ios浏览器路径不变化的坑
weixin_refresh_time = '#weixin_refresh_time=' + new Date().getTime();
location.hash = weixin_refresh_time;
}
}
}
},
replace: function (page) {
Rap.go(page, true);
},
back: function () {
history.back();
},
install: function (Vue) {
Vue.directive('link', {
bind: function (el, binding, vnode) {
el.setAttribute("rap-link", binding.value);
var modifiers = binding.modifiers;
if (modifiers.replace) {
el.setAttribute("rap-replace", 'true');
}
if (modifiers.keep) {
el.setAttribute("rap-keep", 'true');
}
if (modifiers.back) {
el.setAttribute("rap-back", 'true');
}
if (binding.arg) {
el.setAttribute("rap-arg", binding.arg);
}
if (el.addEventListener) {
el.addEventListener('click', v_link_click, false);
}
//ie浣跨敤attachEvent锛屾潵娣诲姞浜嬩欢
else {
el.attachEvent("onclick", v_link_click);
}
},
update: function (el, binding, vnode) {
el.setAttribute("rap-link", binding.value);
}, unbind: function (el, binding, vnode) {
if (el.removeEventListener) {
el.removeEventListener("click", v_link_click, false);
} else {
el.detachEvent("click", v_link_click);
}
}
});
Vue.directive('jump', {
bind: function (el, binding, vnode) {
el.setAttribute("rap-link", binding.value);
var modifiers = binding.modifiers;
if (modifiers.replace) {
el.setAttribute("rap-replace", 'true');
}
if (modifiers.keep) {
el.setAttribute("rap-keep", 'true');
}
if (modifiers.back) {
el.setAttribute("rap-back", 'true');
}
if (binding.arg) {
el.setAttribute("rap-arg", binding.arg);
}
if (el.addEventListener) {
el.addEventListener('click', v_jump_click, false);
}
//ie浣跨敤attachEvent锛屾潵娣诲姞浜嬩欢
else {
el.attachEvent("onclick", v_jump_click);
}
},
update: function (el, binding, vnode) {
el.setAttribute("rap-link", binding.value);
}, unbind: function (el, binding, vnode) {
if (el.removeEventListener) {
el.removeEventListener("click", v_jump_click, false);
} else {
el.detachEvent("click", v_jump_click);
}
}
});
return Rap.init();
},
default_page: '',
debug: false,
filePostfix: 'html',
RapAppVersion: 1,
global_router: {
query: {},
search: [],
page: '',
hash: ''
},
baseUrl: "",
isReady: false,
$pageDefine: null,
insertScript: function (src) {
if (src instanceof Array) {
var promise = Rap.promise().resolve();
for (var i = 0; i < src.length; i++) {
(function () {
var link = src[i];
promise = promise.then(function (data) {
return Rap.insertScript(link);
});
})();
}
return promise;
} else {
return InsertScript.insert(src);
}
},
loadScript: function (url, raw) {
var p = P();
if (!url) {
p.resolve();
return p;
}
if (url instanceof Array) {
//鍚屾椂杩涜涓€姝ュ姞杞�
var promise = Rap.promise().resolve();
for (var i = 0; i < url.length; i++) {
(function () {
var link = url[i];
var load = Rap.loadScript(link, true);
promise = promise.then(function (data) {
if (data) {
var define = evalJS(data.url, data.content);
}
return load;
});
})();
}
promise = promise.then(function (data) {
if (data) {
evalJS(data.url, data.content);
}
});
return promise;
} else {
var cache_key=url;
if (url.indexOf('?') !== -1) {
url += '&';
} else {
url += '?';
}
url += 'v=' + Rap.RapAppVersion;
//宸插姞杞借繃
var index = scripts_loaded.indexOf(url);
if (index > -1) {
p.resolve();
return p;
}
scripts_loaded.push(url);
Cache.get(cache_key).then(function (content) {
if (!Rap.debug && content) {
try {
if (!raw) {
evalJS(url, content);
}
p.resolve({url: url, content: content});
return p;
} catch (e) {
Cache.remove(url);
}
}
rapGet(url).then(function (content) {
if (!raw) {
evalJS(url, content);
}
Cache.set(cache_key, content);
p.resolve({url: url, content: content});
});
});
}
return p;
},
loadCss: function (url, raw) {
var p = P();
if (!url) {
p.resolve();
return p;
}
if (url instanceof Array) {
var promise = Rap.promise().resolve();
for (var i = 0; i < url.length; i++) {
(function () {
var link = url[i];
var load = Rap.loadCss(link, true);
promise = promise.then(function (data) {
if (data) {
addCss(data.url, data.content);
}
return load;
});
})();
}
return promise.then(function (data) {
if (data) {
addCss(data.url, data.content);
}
});
} else {
var cache_key=url;
if (url.indexOf('?') != -1) {
url += '&';
} else {
url += '?';
}
url += 'v=' + Rap.RapAppVersion;
Cache.get(cache_key).then(function (content) {
if (!Rap.debug && content) {
if (!raw) {
addCss(url, content);
}
p.resolve({url: url, content: content});
} else {
rapGet(url).then(function (content) {
//变量替换
for (var varName in cssVar) {
var name = eval("/var\\(--" + varName.trim() + "\\)/g");
var value = cssVar[varName];
content = content.replace(name, value);
}
Cache.set(cache_key, content);
if (!raw) {
addCss(url, content);
}
p.resolve({url: url, content: content});
});
}
});
}
return p;
},
removeCss: function (url) {
var el = document.getElementById(url);
if (!el) {
if (url.indexOf('?') != -1) {
url += '&';
} else {
url += '?';
}
url += 'v=' + Rap.RapAppVersion;
el = document.getElementById(url);
}
if (el) {
if (el.hasAttribute('num')) {
var num = parseInt(el.getAttribute('num'));
if (num == 1) {
el.parentElement.removeChild(el);
} else {
el.setAttribute('num', num - 1);
}
} else {
el.parentElement.removeChild(el);
}
}
},
compVersion: function (mod, version) {
compVersion[mod] = version;
},
define: function () {
var layout = null;
var rely = null;
var config = null;
if (arguments.length > 2) {
layout = arguments[0];
rely = arguments[1];
config = arguments[2]
} else if (arguments.length > 1) {
rely = arguments[0];
config = arguments[1]
} else {
config = arguments[0];
}
if (!rely) {
rely = [];
}
if (Rap.$IS_CREATE_COMP) {
this.$pageDefine = {
layout: layout,
rely: rely,
config: config
};
} else {
return loadRequire(layout, rely, config);
}
},
appVersion: function (version) {
Rap.RapAppVersion = version;
var rap_v_k = Cache.getKey('RapAppVersion');
var rapAppVersion = localStorage.getItem(rap_v_k);
if (rapAppVersion && rapAppVersion != Rap.RapAppVersion) {
Cache.clear();
localStorage.setItem(rap_v_k, Rap.RapAppVersion);
return false;
}
localStorage.setItem(rap_v_k, Rap.RapAppVersion);
if (!rapAppVersion) {
return false;
}
return true;
},
init: function () {
Rap.isReady = true;
var promise = Rap.promise().resolve();
for (var k in require_init) {
(function (k) {
var item = require_init[k];
var define = loadRequire(k, item.rely, item.config, item.layout);
promise = promise.then(function () {
return define;
})
})(k)
}
return promise;
},
$create: function (url, name, option, thisCallBack) {
var p = P();
var style = option.style;
var modName = name;
var layout = this.$pageDefine.layout;
if (layout) {
layout = urlJoin(url, layout);
}
var rely = this.$pageDefine.rely;
var config = this.$pageDefine.config;
if (layout) {
if (layout.indexOf('/') == 0) {
layout = layout.substr(1);
}
viewLines[modName] = layout.split('/').join('_');
}
var scrollY = 0;
var childMixin = {
data: function () {
return {
router: Rap.global_router,
RapViews: RapShareData.RapViews,
childView: null,
rap: {
page_activated: true
}
};
},
watch: {
'RapViews.index': function () {
if (!this.rap.page_activated)return;
this.$options.RapViews.apply(this);
var init = this.$options.init;
if (init) {
init.apply(this, [Rap.global_router.query, Rap.global_router.search]);
}
}
},
RapViews: function () {
var index = -1;
for (var i = 0; i < this.RapViews.items.length; i++) {
var name = this.RapViews.items[i];
if (name == modName) {
index = i;
}
}
if (index + 1 < this.RapViews.items.length) {
this.childView = this.RapViews.items[index + 1];
}
},
created: function () {
if (style) {
addCss(modName, style);
}
this.$options.RapViews.apply(this);
if (thisCallBack) {
thisCallBack(this);
}
},
activated: function () {
if (!this.rap.page_activated) {
addCss(modName, style);
setTimeout(function () {
document.documentElement.scrollTop = scrollY;
document.body.scrollTop = scrollY;
window.scrollTo(0, scrollY);
}, 2);
this.$options.RapViews.apply(this);
var init = this.$options.init;
if (init) {
init.apply(this, [Rap.global_router.query, Rap.global_router.search]);
}
}
this.rap.page_activated = true;
},
deactivated: function () {
this.rap.page_activated = false;
Rap.removeCss(modName);
scrollY = SCROLL_Y;
},
mounted: function () {
var init = this.$options.init;
if (init) {
init.apply(this, [Rap.global_router.query, Rap.global_router.search]);
}
},
destroyed: function () {
Rap.removeCss(modName);
},
methods: {}
};
childMixin.render = function (_c) {
var render = option.render;
if (typeof(render) == 'string') {
eval('render=' + option.render);
}
if (render) {
return render.call(this, _c);
}
};
var staticRenderFns = option.staticRenderFns;
if (typeof(staticRenderFns) == 'string') {
childMixin.staticRenderFns = eval('[' + option.staticRenderFns + ']');
} else {
childMixin.staticRenderFns = option.staticRenderFns;
}
delete Rap.$pageDefine;
loadRequire(url, rely, config, layout).then(function (config) {
if (!config.mixins) {
config.mixins = [];
}
config.mixins.push(childMixin);
if (config.name) {
modName = config.name;
}
var comp = Vue.component(modName, config);
p.resolve();
});
return p;
},
loadUrl: function (url) {
var path = url.split('/');
var m = [];
for (var i = 0; i < path.length; i++) {
var item = path[i];
if (!(parseInt(item) + "" == item || item.indexOf('@') == 0)) {
m.push(item);
}
}
path = m;
Rap.global_router.page = path.join('/');
var as = routers[Rap.global_router.page];
if (as) {
if (typeof(as) == 'function') {
as(Rap.global_router.query, Rap.global_router.search);
return;
} else {
Rap.global_router.page = as;
}
}
var modName = Rap.global_router.page.split('/').join('_');
if (Vue.component(modName)) {
Rap.onViewChangeCallBack(modName);
} else {
this.loadMod(Rap.global_router.page).then(function () {
Rap.onViewChangeCallBack(modName);
}).catch(function (e) {
console.log(Rap.global_router.page + '加载失败');
console.log(e);
if (onNetError) {
onNetError(e);
}
});
}
},
/**
* 预加载模块 异步队列加载的 请使用 loadMod
*/
preComp: function (url) {
Rap.loadMod(url);
},
require: function (depends, callback) {
if (!callback) {
callback = {};
}
loadRequire('', depends, callback, '');
},
/**
* 加载模块
* @param url
*/
loadMod: function (url) {
var p = P();
if (!url) {
p.resolve();
return p;
}
if (url instanceof Array) {
p.resolve();
for (var i = 0; i < url.length; i++) {
(function () {
var load = Rap.loadMod(url[i]);
p = p.then(function () {
return load;
});
})();
}
return p;
} else {
url = url.substr(url.indexOf('/') == 0 ? 1 : 0);
var modUrl = this.baseUrl + url;
var path = url.split('/');
var modName = path.join('_');
path.pop();
url = path.join("/") + "/";
if (Vue.component(modName)) {
p.resolve();
return p;
}
var key = Cache.getKey(modUrl + '.ver');
var key_content = modUrl + '.rap';
var version = Rap.RapAppVersion;
if (compVersion[modUrl]) {
version = compVersion[modUrl];
}
function loadFromNet() {
var template = "";
var style = "";
var script = "";
var link = modUrl;
if (modUrl.indexOf('/') != 0 && Rap.router_model == 'history') {
link = '/' + Rap.history_base + '/' + modUrl;
}
return rapGet(link + '.' + Rap.filePostfix + '?version=' + version).then(function (content) {
var el = document.createElement('div');
el.innerHTML = content;
var links = [];
for (var i = 0; i < el.children.length; i++) {
var child = el.children[i];
if (child.tagName == "STYLE") {
style = child.innerHTML.trim();
for (var varName in cssVar) {
var name = eval("/var\\(--" + varName.trim() + "\\)/g");
var value = cssVar[varName];
style = style.replace(name, value);
}
} else if (child.tagName == "TEMPLATE") {
template = child.innerHTML;
} else if (child.tagName == "SCRIPT") {
script = child.innerHTML;
} else if (child.tagName == "LINK") {
var href = child.getAttribute('href');
links.push(href);
}
}
var promise = Rap.promise().resolve();
for (var i = 0; i < links.length; i++) {
(function (ii) {
promise = promise.then(function () {
return rapGet(links[ii]).then(function (content) {
style += "\b\r" + content;
});
})
})(i);
}
return promise;
}).then(function () {
var compile = Vue.compile(template);
var lc = {
render: compile.render,
staticRenderFns: compile.staticRenderFns,
style: style,
script: script,
name: key_content,
base: url
};
var lc_json = {
render: compile.render.toString(),
staticRenderFns: compile.staticRenderFns.toString(),
style: style,
script: script,
name: key_content,
base: url
};
Cache.set(key_content, JSON.stringify(lc_json));
return lc;
}).then(function (lc) {
return evalScript(modUrl, url, modName, lc);
}).catch(function (e) {
console.error('url:' + link + " get error");
console.error(e);
p.reject(e);
});
}
if (!Rap.debug) {
Cache.get(key_content).then(function (content) {
if (content) {
var json = JSON.parse(content);
evalScript(modUrl, url, modName, json).then(function () {
p.resolve();
});
} else {
loadFromNet().then(function () {
p.resolve();
});
}
}).catch(function () {
loadFromNet().then(function () {
p.resolve();
});
});
return p;
}
return loadFromNet();
}
},
$query: function () {
var routerUrl = Rap.routerUrl();
var query = {};
if (routerUrl.indexOf('?') > -1) {
routerUrl = routerUrl.substr(routerUrl.indexOf('?') + 1);
var qs = routerUrl.split('&');
for (var i = 0; i < qs.length; i++) {
var q = qs[i];
if (!q)continue;
var kv = q.split('=');
query[kv[0]] = kv[1];
}
}
return query;
},
$search: function (ids) {
var routerUrl = Rap.routerUrl();
routerUrl = routerUrl.substr(routerUrl.indexOf('/') == 1 ? 2 : 1);
if (routerUrl.indexOf('?') > -1) {
routerUrl = routerUrl.substr(0, routerUrl.indexOf('?'));
}
var path = routerUrl.split('/');
var m = [];
var args = arguments;
for (var i = 0; i < path.length; i++) {
var item = path[i];
if (parseInt(item) + "" == item) {
m.push(item);
} else if (item.indexOf('@') == 0) {
m.push(item.substr(1));
}
}
if (args.length > 0) {
var result = {};
for (i = 0; i < args.length; i++) {
result[args[i]] = m[i];
}
return result;
}
return m;
}
, routerCall: null,
onHash: function (call) {
this.routerCall = call;
},
onRouter: function (call) {
this.routerCall = call;
},
onRouterChange: function (e) {
if (weixin_refresh_back) {
weixin_refresh_back = false;
return;
}
var routerUrl = Rap.routerUrl();
routerUrl = routerUrl.substr(routerUrl.indexOf('/') == 1 ? 2 : 1);
if (Rap.is_weixin && weixin_refresh_time && location.hash == weixin_refresh_time) {
weixin_refresh_back = true;
//微信浏览器的坑解决
history.back();
return;
}
var last = APPMainView.$children[APPMainView.$children.length - 1].$options._componentTag;
var to = routerUrl.split('/').join('_');
if (last == to) {
return;
}
if (!IS_GO && APPMainView.$children.length > 0) {
var index = APPMainView.keep_include.indexOf(last);
if (index > -1 && index == APPMainView.keep_include.length - 1) {
APPMainView.keep_include.splice(index, 1);
}
}
if (IS_GO && APPMainView.$children.length > 0) {
var index = APPMainView.keep_include.indexOf(to);
if (index > -1) {
APPMainView.keep_include.splice(index, 1);
}
}
if (IS_GO) {
document.documentElement.scrollTop = 0;
document.body.scrollTop = 0;
window.scrollTo(0, 0);
}
IS_GO = false;
if (Rap.routerCall && Rap.routerCall(routerUrl)) {
return;
}
if (!routerUrl && Rap.default_page) {
Rap.replace(Rap.default_page);
return;
}
Rap.global_router.hash = routerUrl;
if (routerUrl.indexOf('?') > -1) {
routerUrl = routerUrl.substr(0, routerUrl.indexOf('?'));
} else if (routerUrl.indexOf('&') > -1) {
routerUrl = routerUrl.substr(0, routerUrl.indexOf('&'));
} else if (routerUrl.indexOf('&') > -1) {
routerUrl = routerUrl.substr(0, routerUrl.indexOf('&'));
}
for (var key in Rap.global_router.query) {
delete Rap.global_router.query[key]
}
var query = Rap.$query();
for (var key in query) {
var value = query[key];
if (parseInt(value) + "" == value) {
value = parseInt(value);
}
Vue.set(Rap.global_router.query, key, value);
}
Rap.global_router.search.length = 0;
var search = Rap.$search();
for (var i = 0; i < search.length; i++) {
var value = search[i];
if (parseInt(value) + "" == value) {
value = parseInt(value);
}
Rap.global_router.search.push(value);
}
Rap.loadUrl(routerUrl);
},
onViewChangeCallBack: null,
onViewChange: function (fun) {
this.onViewChangeCallBack = fun;
},
router: function (hash, as) {
hash = hash.substr(hash.indexOf('/') == 0 ? 1 : 0);
as = as.substr(as.indexOf('/') == 0 ? 1 : 0);
routers[hash] = as;
}
};
var RapShareData = {
RapViews: {
index: 1,
items: []
},
router: Rap.global_router,
childView: '',
keep_include: []
};
var APPMainView = null;
Rap.MainView = {
data: function () {
return RapShareData;
},
watch: {
'RapViews.index': {
handler: function () {
this.childView = this.RapViews.items[0];
}, deep: true
}
},
created: function () {
APPMainView = this;
if (this.RapViews.items.length > 0) {
this.childView = this.RapViews.items[0];
}
}
};
function currentItems(items, view) {
items.push(view);
var layout = viewLines[view];
if (layout) {
currentItems(items, layout);
}
}
Rap.onViewChange(function (view) {
var items = [];
currentItems(items, view);
items = items.reverse();
RapShareData.RapViews.items = items;
RapShareData.RapViews.index++;
});
window.addEventListener("popstate", Rap.onRouterChange, false);
Rap.build = function (file) {
var p = P();
var div = document.createElement("div");
div.setAttribute('style', "width: 100%;height: 100%; background: white;position: fixed;left: 0;right: 0;text-align: center;padding-top: 30px;top: 0;z-index: 10000;font-size: 21px;");
div.textContent = "编译中...";
document.body.appendChild(div);
function buildJs() {
var is_base = file?true:false;
Cache.pack(is_base).then(function (js) {
doSave(js, 'application/json', 'rap-all.json');
p.resolve(js);
});
}
function doSave(value, type, name) {
var blob;
if (typeof window.Blob == "function") {
blob = new Blob([value], {type: type});
} else {
var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder;
var bb = new BlobBuilder();
bb.append(value);
blob = bb.getBlob(type);
}
var URL = window.URL || window.webkitURL;
var bloburl = URL.createObjectURL(blob);
var anchor = document.createElement("a");
if ('download' in anchor) {
anchor.style.visibility = "hidden";
anchor.href = bloburl;
anchor.download = name;
document.body.appendChild(anchor);
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
anchor.dispatchEvent(evt);
document.body.removeChild(anchor);
} else {
location.href = bloburl;
}
}
if (!file) {
file='/home/lyfile';
}
rapGet(file).then(function (content) {
content = JSON.parse(content);
function renderUrl() {
var url = content.pop();
Rap.redirectTo("/"+url);
if (content.length > 0) {
setTimeout(renderUrl, 500);
} else {
buildJs();
}
}
renderUrl();
});
return p;
};
Rap.get = rapGet;
Rap.promise = P;
Rap.cache = Cache;
//弹框
Rap.showPopup = function (url, config) {
Rap.loadMod(url).then(function () {
url = url.split('/').join('_');
if (!config) {
config = {};
}
if (url.indexOf('_') == 0) {
url = url.substr(1);
}
config.el = document.createElement('div');
if (!config.methods) {
config.methods = {};
}
config.methods.$close = function (done) {
if (done && typeof(done) == 'function') {
done();
}
instance.$el.parentElement.removeChild(instance.$el);
instance.$destroy();
};
var instance = new (Vue.extend(Vue.component(url)))(config);
document.body.appendChild(instance.$el);
});
};
Rap.version = '2.1.0';
function checkUa() {
var ua = window.navigator.appVersion.toLowerCase();
Rap.is_weixin = ua.indexOf('micromessenger') > -1;
Rap.is_android = ua.indexOf('android') > -1;
Rap.is_iphone = ua.indexOf('iphone') > -1;
Rap.is_ipad = ua.indexOf('ipad') > -1;
Rap.is_ios = Rap.is_iphone || Rap.is_ipad;
Rap.is_android_mobile = Rap.is_android && ua.indexOf('mobile') > -1;
Rap.is_android_pad = Rap.is_android && ua.indexOf('mobile') == -1;
}
if (location.hash.indexOf('#weixin_refresh_time') > -1) {
history.replaceState(null, '', "#");
}
checkUa();
return Rap;
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/tengzhinei/Vue-rap.git
git@gitee.com:tengzhinei/Vue-rap.git
tengzhinei
Vue-rap
Vue-rap
master

搜索帮助