Signin  Pricing  Examples  Support

Libraries > dlg.js

dlg.js - Search Live Website Code
It's like doing CTRL+F on millions of websites' source code



NerdyData is a search engine for source code. Search the source code of 200 million websites.


JAVASCRIPT FILE http://gambit.gg/public/js/dlg.js


Libraries on gambit.gg


core.js

dlg.js

gs.js

en.js

scroll.js

tabs.js

jquery.js

modernizr.custom.js

news-js.js

router.js

setup.js

timezone.js

viewer.js



Explore More


css_browser_selector.js

mootools.js

jquery-1.9.1.min.js

jquery.nivo.slider.js

dlg.js
				
/**
 * Dialog box.
 */
var DLG = {
	/**
	 * Define the array to store modals instances
	 */
	modals: {},
	/**
	 * Define the id of DLG node
	 */
	id: 'dlgBox',
	insertAfter: '#main',
	/**
	 * Array of ids
	 */
	ids: [],
	/**
	 * Cache dlg body node
	 */
	dlgBody: null,
	/**
	 * Defining an array for control buttons to evoide recursion
	 */
	containers: {},
	/**
	 * Has the class been initialized?
	 */
	initialized: false,
	/**
	 * Is DLG visiable?
	 */
	_is_shown:false,
	/**
	 * DLG instance
	 */
	instance: null,
	/**
	 * Default options
	 */
	options: {
		key: null,
		moveable:false,
		shader:false,
		scroll:false,
		close:true,
		ctrl:true,
		btn:false,
		fullscreen:false,
		animTime: 300,
		top: null,
		maxWidth:null,
		maxHeight:null,
		containProportions:false,
		offset:10,
		reset:true
	},
	
	win: null,
	doc: null,
	
	deferred: new $.Deferred(),
	
	/**
	 * Initialize Dialog Box.
	 *
	 * @constructor
	 */
	initialize: function(win, doc) {
		if(this.initialized)
			return false;
		
		this.win = $(win);
		this.doc = $(doc);
		this.instance = this;
		
		this.initialized = true;
		console.log('[DLG] initialized');
	},
	
	/**
	 * Initialize Dialog Box.
	 *
	 * @constructor
	 */
	init: function(options) {
		//console.log('[DLG] initializing...');
		//this.options = $.extend({}, this.options, options);
		var m = this;
		var d = this.modals[options.key] ? this.modals[options.key].resetBox() : null;
		
		if(!d) {
			var deleyed = {
				show: function() {
					
					d = m.buildDLG(options);
					
					m.ids.push(m.id +'_'+ parseInt(m.ids.length + 1));
					m.modals[options.key] = d;
					//console.log('[DLG] initialized.');
				}
			};
			this.deferred.promise(deleyed);
			var delay = true;
			if(delay)
				this.deferred.resolve();
			
			deleyed.done(function() {
				deleyed.show();
			});
		}
		
		return d;
	},
	
	/**
	 * Builds a DLG object.
	 *
	 * @return a dlgBox object.
	 */
	buildDLG: function(options) {
		var dlgBox = {
			_is_shown:false,
			options: $.extend({}, this.options, options),
			containers: {},
			onShowCallback: null,
			onHideCallback: null,
			t: null,
			prevHeight: 0,
			_deleyedPosCenter: false,
			_resizeingAnimation: false,
			_resizingToW: null,
			_resizingToH: null,
			_initW: null,
			_initH: null,
			/**
			 * Add element to DLG
			 *
			 * @constructor
			 */
			addElement: function(options) {
				var defaults = {
					type:'div',
					attr:{
						className:'row'
					}
				}
				
				var options = $.extend({}, defaults, options);
				
				if(!options.target)
					App.ce(options.type, options.attr, this.containers.content);
				else {
					App.ce(options.type, options.attr, $(options.target, this.containers.content));
				}
				
				return this;
			},
			
			/**
			 * Add a control button to DLG
			 *
			 * @attibutes
			 */
			addControl: function(options) {
				var defaults = {
					type:'button',
					busy:false,
					attr:{
						className:'button'
					}
				}
				
				var options = $.extend({}, defaults, options);
				
				if(options.busy) {
					App.cbb({
						id: options.attr.id,
						busyLabel: options.attr.busyLabel,
						label: options.attr.innerHTML,
						baseClass: options.attr.className,
						onClick: options.onClick
					},
					this.containers.ctrlHolder);
				}
				else {
					if(options.onClick)
						App.ce(options.type, options.attr, this.containers.ctrlHolder).click(function(e){ Core.preventDefault(e); options.onClick(); });
					else
						App.ce(options.type, options.attr, this.containers.ctrlHolder);
				}
				
				return this;
			},
			
			/**
			 * Resets inner content of DLG
			 *
			 * @param
			 */
			resetBox: function() {
				//console.log('[DLG] reseting');
				this.containers.title.html('');
				this.containers.title.hide();
				
				this.containers.close.hide();
				
				this.containers.inner.css({height:"",width:""});
				this.containers.content.html('');
				this.options.innerHTML = '';
				
				this.containers.btnHolder.html('');
				this.containers.btnHolder.hide();
				
				this.containers.ctrlHolder.html('');
				this.containers.ctrlHolder.hide();
				
				this.containers.shader.hide();
				//this.containers.scroller.hide();
				//this.containers.scrollPointer.css({top:0});
				
				this.containers.body.css({top:"",left:""});
				DLG.win.off(".dlg_center");
				DLG.win.off(".wheelScrollBox");
				
				return this;
			},
			
			/**
			 * Show Dialog Box.
			 *
			 * @attributes
			 */
			show: function(key) {
				console.log('[DLG] showing');
				m = key ? DLG.modals[key] : this;
				
				if(m.options.shader)
					m.containers.shader.show();
				
				if(m.options.title) {
					m.containers.title.show();
					m.containers.title.html(m.options.title);
				}
				
				if(m.options.close)
					m.containers.close.show();
				
				if(m.options.innerHTML)
					m.containers.content.html(m.options.innerHTML);
				
				if(m.options.btn)
					m.containers.btnHolder.show();
				
				if(m.options.ctrl)
					m.containers.ctrlHolder.show();
				
				if(m.options.fullscreen) {
					m.place((this.options.maxWidth && this.options.maxWidth < (DLG.win.width() - (this.options.offset * 2))) ? (DLG.win.width() - this.options.maxWidth - (this.options.offset * 2))/2 : this.options.offset, (this.options.maxHeight && this.options.maxHeight < (DLG.win.height() - (this.options.offset * 2))) ? (DLG.win.height() - this.options.maxHeight - (this.options.offset * 2))/2 : this.options.offset);
					m.resize({w:DLG.win.width() - parseInt(m.containers.body.outerWidth(true) - m.containers.inner.width()) - (this.options.offset * 2), h:DLG.win.height() - parseInt(m.containers.body.outerHeight(true) - m.containers.inner.height()) - (this.options.offset * 2), animated:true});
				}
				else {
					m.resize();
				}
				
				var ml = $('[data-type="main-layout"]');
				if(ml.length > 0)
					ml.addClass('fixed').data("offset", ml.position().top).css({top:(0 - Page.scroll.top) + ml.position().top, width:"100%"});
				
				m.containers.wrapper.css({ visibility:"visible" });
				m.containers.body.removeClass('invisible');
				m._is_shown = true;
				m.onShow();
				//m.options.scroll ? m.checkState() : null;
				m.options.scroll ? $(m.containers.wrapper).customScroll({ padding:4, showOnHover:false, usePadding:true }) : null;
				
				DLG.win.off(".dlg_center");
				DLG.win.on("resize.dlg_center", {dlg:m}, function(e) { e.data.dlg.options.maxWidth = DLG.win.width(); e.data.dlg.options.maxHeight = DLG.win.height(); e.data.dlg.resize({containProportions:true}).placeCenter(); });
				
				return m;
			},
			
			/**
			 * Hide Dialog Box.
			 *
			 * @param
			 */
			hide: function(key) {
				console.log("[DLG] hiding");
				m = key ? DLG.modals[key] : this;
				m.onHide();
				m.containers.wrapper.css({ visibility:"hidden" });
				m.containers.body.addClass('invisible');
				m.containers.shader.hide();
				//m.containers.scroller.hide();
				
				var ml = $('[data-type="main-layout"]');
				if(ml.length > 0)
				{
					var p = Math.abs(ml.position().top - ml.data("offset"));
					ml.removeAttr("style").removeClass('fixed');
					$('body, html').scrollTop(p);
				}
				
				if(m.options.remove)
				{
					m.containers.wrapper.remove();
					//m.containers.body.remove();
					//m.containers.shader.remove();
					//m.containers.scroller.remove();
					delete DLG.modals[this.options.key];
					DLG.ids.splice(DLG.ids.indexOf(DLG.id +'_'+ parseInt(DLG.ids.length)), 1);
				}
				else if(m.options.reset)
					m.resetBox();
				m._is_shown = false;
				
				DLG.win.off(".dlg_center");
				DLG.win.off(".wheelScrollBox");
				
				return m;
			},
			
			onShow: function(callback) {
				if(callback)
					this.onShowCallback = callback;
				else {
					if($.isFunction(this.onShowCallback))
						this.onShowCallback();
					else
						eval(this.onShowCallback);
				}
				
				return this;
			},
			
			onHide: function(callback) {
				if(callback)
					this.onHideCallback = callback;
				else {
					if($.isFunction(this.onHideCallback))
						this.onHideCallback();
					else
						eval(this.onHideCallback);
				}
				
				return this;
			},
			
			/**
			 * Place box in the center of a page.
			 *
			 * @param
			 */
			placeCenter: function(animated) {
				//console.log("[DLG] placing center");
				this._deleyedPosCenter =  this._resizeingAnimation ? true : false;
				var x = (DLG.win.width() - this.containers.body.outerWidth(true))/2,
					y = this.options.top != null ? this.options.top : (DLG.win.height()/2 - this.containers.body.outerHeight(true)/2);
				
				if(y < 0) y = this.options.offset;
				if(x < 0) x = this.options.offset;
				
				if(animated) {
					this.containers.body.animate({
						top: y,
						left: x
					},
					this.options.animTime || 300);
				}
				else {
					this.containers.body.css({
						top: y,
						left: x
					});
				}
				
				return this;
			},
			
			/**
			 * Place box in the specific position of a page.
			 *
			 * @param x int
			 * @param y int
			 */
			place: function(x, y) {
				var m = this;
				
				m._deleyedPosCenter = false;
				if(!x && !y && !m._resizingToW && !m._resizingToW)
					return this;
				else if(!x && !y) {
					x = (DLG.win.width() - (m._resizingToW + parseInt(m.containers.body.outerWidth(true) - m.containers.inner.width())))/2;
					y = (DLG.win.height() - (m._resizingToH + parseInt(m.containers.body.outerHeight(true) - m.containers.inner.height())))/2;
				}
				
				m.containers.body.animate({
					top: y,
					left: x
				},
				m.options.animTime || 300);
				
				return m;
			},
			
			/**
			 * Reposition a box.
			 *
			 * @param
			 */
			reposition: function() {
				var m = this, moveEvt;
				var stopMoveHor = false, stopMoveVer = false;
				m.addClass("moveable");
				m.containers.bar.on("mousedown", function(evt) {
					Core.preventDefault(evt);
					Core.stopPropagation(evt);
					var el = m.containers.body;
					var top = parseInt(m.containers.body.css("top"));
					var left = parseInt(m.containers.body.css("left"));
					var width = parseInt(m.containers.body.outerWidth(true));
					var height = parseInt(m.containers.body.outerHeight(true));
					var topPos, leftPos;
					
					DLG.win.on("mousemove.reposition", function(e) {
						stopMoveVer = false;
						stopMoveHor = false;
						if((el.offset().top + height) > DLG.win.height()) {
							topPos = DLG.win.height() - height;
							stopMoveVer = true;
						}
						else if(el.offset().top < 0) {
							topPos = 0;
							stopMoveVer = true;
						}
						else {
							topPos = top + (e.clientY - evt.clientY);
							if(topPos < 0) topPos = 0;
							else if((topPos + height) > DLG.win.height()) topPos = DLG.win.height() - height;
							stopMoveVer = false;
						}
						
						if((el.offset().left + width) > DLG.win.width()) {
							leftPos = DLG.win.width() - width;
							stopMoveHor = true;
						}
						else if(el.offset().left < 0) {
							leftPos = 0;
							stopMoveHor = true;
						}
						else {
							leftPos = left + (e.clientX - evt.clientX);
							if(leftPos < 0) leftPos = 0;
							else if((leftPos + width) > DLG.win.width()) leftPos = DLG.win.width() - width;
							stopMoveHor = false;
						}
						
						if(!stopMoveVer)
							m.containers.body.css("top", topPos);
						if(!stopMoveHor)
							m.containers.body.css("left", leftPos);
					});
				});
				
				DLG.win.on("mouseup", function(evt) {
					DLG.win.off(".reposition");
				});
				
				return m;
			},
			
			setInitSize: function(w, h) {
				//console.log("[DLG] setting initial dimensions");
				this._initW = w;
				this._initH = h;
				
				return this;
			},
			
			/**
			 * Resize box.
			 *
			 * @param width
			 * @param height
			 * @function callback
			 */
			resize: function(options, callback) {
				//console.log("[DLG] resizing");
				m = this;
				
				var defaults = {
					w: null,
					h: null,
					containProportions: false,
					animated: false
				}
				
				var options = $.extend({}, defaults, options);
				
				if(typeof this.options.maxWidth == "undefined" || !this.options.maxWidth)
					this.options.maxWidth = DLG.win.width();
				if(typeof this.options.maxHeight == "undefined" || !this.options.maxHeight)
					this.options.maxHeight = DLG.win.height();
				
				var tempW, tempH, intoW, intoH;
				
				if((typeof options.w == 'undefined' || typeof options.w == 'object') && (typeof options.h == 'undefined' || typeof options.h == 'object')) {
					if((this.containers.body.outerWidth(true) + (this.options.offset * 2)) > this.options.maxWidth)
						intoW = this.options.maxWidth - parseInt(this.containers.body.outerWidth(true) - this.containers.inner.width()) - (this.options.offset * 2);
					else if(this._initW < this.options.maxWidth)
						intoW = this._initW;
					else
						intoW = parseInt(this.containers.body.outerWidth(true) - this.containers.inner.width());
					
					options.w = intoW;
					options.h = options.containProportions ? Math.floor(this._initH / this._initW * intoW) : this._initH;
				}
				else {
					if((options.w + parseInt(this.containers.body.outerWidth(true) - this.containers.inner.width())) > this.options.maxWidth)
						intoW = this.options.maxWidth - parseInt(this.containers.body.outerWidth(true) - this.containers.inner.width()) - (this.options.offset * 2);
					if((options.h + parseInt(this.containers.body.outerHeight(true) - this.containers.inner.height())) > this.options.maxHeight)
						intoH = this.options.maxHeight - parseInt(this.containers.body.outerHeight(true) - this.containers.inner.height()) - (this.options.offset * 2);
				}
				
				if(typeof intoW != "undefined" && typeof intoH == "undefined") {
					tempW = intoW;
					tempH = options.containProportions ? Math.floor(options.h / options.w * tempW) : options.h;
				}
				else if(typeof intoW == "undefined" && typeof intoH != "undefined") {
					tempH = intoH;
					tempW = options.containProportions ? Math.floor(options.w / options.h * tempH) : options.w;
				}
				else if(typeof intoW != "undefined" && typeof intoH != "undefined") {
					var a, b;
					a = Math.floor(options.h / options.w * intoW);
					b = Math.floor(options.w / options.h * intoH);
					if(a > intoH) {
						tempH = intoH;
						tempW = options.containProportions ? Math.floor(options.w / options.h * tempH) : intoW;
					}
					else if(b > intoW) {
						tempW = intoW;
						tempH = this.options.containProportions ? Math.floor(options.h / options.w * tempW) : intoH;
					}
				}
				else if(typeof intoW == "undefined" && typeof intoH == "undefined") {
					tempW = options.w;
					tempH = options.h;
				}
				
				w = tempW;
				h = tempH;
				this._resizingToW = tempW;
				this._resizingToH = tempH;
				
				if(DLG.win.width() < (w + (m.containers.close.position().left - m.containers.body.outerWidth(true)) + (m.containers.close.width() - m.options.offset)))
					m.containers.close.css('right', 0 - m.options.offset);
				if(DLG.win.height() < (h + (m.containers.close.height(true) - m.options.offset - m.containers.close.position().top)))
					m.containers.close.css('top', 0 - m.options.offset);
				
				if(typeof callback != "undefined") {
					var old_call = callback;
					callback = function() {
						if(m._deleyedPosCenter)
							m.placeCenter();
						m._resizingToW = null;
						m._resizingToH = null;
						return old_call() || null;
					}
				}
				
				if(w && h) {
					this._resizeingAnimation = true;
					if(options.animated) {
						this.containers.inner.animate({
							width: w,
							height: h
						},
						this.options.animTime || 300,
						callback);
						this.containers.title.animate({
							width: w
						},
						this.options.animTime || 300);
					}
					else {
						this._resizeingAnimation = false;
						this.containers.inner.css({ width: w, height: h });
						this.containers.title.css({ width: w });
						if(typeof callback != "undefined") callback();
					}
				}
				else if(!w && h) {
					this._resizeingAnimation = true;
					if(options.animated) {
						this.containers.inner.animate({
							height: h
						},
						this.options.animTime || 300,
						callback);
					}
					else {
						this._resizeingAnimation = false;
						this.containers.inner.css({ height: h });
						if(typeof callback != "undefined") callback();
					}
				}
				else if(w && !h) {
					this._resizeingAnimation = true;
					if(options.animated) {
						this.containers.inner.animate({
							width: w
						},
						this.options.animTime || 300,
						callback);
						this.containers.title.animate({
							width: w
						},
						this.options.animTime || 300);
					}
					else {
						this._resizeingAnimation = false;
						this.containers.inner.css({ width: w });
						this.containers.title.css({ width: w });
						if(typeof callback != "undefined") callback();
					}
				}
				
				return this;
			},
			
			isShown: function(key) {
				m = key ? DLG.modals[key] : this;
				return m._is_shown;
			},
			
			hideControls: function() {
				this.containers.ctrlHolder.hide();
				return this;
			},
			
			showControls: function() {
				this.containers.ctrlHolder.show();
				return this;
			},
			
			hideButtons: function() {
				this.containers.btnHolder.hide();
				return this;
			},
			
			showButtons: function() {
				this.containers.btnHolder.show();
				return this;
			},
			
			addClass: function(className) {
				this.containers.wrapper.addClass(className);
				return this;
			},
			removeClass: function(className) {
				this.containers.wrapper.removeClass(className);
				return this;
			},
			css: function(attr,value) {
				if(value) {
					this.containers.wrapper.css(attr,value);
					return this;
				}
				else
					return this.containers.wrapper.css(attr);
			},
			attr: function(id,value) {
				if(value) {
					this.containers.wrapper.attr(id,value);
					return this;
				}
				else
					return this.containers.wrapper.attr(attr);
			}
		};
		
		//console.log('[DLG] building...');
		dlgBox.containers.wrapper = $('<div id="'+ this.id +'_'+ parseInt(this.ids.length + 1) +'_wrapper" class="'+ this.id +'-wrapper"></div>').css("z-index", parseInt(1000 + this.ids.length - 1)).insertBefore(DLG.insertAfter);
		
		dlgBox.containers.shader = $('<div id="'+ this.id +'_'+ parseInt(this.ids.length + 1) +'_underlayer" class="'+ this.id +'-underlayer"></div>').css("z-index", parseInt(1000 + this.ids.length - 1)).appendTo(dlgBox.containers.wrapper).hide();
		
		//dlgBox.containers.scroller = $("<div/>").attr("id", this.id +"_"+ parseInt(this.ids.length + 1) +"_scroller").addClass(this.id +"-scroller").css("z-index", parseInt(2000 + this.ids.length - 1)).appendTo(dlgBox.containers.wrapper).hide();
		
		//dlgBox.containers.scrollPointer = $("<div/>").addClass(this.id +"-scroll").appendTo(dlgBox.containers.scroller);
				
		dlgBox.containers.body = $('<div id="'+ this.id +'_'+ parseInt(this.ids.length + 1) +'" class="'+ this.id +' invisible"></div>').css("z-index", parseInt(1000 + this.ids.length)).appendTo(dlgBox.containers.wrapper);
		
		dlgBox.containers.bar = $('<div class="'+ this.id +'-bar"></div>').appendTo(dlgBox.containers.body);
		
		dlgBox.containers.title = $('<div class="title"></div>').appendTo(dlgBox.containers.bar);
		
		dlgBox.containers.close = $('<div class="hide-wrap"><div class="hide">&times;</div></div>').appendTo(dlgBox.containers.bar).on('click', function(e) { Core.preventDefault(e); dlgBox.hide(); });
		
		dlgBox.containers.inner = $('<div class="'+ this.id +'-inner"></div>').appendTo(dlgBox.containers.body);
		
		dlgBox.containers.content = $('<div class="content"></div>').appendTo(dlgBox.containers.inner);
		
		dlgBox.containers.btnHolder = $('<div id="'+ this.id +'_'+ parseInt(this.ids.length + 1) +'_buttonsHolder" class="'+ this.id +'-buttonsHolder"></div>').appendTo(dlgBox.containers.body).hide();
		
		dlgBox.containers.ctrlHolder = $('<div id="'+ this.id +'_'+ parseInt(this.ids.length + 1) +'_controlsHolder" class="'+ this.id +'-controlsHolder"></div>').appendTo(dlgBox.containers.body).hide();
		
		DLG.win.keyup(function(e) { if(e.which == 27) DLG.modals[Core.lastKey(DLG.modals)] ? DLG.modals[Core.lastKey(DLG.modals)].hide() : false; });
		
		if(dlgBox.options.moveable) dlgBox.reposition();
		
		return dlgBox;
	}
};

$(function() {
	DLG.initialize(window, document);
});


A B C D E F G H I J K L M N O P Q R S T U V W X Y Z