-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo14.js
199 lines (171 loc) · 4.95 KB
/
demo14.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Created by Administrator on 2017/8/30 0030.
*/
//组合模式--整体是对部分的组合,这样就简化了复杂的整体,通过不同的部分组合又丰富了
//整体
var News = function(){
this.children = [];
this.element = null;
}
News.prototype = {
init : function(){
throw new Error("请重写你的方法")
},
add : function(){
throw new Error("请重写你的方法")
},
getElement : function(){
throw new Error("请重写你的方法")
}
}
var Container = function(id,parent){
News.call(this);
this.id = id;
this.parent = parent;
this.init();
}
inheritPrototype(Container,News);
Container.prototype.init = function(){
this.element = document.cteateElement('ul');
this.element.id =this.id;
this.element.className ='new-container';
}
Container.prototype.add = function(child){
this.children.push(child);
this.element.appendChild(child.getElement());
return this;
}
Container.prototype.getElment = function(){
return this.element;
}
Container.prototype.show = function(){
this.parent.appendChild(this.element)
}
var Item = function(className){
News.call(this);
this.className = className || '';
this.init();
}
inheritPrototype(Item,News);
Item.prototype.init = function(){
this.element = document.cteateElement('li');
this.element.id =this.id;
this.element.className =this.className;
}
Item.prototype.add = function(child){
this.children.push(child);
this.element.appendChild(child.getElement());
return this;
}
Item.prototype.getElment = function(){
return this.element;
}
var NewsGroup = function(className){
News.call(this);
this.className = className || '';
this.init();
}
inheritPrototype(Item,News);
NewsGroup.prototype.init = function(){
this.element = document.cteateElement('div');
this.element.className =this.className;
}
NewsGroup.prototype.add = function(child){
this.children.push(child);
this.element.appendChild(child.getElement());
return this;
}
NewsGroup.prototype.getElment = function(){
return this.element;
}
var ImageNews = function(url,href,classname){
News.call(this);
this.url = url || '';
this.href = href || '#';
this.className = className || '';
this.init();
}
inheritPrototype(ImageNews,News);
ImageNews.prototype.init = function(){
this.element = document.cteateElement('a');
var img = new Image();
img.src = this.url;
this.element.appendChild(img)
this.element.className ='image-news' + this.className;
this.element.href = this.href;
}
ImageNews.prototype.add = function(){}
ImageNews.prototype.getElment = function(){
return this.element;
}
var IconNews = function(text,href,type){
News.call(this);
this.text = text || '';
this.href = href || '#';
this.type = type || 'video';
this.init()
}
inheritPrototype(IconNews,News);
IconNews.prototype.init = function(){
this.element = document.createElement('a');
this.element.innerHTML = this.text;
this.element.href = this.href;
this.element.className = 'icon' + this.type;
}
IconNews.prototype.add = function(){};
IconNews.prototype.getElement = function(){
return this.element;
}
var EasyNews = function(text,href){
News.call(this);
this.text = text || '';
this.href = href || '';
this.init()
}
inheritPrototype(EasyNews,News);
EasyNews.prototype.init = function(){
this.element = document.createElement('a');
this.element.innerHTML = this.text;
this.element.href = this.href;
this.element.className = 'text';
}
EasyNews.prototype.add = function(){};
EasyNews.prototype.getElement = function(){
return this.element;
}
var TypeNews = function(text,href,type,pos){
News.call(this);
this.text = text || '';
this.href = href || '';
this.type = type || '';
this.pos = pos || 'left';
this.init()
}
inheritPrototype(TypeNews,News);
TypeNews.prototype.init = function(){
this.element = document.createElement('a');
if(this.pos === 'left'){
this.element.innerHTML = '[' + this.type + ']' + this.text;
}else{
this.element.innerHTML = this.text + '[' + this.type +']';
}
this.element.href = href;
this.element.className = 'text';
}
EasyNews.prototype.add = function(){};
EasyNews.prototype.getElement = function(){
return this.element;
}
//把新闻模块创建出来
var news = new Container('news',document.body);
news.add( new Item('normal').add( new IconNews('哈哈哈哈哈','#','video' ))).add(
new Item('normal').add( new ItemNews('呵呵呵呵呵呵呵'),'#','live' )
).add(
new Item('normal').add( new NewsGroup('has-img').add( new ImageNews('img/1.jpg','#','small').add(
new EasyNews('吼吼吼吼吼吼','#').add( new EsyNews('啥啥啥啥啥'),'#' )
) ) )
).add( new Item('normal').add( new TypeNews('咯咯咯咯咯咯'),'#','NBA','left' )).add(
new Item('normal').add(
new TypeNews('叽叽叽叽叽叽','#','CBA','right')
)
).show()