UNPKG

4.56 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 // console.log(newPageFunction)
23 // 如果有created方法则执行
24 if (newPageFunction.created) {
25 // 注入运行环境
26 newPageFunction.created.apply(assign(newPageFunction, {
27 $el: entryDom,
28 data: newPageFunction.data,
29 activePage: window.ozzx.activePage,
30 domList: window.ozzx.domList
31 }))
32 }
33
34 // 判断页面是否有下属模板,如果有运行所有模板的初始化方法
35 for (var key in newPageFunction.template) {
36 var templateScript = newPageFunction.template[key]
37 if (templateScript.created) {
38 // 获取到当前配置页的DOM
39 // 待修复,临时获取方式,这种方式获取到的dom不准确
40 var domList = entryDom.getElementsByClassName('ox-' + key)
41 if (domList.length !== 1){
42 console.error('我就说会有问题吧!')
43 console.log(domList)
44 }
45 // 为模板注入运行环境
46 templateScript.created.apply(assign(newPageFunction.template[key], {
47 $el: domList[0].children[0],
48 data: templateScript.data,
49 activePage: window.ozzx.activePage,
50 domList: window.ozzx.domList
51 }))
52 }
53 }
54}
55
56// ozzx-name处理
57function pgNameHandler (dom) {
58 // 遍历每一个DOM节点
59 for (var i = 0; i < dom.children.length; i++) {
60 var tempDom = dom.children[i]
61
62 // 判断是否存在@name属性
63 var pgName = tempDom.attributes['@name']
64 if (pgName) {
65 // console.log(pgName.textContent)
66 // 隐藏元素
67 tempDom.hide = function () {
68 this.style.display = 'none'
69 }
70 window.ozzx.domList[pgName.textContent] = tempDom
71 }
72 // 判断是否有点击事件
73 var clickFunc = tempDom.attributes['@click']
74
75 if (clickFunc) {
76
77 tempDom.onclick = function() {
78 var clickFor = this.attributes['@click'].textContent
79 // 判断页面是否有自己的方法
80 var newPageFunction = window.ozzx.script[window.ozzx.activePage]
81 // console.log(this.attributes)
82 // 判断是否为模板
83 var templateName = this.attributes['template']
84 // console.log(templateName)
85 if (templateName) {
86 newPageFunction = newPageFunction.template[templateName.textContent]
87 }
88 // console.log(newPageFunction)
89 // 取出参数
90 var parameterArr = []
91 var parameterList = clickFor.match(/[^\(\)]+(?=\))/g)
92
93 if (parameterList && parameterList.length > 0) {
94 // 参数列表
95 parameterArr = parameterList[0].split(',')
96 // 进一步处理参数
97
98 for (var i = 0; i < parameterArr.length; i++) {
99 var parameterValue = parameterArr[i].replace(/(^\s*)|(\s*$)/g, "")
100 // console.log(parameterValue)
101 // 判断参数是否为一个字符串
102
103 if (parameterValue.charAt(0) === '"' && parameterValue.charAt(parameterValue.length - 1) === '"') {
104 parameterArr[i] = parameterValue.substring(1, parameterValue.length - 1)
105 }
106 if (parameterValue.charAt(0) === "'" && parameterValue.charAt(parameterValue.length - 1) === "'") {
107 parameterArr[i] = parameterValue.substring(1, parameterValue.length - 1)
108 }
109 // console.log(parameterArr[i])
110 }
111 clickFor = clickFor.replace('(' + parameterList + ')', '')
112 }
113 // console.log(newPageFunction)
114 // 如果有方法,则运行它
115 if (newPageFunction[clickFor]) {
116 // 绑定window.ozzx对象
117 // console.log(tempDom)
118 // 待测试不知道这样合并会不会对其它地方造成影响
119 newPageFunction.$el = this
120 newPageFunction.domList = window.ozzx.domList
121 newPageFunction[clickFor].apply(newPageFunction, parameterArr)
122 }
123 }
124 }
125 // 递归处理所有子Dom结点
126 if (tempDom.children.length > 0) {
127 pgNameHandler(tempDom)
128 }
129 }
130}
\No newline at end of file