📄 正在查看:static/admin/js/global.js
大小:14,292 字节 · 修改:2014-04-07 07:15:32 · 行数:436
1$(function() {
2 loadTab();
3 setTab();
4});
5
6// 通王 Ajax
7window.isIE6 = window.VBArray && !window.XMLHttpRequest;
8window.twAjax = {
9 //加载半透明效果
10 loading : function() {
11 twAjax.remove();
12 window.isIE6 && twAjax.unObj();
13 $("body").prepend('<div class="ajaxoverlay"></div><div class="ajaxtips"><div class="ajaximg"></div></div>');
14 if(window.isIE6) $(".ajaxoverlay").css({"width":document.documentElement.clientWidth, "height":document.documentElement.clientHeight});
15 $(window).resize(twAjax.setTopLeft);
16 twAjax.setTopLeft();
17 },
18
19 //隐藏object,select
20 unObj : function() {
21 $("object,select").each(function(){
22 if($(this).css("visibility") != "hidden") $(this).attr("_tw_bugs_visibility_tw_", $(this).css("visibility")).css("visibility", "hidden");
23 });
24 },
25
26 //显示object,select
27 disObj : function() {
28 $("[_tw_bugs_visibility_tw_]").each(function(){
29 $(this).css("visibility", $(this).attr("_tw_bugs_visibility_tw_")).removeAttr("_tw_bugs_visibility_tw_");
30 });
31 },
32
33 //删除半透明框和提示框
34 remove : function() {
35 document.onkeydown = null;
36 window.isIE6 && twAjax.disObj();
37 $(".ajaxoverlay,.ajaxtips").remove();
38 },
39
40 //关闭
41 close : function() {
42 $(".ajaxtips").animate({top:0}, 250, twAjax.remove);
43 },
44
45 //设置提示框位置
46 setTopLeft : function(H) {
47 if($(".ajaxtips").length == 0) return;
48 $(".ajaxtips").css({"top":twAjax.getHeight(H), "left":twAjax.getWidth()});
49 },
50
51 getHeight : function(H) {
52 if(window.isIE6) {
53 return document.documentElement.scrollTop+(document.documentElement.clientHeight-$(".ajaxtips").height())/2-(typeof H == 'number' ? H : 0);
54 }else{
55 return ($(".ajaxoverlay").height()-$(".ajaxtips").height())/2-(typeof H == 'number' ? H : 0);
56 }
57 },
58
59 getWidth : function() {
60 return ($(".ajaxoverlay").width()-$(".ajaxtips").width())/2;
61 },
62
63 //设置提示框动画
64 setTopAn : function(H) {
65 var T = twAjax.getHeight(H);
66 $(".ajaxtips").css({"top":0, "left":twAjax.getWidth()}).animate({top:T+10}, 150).animate({top:T-20}, 150).animate({top:T}, 150);
67 },
68
69 //写入对话框代码
70 tipsHtml : function(str) {
71 if($(".ajaxtips").length == 0) twAjax.loading();
72 $(".ajaxtips").html(str);
73
74 $(".ajaxbox").width("auto");
75 var W = $(".ajaxbox").width()+5;
76 $(".ajaxbox").css({"width":(W>850?850:(W<180?180:W))});
77 },
78
79 //调试程序
80 debug : function(data) {
81 var msg = "<div style='width:100%;overflow:auto;'><b>" + data.kp_error + "</b></div>";
82 twAjax.tipsHtml('<div class="ajaxbox bfalse">'+ msg +'<u>\u6211\u77E5\u9053\u4E86</u></div>');
83 twAjax.setTopAn();
84
85 $(".ajaxtips u").click(twAjax.close);
86 },
87
88 //提示框
89 alert : function(data) {
90 window.twData = data = toJson(data);
91 if(window.twExit) return;
92
93 twAjax.tipsHtml('<div class="ajaxbox b'+ (data.err==0 ? true : false) +'"><i></i><b>'+ data.msg +'</b><u>\u6211\u77E5\u9053\u4E86</u></div>');
94 twAjax.setTopAn();
95
96 $(".ajaxtips u").click(function(){
97 twAjax.close();
98 if(!window.twName && data.name != '') $("[name='"+data.name+"']").focus();
99 });
100 if(!window.twErr && data.err==0) setTimeout(twAjax.close, 1000);
101 },
102
103 //确定框
104 confirm : function(msg, func) {
105 twAjax.tipsHtml('<div class="ajaxbox bnote"><i></i><b>'+ msg +'</b></div>');
106 $(".ajaxbox").append('<p class="cf"><a id="noA" class="but3">取消</a><a id="okA" class="but3">确认</a></p>');
107 twAjax.setTopAn();
108
109 $("#noA,#okA").attr("href","javascript:;");
110 $("#noA").click(twAjax.close);
111 $("#okA").click(function(){ twAjax.remove(); func(); });
112
113 document.onkeydown = function(e) {
114 var e = window.event || e;
115 var k = e.which || e.keyCode;
116 if(k == 27) {
117 twAjax.close();
118 }else if(k == 13) {
119 twAjax.remove();
120 func();
121 }
122 }
123 },
124
125 //提交表单
126 submit : function(selector, callback) {
127 $(selector).submit(function(){
128 twAjax.postd($(this).attr("action"), $(this).serialize(), callback);
129 return false;
130 });
131 },
132
133 //提交数据(加强版,具有加载和提示框功能)
134 postd : function(url, param, callback) {
135 twAjax.loading();
136 twAjax.post(url, param, (!callback ? twAjax.alert : callback));
137 },
138
139 //提交数据
140 post : function(url, param, callback) {
141 $.ajax({
142 type : "POST",
143 cache : false,
144 url : url,
145 data : param,
146 success : callback,
147 error : function(html){
148 alert("提交数据失败,代码:"+ html.status +",请稍候再试");
149 }
150 });
151 },
152
153 //获取数据
154 get : function(url, callback) {
155 $.ajax({
156 type : "GET",
157 cache : true,
158 url : url,
159 success : callback,
160 error : function(html){
161 alert("获取数据失败,代码:"+ html.status +",请稍候再试");
162 }
163 });
164 }
165};
166
167// 通王 dialog
168$.twDialog = function(options) {
169 if(options == "open") { $("#twdialog").show(); return false;
170 }else if(options == "close") { $("#twdialog").hide(); return false;
171 }else if(options == "remove") { $("#twdialog").remove(); $(window).off("resize", resize_position); return false;
172 }else if($("#twdialog").length) { alert("已存在一个对话框了,不允许再创建!"); return false; }
173 var objd, tval, dx, dy, sx, sy, objH, objW, bWidth, bHeight, left, top, maxLeft, maxTop, newH, newW;
174 var defaults = {
175 title:"标题",
176 open:true,
177 modal:true,
178 resizable:true,
179 width:600,
180 height:300,
181 top:"center",
182 left:"center",
183 zIndex:199,
184 minW:300,
185 minH:150,
186 remove:true
187 };
188 var o = $.extend(defaults, options);
189
190 //init
191 $("body").append('<div id="twdialog"><div id="twdialogbox"><div id="twdialog_title"><span></span><a href="javascript:;">close</a></div><div id="twdialog_content"><div style="padding:8px">玩命加载中...</div></div><div id="twdialog_button"><input type="button" value="确定" class="but1 ok"><input type="button" value="取消" class="but1 close"></div></div></div>');
192
193 objd = $("#twdialogbox");
194 if(o.content) $("#twdialog_content").html(o.content);
195 $("#twdialog_title span").html(o.title);
196 if(o.open) { $("#twdialog").show(); }else { $("#twdialog").hide(); }
197 if(o.modal) {
198 $("#twdialog").prepend('<div id="twoverlay"></div>');
199 $("#twoverlay").css({"z-index":o.zIndex-1, "width":document.documentElement.clientWidth, "height":document.documentElement.clientHeight});
200 }
201
202 //resizable
203 if(o.resizable) objd.append('<div id="twdialog_resizable_n"></div><div id="twdialog_resizable_e"></div><div id="twdialog_resizable_s"></div><div id="twdialog_resizable_w"></div><div id="twdialog_resizable_nw"></div><div id="twdialog_resizable_ne"></div><div id="twdialog_resizable_sw"></div><div id="twdialog_resizable_se"></div>');
204
205 //初始位置
206 objd.css({"width":o.width, "height":o.height, "z-index":o.zIndex});
207 if(o.top == "center") {objd.css("top",getTop())}else{objd.css("top",o.top)}
208 if(o.left == "center") {objd.css("left",getLeft())}else{objd.css("left",o.left)}
209 _setH();
210
211 //触发拖动
212 $("#twdialog_title,#twdialog_resizable_n,#twdialog_resizable_e,#twdialog_resizable_s,#twdialog_resizable_w,#twdialog_resizable_nw,#twdialog_resizable_ne,#twdialog_resizable_sw,#twdialog_resizable_se").mousedown(function(e){
213 objd = $(this).parent();
214 $("html,body,#twdialog").css("user-select","none");
215 document.onselectstart = objd[0].onselectstart = function(){return false};
216 if(!tval) tval = $(this).attr("id");
217 dx=e.pageX,dy=e.pageY,sx=objd.position().left,sy=objd.position().top,objH=objd.height(),objW=objd.width(),bWidth=document.documentElement.clientWidth,bHeight=document.documentElement.clientHeight;
218 });
219
220 //关闭拖动
221 $(document).mouseup(function(){
222 if(objd) {
223 $("html,body,#twdialog").css("user-select","auto");
224 document.onselectstart = objd[0].onselectstart = function(){return true};
225 }
226 if(tval) tval = null;
227 });
228
229 function _setH() { $("#twdialog_content").css("height", objd.height()-$("#twdialog_title").height()-$("#twdialog_button").height()-7); }
230 function _n(e) { top=e.pageY-(dy-sy); newH = dy-top+objH; if(newH>o.minH && top>=0) objd.css({"top": top, "height": newH}); _setH(); }
231 function _e(e) { left=e.pageX-(dx-sx); newW=left-sx+objW; if(newW>o.minW && e.pageX<bWidth-(objW-(dx-sx-1))) objd.css({"width": newW}); }
232 function _s(e) { top=e.pageY-(dy-sy); newH=top-sy+objH; if(newH>o.minH && e.pageY<bHeight-(objH-(dy-sy-1))) objd.css({"height": newH}); _setH(); }
233 function _w(e) { left=e.pageX-(dx-sx); newW=objW-(left-sx); if(newW>o.minW && left>=0) objd.css({"left": left, "width": newW}); }
234 function getTop(){return Math.max(0, (document.documentElement.clientHeight-objd.height())/2);}
235 function getLeft(){return Math.max(0, (document.documentElement.clientWidth-objd.width())/2);}
236
237 //获得鼠标指针在页面中的位置
238 $(document).mousemove(function(e){
239 switch(tval) {
240 case "twdialog_title":
241 left=e.pageX-(dx-sx), top=e.pageY-(dy-sy), maxLeft=bWidth-objd.width()-2, maxTop=bHeight-objd.height()-2;
242 left = Math.max(0, Math.min(maxLeft, left)); top = Math.max(0, Math.min(maxTop, top)); objd.css({"left": left, "top": top});
243 break;
244 case "twdialog_resizable_n":
245 _n(e); break;
246 case "twdialog_resizable_e":
247 _e(e); break;
248 case "twdialog_resizable_s":
249 _s(e); break;
250 case "twdialog_resizable_w":
251 _w(e); break;
252 case "twdialog_resizable_nw":
253 _n(e); _w(e); break;
254 case "twdialog_resizable_ne":
255 _n(e); _e(e); break;
256 case "twdialog_resizable_sw":
257 _s(e); _w(e); break;
258 case "twdialog_resizable_se":
259 _s(e); _e(e); break;
260 }
261 });
262
263 var resize_position = function() {
264 var obj=$("#twdialogbox"), p=obj.position(), objW=obj.width(), objH=obj.height(), bodyW=document.documentElement.clientWidth, bodyH=document.documentElement.clientHeight;
265 $("#twoverlay").css({"width":bodyW, "height":bodyH});
266 if(p.left+objW+2 > bodyW) obj.css("left", Math.max(bodyW-objW-2, 0));
267 if(p.top+objH+2 > bodyH) obj.css("top", Math.max(bodyH-objH-2, 0));
268 }
269 $(window).on("resize", resize_position);
270
271 //关闭
272 $("#twdialog_title a,#twdialog_button .close").click(function(){
273 if(o.remove) { $.twDialog("remove"); }else{ $.twDialog("close"); }
274 });
275};
276
277//加载JS
278function twLoadJs() {
279 var args = arguments;
280
281 //循环加载JS
282 var load = function(i) {
283 if(typeof args[i] == 'string') {
284 var file = args[i];
285
286 // 不重复加载
287 var tags = document.getElementsByTagName('script');
288 for(var j=0; j<tags.length; j++) {
289 if(tags[j].src.indexOf(file) != -1) {
290 if(i < args.length) load(i+1);
291 return;
292 }
293 }
294
295 var script = document.createElement("script");
296 script.type = "text/javascript";
297 script.src = file;
298
299 // callback next
300 if(i < args.length) {
301 // Attach handlers for all browsers
302 script.onload = script.onreadystatechange = function() {
303 if(!script.readyState || /loaded|complete/.test(script.readyState)) {
304 // Handle memory leak in IE
305 script.onload = script.onreadystatechange = null;
306
307 // Remove the script (取消移除,判断重复加载时需要读 script 标签)
308 //if(script.parentNode) { script.parentNode.removeChild(script); }
309
310 // Dereference the script
311 script = null;
312
313 load(i+1);
314 }
315 };
316 }
317 document.getElementsByTagName('head')[0].appendChild(script);
318 }else if(typeof args[i] == 'function') {
319 args[i]();
320 if(i < args.length) {
321 load(i+1);
322 }
323 }
324 }
325
326 load(0);
327}
328
329//加载CSS
330function twLoadCss(file) {
331 // 不重复加载
332 var tags = document.getElementsByTagName('link');
333 for(var j=0; j<tags.length; j++) {
334 if(tags[j].href.indexOf(file) != -1) {
335 return false;
336 }
337 }
338
339 var link = document.createElement("link");
340 link.rel = "stylesheet";
341 link.type = "text/css";
342 link.href = file;
343 document.getElementsByTagName('head')[0].appendChild(link);
344}
345
346//html转json
347function toJson(data) {
348 var json = {};
349 try{
350 json = eval("("+data+")");
351
352 if(json.kp_error) {
353 twAjax.debug(json);
354 window.twExit = true; // 用来终止程序执行
355 }else{
356 window.twExit = false;
357 }
358 }catch(e){
359 alert(data);
360 }
361 return json;
362}
363
364function time() {
365 return (new Date).getTime();
366}
367
368function P(str) {
369 return parent.$(str);
370}
371
372function I(str) {
373 return document.getElementById(str);
374}
375
376//设置Tabul位置 Adder位置
377function setTabulAdder(){
378 var tabWidth = P("#box_tab").width();
379 var ulWidth = P("#box_tab ul").width();
380 var ulLeft = P("#box_tab ul").position().left;
381
382 if(ulWidth > tabWidth){
383 P("#leftbtn:hidden,#rightbtn:hidden").show();
384 if(P("#adder").position().left != tabWidth+35) P("#adder").css("left", tabWidth+35);
385
386 if(ulLeft < tabWidth-ulWidth) {
387 P("#box_tab ul").animate({left: tabWidth-ulWidth}, "fast");
388 }else{
389 var thisLeft = P("#box_tab ul li.on").offset().left;
390 var endLeft = 167+tabWidth-P("#box_tab ul li.on").width();
391 if(thisLeft > endLeft) P("#box_tab ul").animate({left: ulLeft-(thisLeft-endLeft)}, "fast");
392 }
393 }else{
394 P("#leftbtn:visible,#rightbtn:visible").hide();
395 P("#adder").offset({left: P("#box_tab ul li:last").offset().left + P("#box_tab ul li:last").width() });
396 if(ulLeft != 0 ) P("#box_tab ul").css("left", 0);
397 }
398}
399
400//父级加载Tab
401function loadTab() {
402 if(!parent.twRoot) return;
403
404 var title = $("title").html();
405 var url = location.search;
406 var i = P("#box_frame iframe[src='"+url+"']").index();
407 if(i == -1) {
408 i = P("#box_tab ul li[urlKey='"+urlKey+"']").index();
409 if(i == -1) return;
410 }
411
412 var obj1 = P("#box_tab ul");
413 var obj2 = obj1.children("li").eq(i);
414 var stopNum = url.indexOf("&r=");
415 var newUrl = (stopNum == -1) ? url : url.substring(0, stopNum);
416
417 obj1.width(obj1.width()+200);
418 obj2.attr({"url":newUrl, "urlKey":urlKey, "pKey":pKey, "title":title, "place":place});
419 obj2.children("b").html(title);
420 P("#box_place").html(place);
421
422 parent.setUlwidth();
423 setTabulAdder();
424 parent.loadMenu(pKey, "select");
425}
426
427//设置选项卡
428function setTab() {
429 $(".head dl dd").each(function(i){$(this).attr('i', i)}).click(function(){
430 $(".head dl dd").removeAttr("class");
431 $(this).addClass("on");
432 $(".p .cc").hide();
433 $(".p .cc").eq($(this).attr('i')).show();
434 });
435}
436