UNPKG

3.19 kBJavaScriptView Raw
1// 对象合并方法
2function assign(a, b) {
3 var newObj = {}
4 for (var key in a){
5 newObj[key] = a[key]
6 }
7 for (var key in b){
8 newObj[key] = b[key]
9 }
10 return newObj
11}
12
13// 运行页面所属的方法
14function runPageFunction (pageName, entryDom) {
15 // ozzx-name处理
16 window.ozzx.domList = {}
17 pgNameHandler(entryDom)
18
19 // 判断页面是否有自己的方法
20 var newPageFunction = window.ozzx.script[pageName]
21 if (!newPageFunction) return
22 // 注入运行环境
23 newPageFunction.created.apply(assign(newPageFunction, {
24 $el: entryDom,
25 activePage: window.ozzx.activePage,
26 domList: window.ozzx.domList
27 }))
28 // 判断页面是否有下属模板,如果有运行所有模板的初始化方法
29 for (var key in newPageFunction.template) {
30 var templateScript = newPageFunction.template[key]
31 if (templateScript.created) {
32 // 为模板注入运行环境
33 templateScript.created.apply(assign(newPageFunction.template[key], {
34 $el: entryDom,
35 activePage: window.ozzx.activePage,
36 domList: window.ozzx.domList
37 }))
38 }
39 }
40}
41
42// ozzx-name处理
43function pgNameHandler (dom) {
44 // 遍历每一个DOM节点
45 for (var i = 0; i < dom.children.length; i++) {
46 var tempDom = dom.children[i]
47
48 // 判断是否存在@name属性
49 var pgName = tempDom.attributes['@name']
50 if (pgName) {
51 // console.log(pgName.textContent)
52 window.ozzx.domList[pgName.textContent] = tempDom
53 }
54 // 判断是否有点击事件
55 var clickFunc = tempDom.attributes['@click']
56
57 if (clickFunc) {
58 tempDom.onclick = function() {
59 var clickFor = this.attributes['@click'].textContent
60 // 判断页面是否有自己的方法
61 var newPageFunction = window.ozzx.script[window.ozzx.activePage]
62
63 // console.log(this.attributes)
64 // 判断是否为模板
65 var templateName = this.attributes['template']
66 // console.log(templateName)
67 if (templateName) {
68 newPageFunction = newPageFunction.template[templateName.textContent]
69 }
70 // console.log(newPageFunction)
71 // 取出参数
72 var parameterArr = []
73 var parameterList = clickFor.match(/[^\(\)]+(?=\))/g)
74 if (parameterList && parameterList.length > 0) {
75 // 参数列表
76 parameterArr = parameterList[0].split(',')
77 clickFor = clickFor.replace('(' + parameterList + ')', '')
78 }
79 // console.log(newPageFunction)
80 // 如果有方法,则运行它
81 if (newPageFunction.methods[clickFor]) {
82 // 绑定window.ozzx对象
83 // console.log(tempDom)
84 newPageFunction.methods[clickFor].apply({
85 $el: tempDom,
86 activePage: window.ozzx.activePage,
87 domList: window.ozzx.domList,
88 data: newPageFunction.data,
89 methods: newPageFunction.methods
90 }, parameterArr)
91 }
92 }
93 }
94 // 递归处理所有子Dom结点
95 if (tempDom.children.length > 0) {
96 pgNameHandler(tempDom)
97 }
98 }
99}
\No newline at end of file