btc_wev8.js
10.2 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
function BTC(){
/*----------------和业务有关的公用函数Start-----------------*/
if(!window.getBTC){
if(!window.BTCArray){
window.BTCArray=[];
}
window.getBTC=(function(){
return new Function("id","for(var i=0;i<BTCArray.length;i++){if(BTCArray[i].id == id) return BTCArray[i]}");
})()
}
var typeObj = function(type){
return new Function('o', "return Object.prototype.toString.call(o)=='[object " + type + "]'");
};
var isObj = typeObj("Object");
var isArray = typeObj("Array");
var x;
var initAjax = function(){
try {
x = new ActiveXObject("Msxml2.XMLHTTP");//IE高版本创建XMLHTTP
}catch(E) {
try {
x = new ActiveXObject("Microsoft.XMLHTTP");//IE低版本创建XMLHTTP
}
catch(E) {
x = new XMLHttpRequest();//兼容非IE浏览器,直接创建XMLHTTP对象
}
}
return x;
}
/*----------------和业务有关的公用函数End-----------------*/
/*默认显示配置及用户当前配置*/
var dftCfg = {
id:(function(){
return new Date().getTime();
})(),
renderTo: null, //选框依附位置
headerURL:null, //标题栏地址
contentURL:null, //内容地址
autoLoadContent:true, //是否初始化时加载详细
contentHeight:null, //详细区域最大高度
contentHandler:null,//详细内容点击事件
needAdjustPos:true, //是否需要自动调整位置
splitNum:4 //详细内容每多少个分词换行
},curCfg = {};
var $btc = this;
$btc.version="1.0";
$btc.loaded = false;
$btc.pubDate="2014-12-9",
/**
* 存储所有子字段id
*/
$btc.itemids = [];
/**
* 存储所有分组id
*/
$btc.groupids = [];
/**
* 清空字段记忆
*/
$btc.reset = function(){
$this.itemids = [];
$this.groupids = [];
itemnames =[],itemtops=[],groups=[];
}
/**
* 过滤重复id字段
*/
$btc.multifilter = function(id,o){
for(var i=0;i<o.length;i++){
if(o[i] == id){
return false;
}
}
o.push(id);
return true;
}
$btc.apply=function(o,c,d){
if(d){
$btc.apply(o,d);
}
if(o&&c&&isObj(c)){
for( var p in c){
o[p] = c[p];
}
}
return o;
};
$btc.init=function(options){
$btc.apply(curCfg,options,dftCfg);
$btc.apply(this,curCfg);
window.BTCArray.push(this);
var _html = ["<div class='btc_container' id='container_"+curCfg.id+"'>",
"<div class = 'btc-header' style='background: #f5f5f5 !important;'>",
"<table class = 'btc-header-table'>",
"<colgroup>",
"<col width='70px'>",
"<col width='*'>",
"</colgroup>",
"<tr>",
"<td style='background: #f5f5f5 !important;'>",
"<ul class = 'btc-header-ul'>",
"<li onclick=javascript:getBTC('"+curCfg.id+"').contentAjaxHandler(this) class = 'btc-header-li li-highlight'>全部分类</li>",
"</ul>",
"</td><td style='background: #f5f5f5 !important;'>",
"<ul class = 'btc-header-ul' id='btc-header-ul"+curCfg.id+"'>",
"</ul>",
"</td>",
"</tr>",
"</table>",
"</div>",
"<div class = 'btc-browser' id='btc_"+curCfg.id+"'>",
"</div>",
"</div>"
];
curCfg.renderTo.append(_html.join(''));
if(!!curCfg.contentHeight){
$("#btc_"+curCfg.id).css("max-height",curCfg.contentHeight);
}
$btc.headAjaxHandler();
};
$btc.resetPosition = function(){
if(!curCfg.needAdjustPos){
return;
}
var $currBtc = $("#container_"+curCfg.id);
var left = $currBtc.offset().left;
var top = $currBtc.offset().top;
var width = $currBtc.width();
var height = $currBtc.height();
var winWidth = $(document.body).offset().left+$(document.body).width();
var winHeight = $(document.body).offset().top+$(document.body).height();
if(left+width > winWidth){
$currBtc.animate({
right:0
},500);
}
if(top+height>winHeight){
$("#btc_"+curCfg.id).animate({
maxHeight:winHeight-top-$(".btc-header").height()-50-10,
minHeight:70
},500);
$("#btc_"+curCfg.id).niceScroll();
}
if(window.width < $("#container_"+curCfg.id).width()){
$("#container_"+curCfg.id).width(window.width-5);
}
}
$btc.eventList=[];
$btc.addEvent=(function(){
return new Function("env","fn","obj","obj=obj||document;window.attachEvent?obj.detachEvent('on'+env,fn):obj.removeEventListener(env,fn,false);window.attachEvent?obj.attachEvent('on'+env,fn):obj.addEventListener(env,fn,false);this.eventList.push([env,fn,obj]);");
})();//事件绑定
$btc.detachEvent=(function(){
return new Function('env','fn','obj',"obj=obj||document;window.attachEvent?obj.detachEvent('on'+env,fn):obj.removeEventListener(env,fn,false);");
})(); //取消事件绑定
$btc.clickHandler = function(event){
var e = event || window.event;
var $container = $("#container_"+curCfg.id);
var mousePos = [e.clientX,e.clientY];
var containPos = [$container.offset().left,$container.offset().top];
var $height = $container.height();
var $width = $container.width();
if(mousePos[0]<containPos[0]||
mousePos[1]<containPos[1]||
mousePos[0]>containPos[0]+$width||
mousePos[1]>containPos[1]+$height){
$container.remove();
}
}
$btc.remove = function(){
$("#container_"+curCfg.id).remove();
}
$btc.setContainerStyle = function(o,c){
var t,s;
if(isArray(o)&&isArray(c)){
while(t = o.shift()){
s=c.shift();
$("#container_"+curCfg.id).css(t,s);
}
}
}
$btc.headAjaxHandler=function(){
if(!curCfg.headerURL){
return;
}
x = initAjax(); //创建XMLHttpRequest对象
x.open("post", curCfg.headerURL, true);
x.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
x.onreadystatechange = function(){
if (x.readyState == 4) {
if (x.status == 200) {
var json = eval('('+x.responseText+')');
var vk = json.map;
if(!isArray(vk)){
return;
}
var o;
while(o = vk.shift()){
$("#btc-header-ul"+curCfg.id).append('<li onclick=javascript:getBTC('+curCfg.id+').contentFilter(this,"'+o.key+'") class = "btc-header-li">'+o.value+'</li>');
}
if(curCfg.autoLoadContent){
$btc.contentAjaxHandler();
}
}
}
}; //指定响应函数
x.send(null);
};
$btc.contentAjaxHandler=function(){
if(!curCfg.contentURL){
return;
}
x = initAjax(); //创建XMLHttpRequest对象
x.open("post", curCfg.contentURL, true);
x.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
x.onreadystatechange = function(){
if (x.readyState == 4) {
if (x.status == 200) {
$btc.itemids = [];
$("#btc_"+curCfg.id).html('');
var json = eval('('+x.responseText+')');
var kv = json.groups;
if(!isArray(kv)){
return;
}
var o;
while(o = kv.shift()){
var _html = [];
if(kv.length == 0){
_html.push('<div id=group_'+o.groupid+' class = "btc-browser-item btc-browser-lastitem">');
}else{
_html.push('<div id=group_'+o.groupid+' class = "btc-browser-item">');
}
_html.push('<table align="center">');
_html.push('<colgroup><col width="62px"><col width="*"></colgroup><tr>');
_html.push('<td class="btc-browser-item-theader" style="background: #fff !important;width:62px !important;border-bottom:0px !important;">');
_html.push('<div class="btc-head-div" title='+o.groupname+'>');
_html.push(o.groupname);
_html.push('</div>');
_html.push('<span class="btc-browser-item-theader-span">|</span>');
_html.push('</td><td style="background: #fff !important;border-bottom:0px !important;">');
var p;
var m=0;
var mark = o.items.length;
while(p = o.items.shift()){
if($btc.multifilter(p.key,$btc.itemids)==false){
continue;
}
if(m%curCfg.splitNum==0&&m != 0){
_html.push('<br>');
}else{
if(o.items.length != mark-1){
_html.push('<span class="split">|</span>');
}
}
m++;
_html.push('<span class="item-to-choose" onclick="javascript:getBTC('+curCfg.id+').contentHandler('+p.key+')">'+p.value+'</span>');
}
if(o.items.length == 0){
_html.push(" ");
}
_html.push('</td></tr></table></div>');
$("#btc_"+curCfg.id).append(_html.join(''));
$("#btc_"+curCfg.id).niceScroll({
cursorwidth:"5px",
cursorcolor:"#3574d3",
horizrailenabled:false
});
}
$btc.resetPosition();
$btc.loaded = true;
}
}
}; //指定响应函数
x.send(null);
};
$btc.contentFilter=function(o,k){
if($btc.loaded == false){
$btc.contentAjaxHandler();
}
$(".btc-header-li-current").removeClass("btc-header-li-current");
$(o).addClass("btc-header-li-current");
var timetask = setInterval(function(){
if($btc.loaded == true){
$(".btc-browser-item").each(function(){
$(this).hide();
});
if(!!k){
$("#group_"+k).show();
$("#group_"+k).addClass("btc-browser-lastitem");
}else{
$(".btc-browser-item").each(function(){
$(this).show();
});
}
clearInterval(timetask);
}
},100);
}
}