PageSourceSearch

https://www.yougou.com/template/common/js/ygdialog.js

js yougou.com collected 2026-10-02 14:40:33 UTC 11,244 bytes, 448 lines download raw bytes

1/****************
2**ygDialog
3**2012-08-01 [email protected]
4update 2012-10-29 by xiao.y **框架高度自适应
5update 2012-12-04 by xiao.y **新增页面加载成功后回调
6*****************/
7(function () {
8var _skin="1";
9var _expando = 'ygDialog' + + new Date;
10var _count = 0;
11var _isIE6 = window.VBArray && !window.XMLHttpRequest;
12
13var ygDialog = function (config, ok, cancel) {
14	var defaults=ygDialog.defaults;
15	config =config || defaults || {};
16	for (var i in defaults) {
17		if (config[i] === undefined) config[i] = defaults[i];
18	};
19	config.id = config.id || _expando + _count;
20	_count ++;
21	return ygDialog.list[config.id] =new ygDialog.fn.init(config);
22}
23
24
25ygDialog.fn = ygDialog.prototype = {
26	init: function (config) {
27		if($(document).find("#"+config.id).length>0){
28			return;
29		}
30		var that = this,$wrap;
31		var lock,width,height,url,iframed,title,fixed,closable;
32		that.wrap=$wrap=$("<div>");
33		$wrap.addClass("ygDialog").html(that._templates);
34		$("body").prepend($wrap);
35		$title=$wrap.find(".uiTitle");
36		$content=$wrap.find(".dg_content");
37		$close=$wrap.find(".uiClose");
38		$skinWrap=$wrap.children(":first-child");
39		that.config = config;
40		that.tt=$title;
41		that.skinWrap=$skinWrap;
42		that.con=$content;
43		_skin=config.skin;
44
45		that.skin()
46		.lock()
47		.title(config.title)
48		.content(config.content);
49		if(config.url){
50			that.loading();
51		}
52		that.size(parseInt(config.width),parseInt(config.height));
53		that.position(config.left,config.top);
54		if(config.drag){
55			that.drag();
56		}
57
58		$(window).resize(function(){
59			 that.position(config.left,config.top);
60		});
61		$(window).scroll(function(){
62			that.ie6SelectFix();
63		});
64
65		if(config.closable){
66			$wrap.find(".uiClose").bind('click',function(){
67				that.close();
68			});
69		}
70
71		ygDialog.close=function(){that.close();}
72		config.init && config.init.call(that, window);
73
74		return that;
75	},
76	skin:function(){
77		var that=this,config=that.config;
78		if(config.id){
79			that.skinWrap.parent().attr("id",config.id);
80		}
81		that.skinWrap.attr("class","dialogSkin"+_skin);
82		return this;
83
84	},
85	loading:function(){
86		var that=this,wrap=that.wrap,config=that.config;
87		var loadDiv=$("<div>").addClass("dg_loading");
88		loadDiv.html('<img src="//s2.ygimg.cn/template/common/images/loading.gif"> 处理中,请稍候...');
89		//loadDiv.html('<img src="/template/common/images/loading16.gif" />');
90		that.loadDiv=loadDiv;
91		$("body").append(loadDiv);
92		if(_isIE6){
93			loadDiv.css({'top':loadDiv.offset().top+$(window).scrollTop()});
94		}
95		return that;
96	},
97	lock:function(){
98		var that=this,config=that.config;
99		if(!config.lock || $("#dialogMask").length>0) return that;
100		var mask=$("<div>").addClass("dialogMask");
101		if(_isIE6){
102			mask.html('<iframe src="about:blank" style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:-1;filter:alpha(opacity=0)"></iframe>');
103		}
104		$("body").append(mask);
105		that.mask=mask;
106		mask.height($(document).height());
107		$(window).scroll(function(){
108			mask.height($(document).height());
109		});
110		$(window).resize(function(){
111			mask.height($(document).height());
112		})
113		return that;
114	},
115	// 强制覆盖IE6下拉控件
116	ie6SelectFix:function () {
117		var that=this,config=that.config;
118		if(_isIE6 && !config.iframed)
119		{
120			var wrap = that.wrap,
121				expando = _expando + 'iframeMask',
122				w = wrap.width(),
123				h = wrap.height();
124
125			if ($("body").find("iframe").attr("id")==expando) {
126				iframe.width(w);
127				iframe.height(h);
128			} else {
129				iframe = $("<iframe>");
130				iframe.attr({"id":expando,"src":"about:blank"});
131				iframe.addClass('hackiframe').css({'position':'absolute','z-index':'-1','left':'0','top':'0','background':'#fff'});
132				iframe.width(w);
133				iframe.height(h);
134				wrap.append(iframe);
135			};
136		}
137	},
138	title:function(text){
139		var that=this,wrap=that.wrap,config=that.config;
140		var tt=that.tt;
141		if(_skin=="2"&& !config.title){
142			tt.remove();
143		}
144		if(!config.closable){
145			wrap.find(".uiClose").remove();
146		}
147//		if(_skin=="1"){
148//			wrap.find(".uiClose").html("关闭");
149//		}
150//		if(_skin=="3"){
151//			wrap.find(".uiClose").html("关闭");
152//		}
153		if(config.title){
154			tt.html(text || config.title);
155		}
156		return that;
157	},
158	iframeLoaded:function(){
159			var that=this,
160			iframe=that.iframe,
161			config=that.config,
162			w,h,
163			wrap=that.wrap;
164			iframe.prev('p').remove();
165			try{
166				w=iframe.contents().width();
167				h=iframe.contents().height();
168			}catch(e){
169				w=582;
170				h=380;
171			}
172			if(config.width){
173				w=config.width;
174			}
175			if(config.height){
176				h=config.height;
177			}
178			iframe.width(parseInt(w)).height(parseInt(h));
179			that.size(parseInt(w),parseInt(h));
180			that.position(config.left,config.top);
181			wrap.css({"visibility":"visible"});
182			that.loadDiv.remove();
183			if(typeof(config.loaded)=="function"){
184				config.loaded();
185			}
186			return that;
187	},
188	content:function(text){
189		var that=this,
190		wrap=that.wrap,
191		config=that.config,
192		con=that.con;
193
194		if(text){
195			con.html(text);
196			wrap.css({"visibility":"visible"});
197			if(typeof(config.loaded)=="function"){
198				config.loaded();
199			}
200		}
201
202		//ajax load
203		if(config.url &&!config.iframed){
204			con.hide();
205			con.load(config.url,function(responseTxt,statusTxt,xhr){
206				if(statusTxt=="success"){
207					setTimeout(function(){
208					var w=con.width();
209					var h=con.height();
210					if(config.width){
211						w=config.width;
212					}
213					if(config.height){
214						h=config.height;
215					}
216					that.size(parseInt(w),parseInt(h)).position();
217					con.show();
218					wrap.css({"visibility":"visible"});
219					that.loadDiv.remove();
220					},500);
221					if(typeof(config.loaded)=="function"){
222						config.loaded();
223					}
224				  }
225				});
226		}
227
228		//iframe load
229		if(config.url &&config.iframed){
230			var iframe = $("<iframe>");
231			iframe.attr("src",config.url);
232			iframe.attr("id","dialogIframe"+_count).attr("frameborder","0").attr("name","dialogIframe"+_count);
233			iframe.attr("scrolling","auto").css({"width":"100%","height":"100%"});
234			if (iframe[0].attachEvent){
235				iframe[0].attachEvent("onload", function(){
236						that.iframeLoaded();
237				});
238			} else {
239				iframe[0].onload = function(){
240						that.iframeLoaded();
241
242				};
243			}
244		}
245		con.append(iframe);
246		that.iframe=iframe;
247		that.ie6SelectFix();
248		return that;
249
250	},
251	close:function(){
252		var that=this;
253		var wrap=that.wrap,config=that.config;
254		var fn=config.close;
255		if(typeof fn === 'function')
256			config.close();
257		wrap.remove();
258		if(config.lock) that.unlock();
259		if($.browser.version=="6.0"&&$.browser.msie){
260			$("#hackDialogIframe").remove();
261		}
262		if(that.iframe){
263			that.iframe.attr('src','about:blank');
264			that.iframe.remove();
265		}
266		return that;
267	},
268	unlock:function(){
269		var that=this;
270		that.mask.remove();
271		return that;
272	},
273	size:function(w,h){
274		var that=this,
275		con=that.con,
276		config=that.config,
277		wrap=that.wrap,
278		wrapW=wrap.width(),
279		minW=config.minWidth,
280		minH=config.minHeight;
281		var _w = con.width();
282		var _h = con.height();
283		var _titH=wrap.find('.dg_title').height();
284		if(w){
285			_w=w;
286		}
287		if(h){
288			_h=h;
289		}
290		if(_w<minW){
291			_w=minW;
292		}
293		if(_h<minH-_titH){
294			_h=minH-_titH;
295		}
296
297		if(!config.url || w){
298			con.width(_w);
299		}
300		if(!config.url || h){
301			con.height(_h);
302		}
303		//chrome iframe hack
304		if(config.url && h){
305			con.css({'overflow':'hidden'});
306		}
307
308		if(_skin=="2"){
309		    wrap.width(parseInt(_w)+6);
310		}else if(_skin=="3"){
311			wrap.width(parseInt(_w)+10)
312		}else{
313		    wrap.width(parseInt(_w)+2);
314		}
315		wrap.find('.dg_title').width(_w);
316		wrap.height(parseInt(_h+_titH));
317		return that;
318	},
319	position:function(left,top){
320		var that=this,
321		wrap =that.wrap,
322		config=that.config,
323		con=that.con,
324		_sw=$(window).width(),
325		_sh=$(window).height(),
326		_w = con.width(),
327		_h = con.height(),
328		_titH=wrap.find('.dg_title').height();
329		if(config.width){
330			_w=config.width;
331		}
332		if(config.height){
333			_h=config.height;
334		}
335		var posL=left||(_sw-_w)/2;
336		var posT=top||(_sh-con.height()-_titH)/2;
337		if(_isIE6 || !config.fixed){
338			posT+=$(window).scrollTop();
339		}
340		wrap.css({left:posL,top:posT,'margin-left':'0','margin-top':'0'});
341		return that;
342	},
343	drag:function(){
344		var that=this,
345		wrap=that.wrap,
346		drg=wrap.find('.dg_title'),
347		bool=false,
348		offsetX=0,
349        offsetY=0;
350		drg.bind('mousedown',function(event){
351			bool=true;
352			var _this=$(this);
353			var ev=window.event || event;
354			offsetX = ev.clientX-wrap.offset().left;
355            offsetY =ev.clientY-wrap.offset().top;
356            _this.addClass('dg_drag_handle');
357		});
358		drg.bind('mouseup',function(){
359			bool=false;
360			$(this).removeClass('dg_drag_handle');
361		});
362		$(document).mousemove(function(event){
363            if(!bool)return;
364			var ev=window.event || event;
365			var x = ev.clientX-offsetX;
366            var y = ev.clientY-offsetY;
367			if(!_isIE6){
368				x=x-$(window).scrollLeft();
369				y=y-$(window).scrollTop();
370			}
371			var maxY=$(window).height()-wrap.height()-30;
372			var maxX=$(window).width()-wrap.width();
373			if(x<=0 || x>=maxX){return;}
374			if(y<=0 || y>=maxY){return;}
375            wrap.css({"left": x});
376            wrap.css({"top": y});
377         })
378	}
379};
380
381ygDialog.fn.init.prototype = ygDialog.fn;
382ygDialog.list = {};
383
384
385/**
386** 样式类型
387?skin=1
388**/
389var _thisScript="";
390(function (script, i, me) {
391	for (i in script) {
392		if (script[i].src && script[i].src.indexOf('ygdialog') !== -1) me = script[i];
393	};
394	_thisScript = me || script[script.length - 1];
395}(document.getElementsByTagName('script')));
396//_skin = _thisScript.src.split('skin=')[1];
397if(typeof(_skin)=="undefined") _skin="1";
398
399/**
400 ** 载入css
401 **/
402if ($("#link[href*=ygdialog.css]").length == 0){
403	var baseUrl="";
404	if(typeof cdnUrl!="undefined"){
405		baseUrl=cdnUrl;
406	}
407	var strHref = $('link').eq(0).attr('href');
408	var ver=strHref.split('?').pop();
409	baseUrl=baseUrl.replace("http:","");
410	var css_href =baseUrl+'/template/common/css/ygdialog.css?'+ver;
411	var styleTag = document.createElement("link");
412	styleTag.setAttribute('type', 'text/css');
413	styleTag.setAttribute('rel', 'stylesheet');
414	styleTag.setAttribute('href', css_href);
415	$("head")[0].appendChild(styleTag);
416}
417
418
419/**
420 ** 对话框模板
421 **/
422ygDialog.fn._templates =
423	'<div class="dialogSkin1">'
424	+'<div class="uiDiv">'
425	+'<table class="uiTable"><tbody><tr><td><div class="clearfix dg_title"><h3 class="uiTitle">&nbsp;</h3><a class="uiClose" href="javascript:;">&nbsp;</a></div></td></tr><tr><td><div class="dg_content"><p class="dg_loading"><image src="//s2.ygimg.cn/template/common/images/loading16.gif" /></p></div></td></tr></tbody></table></div></div>';
426
427
428
429/**
430 ** 默认配置
431 **/
432ygDialog.defaults = {
433	close: null,// 对话框关闭前执行的函数
434	loaded:null,//url加载完成回调
435	lock: true,	// 是否锁屏
436	closable:true, //是否允许关闭
437	fixed:true,
438	minHeight:100,
439	minWidth:200,
440	skin:1,
441	drag:false
442};
443
444ygDialog.close=function(){};
445
446window.ygDialog=ygDialog;
447})();
448var isYgDailogExist=true;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.