/
task_2_18.js
168 lines (168 loc) · 3.72 KB
/
task_2_18.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* 返回id
* @param string
* @return htmlElement
* @zp
*/
var $ = function (id) {
return document.getElementById(id);
};
/**
* 页面js逻辑构造函数
* @param object
* @return null
* @zp
*/
var CancelQueue = function (obj) {
obj = obj || {};
this.input = obj.input || $("new-number");
this.container = obj.container || $("arr-area");
this.arr = [];
this.update = function () {
var childs = this.container.childNodes;
for (var i = 0, len = this.arr.length; i < len; i++) {
childs[i].setAttribute("data-index", i);
}
};
this.inQueue = function (type) {
var num = this.check();
if (num !== undefined) {
CancelQueue.publicInQueue[type].inArr.call(this, num);
CancelQueue.publicInQueue[type].inDom.call(this, num);
this.update();
}
};
this.outQueue = function (type) {
if (this.arr.length !== 0) {
CancelQueue.publicOutQueue[type].outArr.call(this);
CancelQueue.publicOutQueue[type].outDom.call(this);
this.update();
} else {
alert("该队列已经为空");
}
};
/*事件委托*/
this.childClick = function () {
var self = this;
this.container.onclick = function (event) {
if (event.target.tagName.toLowerCase() === "span") {
var index = event.target.getAttribute("data-index");
this.removeChild(this.childNodes[index]);
self.arr.splice(index, 1);
self.update();
}
};
};
};
CancelQueue.fn = CancelQueue.prototype;
/**
* 生成新的队列dom元素
* @param number
* @return HtmlSpanObejct
* @zp
*/
CancelQueue.fn.burnDom = function (number) {
var span = document.createElement("span"),
text = document.createTextNode(number);
span.appendChild(text);
return span;
};
/**
* 检验表单输入元素
* @param stirng
* @return true || undefined
* @zp
*/
CancelQueue.fn.check = function () {
var pattern = /^\d{1,3}$/,
strNum = this.input.value,
flag = pattern.test(strNum);
if (flag) {
this.input.value = "";
return strNum;
} else {
alert("您输入的数字有误!");
return void 0;
}
};
/**
* 对调用函数进行检验
* @zp
*/
CancelQueue.throwError = function () {
if (!(this instanceof CancelQueue)) {
throw new Error("该函数不能直接调用");
}
};
/**
* 入公共函数对象,添加至对象中
* 注意所有函数不能直接调用
* @zp
*/
CancelQueue.publicInQueue = {
left: {
inArr: function (num) {
CancelQueue.throwError.call(this);
this.arr.unshift(+num);
},
inDom: function (num) {
CancelQueue.throwError.call(this);
this.container.insertBefore(this.burnDom(num), this.container.childNodes[0]);
}
},
right: {
inArr: function (num) {
CancelQueue.throwError.call(this);
this.arr.push(+num);
},
inDom: function (num) {
CancelQueue.throwError.call(this);
this.container.appendChild(this.burnDom(num));
}
}
};
/**
* 出公共函数对象,添加至对象中
* 注意所有函数不能直接调用
* @zp
*/
CancelQueue.publicOutQueue = {
left: {
outArr: function (num) {
CancelQueue.throwError.call(this);
alert(this.arr.shift());
},
outDom: function (num) {
CancelQueue.throwError.call(this);
this.container.removeChild(this.container.firstElementChild);
}
},
right: {
outArr: function (num) {
CancelQueue.throwError.call(this);
alert(this.arr.pop());
},
outDom: function (num) {
CancelQueue.throwError.call(this);
this.container.removeChild(this.container.lastElementChild);
}
}
};
/*测试*/
(function (){
var test = new CancelQueue(),
buttons = $("button-area");
test.childClick();
$("left-in").onclick = function () {
test.inQueue("left");
};
$("right-in").onclick = function () {
test.inQueue("right");
};
$("left-out").onclick = function () {
test.outQueue("left");
};
$("right-out").onclick = function () {
test.outQueue("right");
};
})();