From d45af932f1ea656db3d92233e7f5acd3e2e5eb9e Mon Sep 17 00:00:00 2001 From: shaoying Date: Sat, 12 Dec 2020 17:54:16 +0800 Subject: [PATCH] opt proto.js --- index.html | 2 +- index.js | 2 +- pane/Action.js | 1 + proto.js | 97 ++++++++++++++++++++------------------------------ 4 files changed, 42 insertions(+), 60 deletions(-) diff --git a/index.html b/index.html index 5170f840..5820bdbb 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - + volcanos diff --git a/index.js b/index.js index 1b4af85f..065105d7 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ Volcanos({name: "chat", iceberg: "/chat/", volcano: "/frame.js", {name: "River", help: "群聊组", pos: "left"}, {name: "Action", help: "工作台", pos: "middle"}, {name: "Footer", help: "状态条", pos: "foot", state: ["ncmd", "keys"]}, - ], main: {name: "Header", engine: "remote", list: ["publish/order.js"]}, plugin: [ + ], main: {name: "Header", engine: "remote", list: ["/publish/order.js"]}, plugin: [ "/plugin/state.js", "/plugin/input.js", "/plugin/table.js", diff --git a/pane/Action.js b/pane/Action.js index 53e6e01c..ded67062 100644 --- a/pane/Action.js +++ b/pane/Action.js @@ -21,6 +21,7 @@ Volcanos("onimport", {help: "导入数据", list: [], _init: function(can, msg, // 添加插件 can.onappend._init(can, value, ["/plugin/state.js"], function(plugin) { + can._plugins = (can._plugins||[]).concat([plugin]) plugin.run = function(event, cmds, cb, silent) { var msg = plugin.request(event); cmds = cmds || [] can.run(event, can.onengine[cmds[0]]? cmds: [river, storm, value.action].concat(cmds), function(msg) { typeof cb == "function" && cb(msg) diff --git a/proto.js b/proto.js index 223889ae..4a62e471 100644 --- a/proto.js +++ b/proto.js @@ -1,8 +1,8 @@ function shy(help, meta, list, cb) { - var index = -1, value = "", type = "string", args = arguments; function next(check) { - if (++index >= args.length) {return false} - if (check && check != typeof args[index]) {index--; return false} - return value = args[index], type = typeof value, value + var index = -1, args = arguments; function next(check) { + if (++index >= args.length) { return } + if (check && check != typeof args[index]) { index--; return } + return args[index] } var cb = arguments[arguments.length-1] || function() {} @@ -14,15 +14,16 @@ function shy(help, meta, list, cb) { var Volcanos = shy("火山架", {libs: [], cache: {}, index: 1}, [], function(name, can, libs, cb) { var meta = arguments.callee.meta, list = arguments.callee.list if (typeof name == "object") { var Config = name - meta.volcano = Config.volcano, meta.libs = Config.libs + // 预加载 var Preload = Config.libs; Config.panes.forEach(function(pane) { Preload = Preload.concat(pane.list = pane.list || ["/pane/"+pane.name+".css", "/pane/"+pane.name+".js"]) - Preload = Preload.concat(pane.list = pane.list || ["/pane/"+pane.name+".js"]) }); Preload = Preload.concat(Config.plugin) + // 根模块 + meta.libs = Config.libs, meta.volcano = Config.volcano name = Config.name, can = { _follow: Config.name, - _width: window.innerWidth, _height: window.innerHeight, _target: document.body, _head: document.head, _body: document.body, + _width: window.innerWidth, _height: window.innerHeight, }, libs = Preload.concat(Config.volcano), cb = function(can) { can.onengine._init(can, can.Conf(Config), [], function(msg) { can.base.Log(can) @@ -31,30 +32,24 @@ var Volcanos = shy("火山架", {libs: [], cache: {}, index: 1}, [], function(na } var conf = {}, conf_cb = {} - can = can || {}, list.push(can) && (can.__proto__ = {__proto__: Volcanos.meta, _name: name, _create_time: new Date(), _load: function(name, cb) { + list.push(can = can || {}), can.__proto__ = {__proto__: Volcanos.meta, _name: name, _load: function(name, cb) { + // 加载缓存 for (var cache = meta.cache[name] || []; meta.index < list.length; meta.index++) { - if (name == "/plugin/input/date.css" && cache.length > 0) { continue } - if (name == "/lib/base.js" && cache.length > 0) { continue } - if (list[meta.index] == can) { continue } - cache.push(list[meta.index]) - // 加载缓存 - } + list[meta.index] != can && cache.push(list[meta.index]) + }; meta.cache[name] = cache - for (var i = 0; i < cache.length; i++) { - if (can[cache[i]._name] && can[cache[i]._name]._merge && can[cache[i]._name]._merge(can, cache[i])) { continue } - if (typeof cb == "function" && cb(can, name, cache[i])) { continue} - if (can[cache[i]._name]) { - for (var k in cache[i]) { - can[cache[i]._name].hasOwnProperty(k) || (can[cache[i]._name][k] = cache[i][k]) + // 加载模块 + for (var i = 0; i < cache.length; i++) { var sub = cache[i] + if (can[sub._name] && can[sub._name]._merge && can[sub._name]._merge(can, sub)) { continue } + if (typeof cb == "function" && cb(can, name, sub)) { continue} + if (can[sub._name]) { + for (var k in sub) { + can[sub._name].hasOwnProperty(k) || (can[sub._name][k] = sub[k]) } } else { - can[cache[i]._name] = cache[i] + can[sub._name] = sub } - - - // 加载索引 } - meta.cache[name] = cache }, require: function(libs, cb, each) { if (!libs || libs.length == 0) { typeof cb == "function" && setTimeout(function() { cb(can) }, 10) @@ -66,60 +61,46 @@ var Volcanos = shy("火山架", {libs: [], cache: {}, index: 1}, [], function(na if (meta.cache[source]) { can._load(source, each), can.require(libs.slice(1), cb, each) - return // 缓存加载 + return // 加载缓存 } - if (source.endsWith(".js")) { var script = document.createElement("script") - if (can.user && source.indexOf("publish") == 0) { - source += "?pod="+(can.user.Search(can, "pod")||"") - } - if (can.user && source.indexOf("/publish") == 0) { + if (source.endsWith(".css")) { var style = document.createElement("link") + style.rel = "stylesheet", style.type = "text/css" + style.href = source; style.onload = function() { + can._load(source, each), can.require(libs.slice(1), cb, each) + } // 加载样式 + target.appendChild(style) + + } else if (source.endsWith(".js")) { var script = document.createElement("script") + if (source.indexOf("/publish") == 0 && can.user) { source += "?pod="+(can.user.Search(can, "pod")||"") } script.src = source, script.onload = function() { can._load(source, each), can.require(libs.slice(1), cb, each) } // 加载脚本 target.appendChild(script) - - } else if (source.endsWith(".css")) { var style = document.createElement("link") - style.rel = "stylesheet", style.type = "text/css" - style.href = source; style.onload = function() { - can._load(source, each), can.require(libs.slice(1), cb, each) - } // 加载样式 - target.appendChild(style) } }, request: function(event, option) { event = event || {} - if (event._msg) { - can.core.Item(option, event._msg.Option) - return event._msg - } - - var ls = (can._name||can._help).split("/") - event._pane = ls[ls.length-1] - - var msg = can.misc.Message(event, can) - can.core.Item(option, msg.Option) - return event._msg = msg + event._msg = event._msg || can.misc.Message(event, can) + can.core.Item(option, event._msg.Option) + return event._msg }, - Conf: function(key, value, cb) { + Conf: function(key, value) { if (key == undefined) { return conf } if (typeof key == "object") { conf = key; return conf } - typeof cb == "function" && (conf_cb[key] = cb) - if (value != undefined) { var old = conf[key], res; - conf[key] = conf_cb[key] && (res = conf_cb[key](value, old, key)) != undefined && res || value - } + conf[key] = value == undefined? conf[key]: value + if (conf[key] == undefined && key.indexOf(".") > 0) { - var p = conf, ls = key.split(".") - while (p && ls.length > 0) { + var p = conf, ls = key.split("."); while (p && ls.length > 0) { p = p[ls[0]], ls = ls.slice(1) } return p } - return conf[key] || "" + return conf[key] }, - }) + } if (_can_name) { meta.cache[_can_name] = meta.cache[_can_name] || []