Signin  Pricing  Examples  Support

Libraries > viewer.js

viewer.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/viewer.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


html5shiv.js

conversion.js

utils.js

uikit-0a085188.js

viewer.js
				
var Viewer = {
	
	initialized: false,
	
	xhr: null,
	
	id: "viewer",
	
	count: 0,
	
	dlg: null,
	
	prevHeight: 0,
	
	curHeight: "",
	
	curWidth: "",
	
	maxHeight: null,
	
	maxWidth: null,
	
	timeout: null,
	
	animInProgress: false,
	
	digital_decode: function(val, length) {
		var length = parseInt(length);
		var str = new String(val);
		var cur_l = str.length;
		var dif = length - cur_l;
		if(dif < 0)
			return false;
		else if(dif == 0)
			return val;
		else {
			var ret = '';
			for(var i = 0; i < dif; i++)
				ret += '0';
			
			ret += val;
			return ret;
		}
	},
	
	prev_link: null,
	prev_title: null,
	current_link: null,
	current_title: null,
	
	values: {
		data: null
	},
	
	next: null,
	prev: null,
	
	options: {
		showNav: true,
		topOffset: null,
		leftOffset: null,
		comments: true,
		staticHeight: 720,
		staticWidth: 980
	},
	
	onInitCallback: null,
	onHideCallback: null,
	
	initialize: function(options)
	{
		if(Viewer.initialized)
			return false;
		
		this.options = $.extend({}, Viewer.options, options);
		//Core.loadDeferredScript(Core.staticUrl +'/js/player.js');
		Core.loadDeferredScript('http://vkontakte.ru/js/api/share.js?11');
		
		if($("#"+ this.id +"_wrapper").length > 0)
		{
			Viewer.windH = $(window).height();
			Viewer.containers = {};
			Viewer.containers.wrapper = $('#'+ this.id +'_wrapper');
			Viewer.containers.body = $('#'+ this.id, Viewer.containers.wrapper);
			Viewer.containers.bar = $('.'+ this.id +'-bar', Viewer.containers.body);
			Viewer.containers.title = $('.title', Viewer.containers.bar);
			Viewer.containers.close = $('.hide', Viewer.containers.bar).on('click', function(e) { Core.preventDefault(e); Viewer.hide(true); });
			Viewer.containers.inner = $('.'+ this.id +'-inner', Viewer.containers.body).on('click', function(e) { if(e.target !== this) { return; }; Core.preventDefault(e); Viewer.hide(true); });
			//Viewer.containers.inner.css({minHeight:Viewer.containers.wrapper.height() - Viewer.containers.wrapper.offset().top - (Viewer.containers.inner.outerHeight(true) - Viewer.containers.inner.height())});
			Viewer.containers.inner.css({minHeight:Viewer.windH});
			Viewer.containers.nav = $('.'+ this.id +'-nav', Viewer.containers.body);
			Viewer.containers.main = $('#main');
			Viewer.containers.main_inner = $('[role="main"]', Viewer.containers.main);
			Viewer.showNav(true);
			
			Viewer.curHeight = Viewer.containers.inner.height();
		}
		
		this.initialized = true;
		this.onInit(this);

	},
	
	getPrevLink: function(url) {
		if(!url)
			Viewer.prev_link = window.location.href;
		else
			Viewer.prev_link = url;
		//console.log(Viewer.prev_link);
	},
	
	getPrevTitle: function(title) {
		if(!title)
			Viewer.prev_title = document.title;
		else
			Viewer.prev_title = title;
	},
	
	/**
	 * Get media file.
	 *
	 * @param params
	 * @param type
	 * @param dir
	 */
	getHtml: function(url, title, b)
	{
		if(!Viewer.initialized)
			return false;
		
		Viewer.values.url = url;
		Viewer.values.title = title;
		Viewer.maxWidth = Page.object.width();
		Viewer.maxHeight = Page.object.height();
		Viewer.getPrevLink();
		Viewer.getPrevTitle();
		
		$('#main').addClass("fixed");
		
		if(!Viewer.containers)
		{	
			Viewer.windH = $(window).height();
			Viewer.containers = {};
			Viewer.containers.wrapper = $('<div id="'+ this.id +'_wrapper" class="'+ this.id +'-wrapper"></div>').css("z-index", 1000).insertBefore('#main');
			Viewer.containers.body = $('<div id="'+ this.id +'" class="scrollable '+ this.id +'"></div>').css("z-index", 2).appendTo(Viewer.containers.wrapper);
			Viewer.containers.bar = $('<div class="'+ this.id +'-bar"></div>').appendTo(Viewer.containers.body);
			Viewer.containers.title = $('<div class="title"></div>').appendTo(Viewer.containers.bar);
			Viewer.containers.close = $('<div class="hide">&times;</div>').appendTo(Viewer.containers.bar).on('click', function(e) { Core.preventDefault(e); Viewer.hide(true); }).wrap('<div class="hide-wrap" />');
			Viewer.containers.inner = $('<div class="'+ this.id +'-inner"></div>').appendTo(Viewer.containers.body).on('click', function(e) { if(e.target !== this) { return; }; Core.preventDefault(e); Viewer.hide(true); });
			//Viewer.containers.inner.css({minHeight:Viewer.containers.wrapper.height() - Viewer.containers.wrapper.offset().top - (Viewer.containers.inner.outerHeight(true) - Viewer.containers.inner.height())});
			Viewer.containers.inner.css({minHeight:Viewer.windH});
			Viewer.containers.nav = $('<div class="'+ this.id +'-nav">11</div>').appendTo(Viewer.containers.body);
			Viewer.showNav(true);
		}
		
		Viewer.containers.inner.html('<div class="content"></div>');
		Viewer.containers.wrapper.addClass('loading');
		
		Viewer.xhr ? Viewer.xhr.abort() : false;
		
		Viewer.xhr = $.ajax({
			type: 'GET',
			url: url + (url.search(/\?/)>=0?'&ajax=true':'?ajax=true'),
			dataType: 'html'
		})
		.done(function(response) {
			if(!b)
				Router.changeState(url, title +' - '+ Core.project);
			else
				Router.changeState(url, title +' - '+ Core.project, false);
			
			Viewer.containers.title.html('').hide();
			Viewer.containers.wrapper.removeClass('loading');
			Viewer.containers.inner.html(response);
			Viewer.containers.wrapper.show();
			
			if($('.pageinfo.data', Viewer.containers.body).length > 0){
				if(typeof JSON == "object")
				{
					Viewer.values.data = JSON.parse($('.pageinfo.data', Viewer.containers.body).html());
					Viewer.showNav(true);
				}
			}
			
			Viewer.curHeight = Viewer.containers.inner.height();
			
			//yaCounter10069633.hit(window.location.href);
		});
		
		Router.onRouteChange(function(o) {
			if(o.type == "view")
				Viewer.hide(false);
			else if(o.type == "overlay")
				Viewer.getHtml(o.url, o.title, true);
		});
	},
	
	setHtml: function(response)
	{
		var m = Viewer;
		if(!m.initialized)
			return false;
		
		m.maxWidth = Page.object.width();
		m.maxHeight = Page.object.height();
		
		$('#main').addClass("fixed");
		
		if(!m.containers)
		{	
			m.windH = $(window).height();
			m.containers = {};
			m.containers.wrapper = $('<div id="'+ m.id +'_wrapper" class="'+ m.id +'-wrapper"></div>').css("z-index", 1000).insertBefore('#main');
			m.containers.body = $('<div id="'+ m.id +'" class="scrollable '+ m.id +'"></div>').css("z-index", 2).appendTo(Viewer.containers.wrapper);
			m.containers.bar = $('<div class="'+ m.id +'-bar"></div>').appendTo(Viewer.containers.body);
			m.containers.title = $('<div class="title"></div>').appendTo(Viewer.containers.bar);
			m.containers.close = $('<div class="hide">&times;</div>').appendTo(Viewer.containers.bar).on('click', function(e) { Core.preventDefault(e); m.hide(true); }).wrap('<div class="hide-wrap" />');
			m.containers.inner = $('<div class="'+ m.id +'-inner"></div>').appendTo(m.containers.body).on('click', function(e) { if(e.target !== this) { return; }; Core.preventDefault(e); m.hide(true); });
			m.containers.inner.css({minHeight:m.windH});
			m.containers.nav = $('<div class="'+ m.id +'-nav">11</div>').appendTo(m.containers.body);
			m.showNav(true);
		}
		
		m.containers.inner.html('<div class="content"></div>');
		m.containers.wrapper.addClass('loading');
		
		m.containers.title.html('').hide();
		m.containers.wrapper.removeClass('loading');
		m.containers.inner.html(response);
		m.containers.wrapper.show();
		
		if($('.pageinfo.data', m.containers.body).length > 0){
			if(typeof JSON == "object")
			{
				m.values.data = JSON.parse($('.pageinfo.data', m.containers.body).html());
				m.showNav(true);
			}
		}
		
		m.curHeight = m.containers.inner.height();
		
		//yaCounter10069633.hit(window.location.href);
	},
	
	changeHtml: function(url, title, dir)
	{
		if(!Viewer.initialized)
			return false;
		
		Viewer.values.url = url;
		Viewer.values.title = title;
		Viewer.maxWidth = Page.object.width();
		Viewer.maxHeight = Page.object.height();
		Viewer.animInProgress = true;
		
		var animComplete = {
			complete: function()
			{
				Viewer.animInProgress = false;
				Viewer.xhr ? Viewer.xhr.abort() : false;
				
				Viewer.xhr = $.ajax({
					type: 'GET',
					url: url + (url.search(/\?/)>=0?'&ajax=true':'?ajax=true'),
					dataType: 'html'
				})
				.done(function(response) {
					Router.changeState(url, title +' - '+ Core.project);
					
					Viewer.containers.title.html('').hide();
					Viewer.containers.inner.removeClass('loading');
					Viewer.containers.inner.html(response);
					Viewer.containers.inner.removeAttr('style');
					
					if($('.pageinfo.data', Viewer.containers.body).length > 0)
						if(typeof JSON == "object")
						{
							Viewer.values.data = JSON.parse($('.pageinfo.data', Viewer.containers.body).html());
							Viewer.showNav(true);
						}
					
					Viewer.curHeight = Viewer.containers.inner.height();
					
					//yaCounter10069633.hit(window.location.href);
				});
				
				Router.onRouteChange(function(o) {
					if(o.type == "view")
						Viewer.hide(false);
					else if(o.type == "overlay")
						Viewer.getHtml(o.url, o.title, true);
				});
			}
		};
		var animDeferred = new $.Deferred();
		animDeferred.promise(animComplete);
		animComplete.done(function() {
			animComplete.complete();
		});
		
		var tmpInner = $('<div class="'+ this.id +'-inner"></div>').html('<div class="content"></div>').insertBefore(Viewer.containers.nav);
		if(dir == "next")
		{
			tmpInner.css({position:"absolute",top:parseInt(Viewer.containers.body.outerHeight()-Viewer.containers.body.height()),left:parseInt(tmpInner.outerWidth()),height:Viewer.curHeight}).on('click', function(e) { if(e.target !== this) { return; }; Core.preventDefault(e); Viewer.hide(true); });;
			tmpInner.animate({left:0}, 400);
			Viewer.containers.inner.animate({marginLeft:-(parseInt(tmpInner.outerWidth()))}, 400, function() {
				console.log('[Viewer] next done');
				tmpInner.addClass('loading');
				Viewer.containers.inner.remove();
				Viewer.containers.inner = tmpInner;
				Viewer.containers.inner.css({position:"relative",top:0});
				animDeferred.resolve();
			});
		}
		else if(dir == "prev")
		{
			tmpInner.css({position:"absolute",top:parseInt(Viewer.containers.body.outerHeight()-Viewer.containers.body.height()),left:-(parseInt(tmpInner.outerWidth())),height:Viewer.curHeight}).on('click', function(e) { if(e.target !== this) { return; }; Core.preventDefault(e); Viewer.hide(true); });;
			tmpInner.animate({left:0}, 400);
			Viewer.containers.inner.animate({marginLeft:parseInt(tmpInner.outerWidth())}, 400, function() {
				console.log('[Viewer] prev done');
				tmpInner.addClass('loading');
				Viewer.containers.inner.remove();
				Viewer.containers.inner = tmpInner;
				Viewer.containers.inner.css({position:"relative",top:0});
				animDeferred.resolve();
			});
		}
	},
	
	getBackHtml: function(url, title, node)
	{
		if(!Viewer.initialized)
			return false;
		
		Viewer.url = url;
		Viewer.title = title;
		Viewer.maxWidth = Page.object.width();
		Viewer.maxHeight = Page.object.height();
		Viewer.getPrevLink(url);
		Viewer.getPrevTitle(title);
		
		Viewer.xhr ? Viewer.xhr.abort() : false;
		
		Viewer.xhr = $.ajax({
			type: 'GET',
			url: url + (url.search(/\?/)>=0?'&ajax=true':'?ajax=true'),
			dataType: 'html'
		})
		.done(function(response) {
			if(!Viewer.containers.main)
			{
				//Viewer.containers.main = $('<section id="main"></section>').addClass("fixed").insertAfter(Viewer.containers.wrapper);
				//Viewer.containers.main_inner = $('<div class="wrapper center"></div>').prependTo(Viewer.containers.main).html(response);
				$(response).insertAfter(node);
			}
			else
			{
				Viewer.containers.main.addClass("fixed");
				$(response).insertAfter(node);
				//Viewer.containers.main_inner.html(response);
			}
			
			//yaCounter10069633.hit(window.location.href);
		});
	},
		
	hide: function(b) {
		b = b ? true : false;
		Viewer.containers.wrapper.remove();
		$('#main').removeClass("fixed");
		delete Viewer.containers;
		Viewer.onHide();
		Router.changeState(Viewer.prev_link, Viewer.prev_title, b);
	},
	
	onHide: function(callback) {
		if($.isFunction(callback))
			this.onHideCallback = callback;
		else {
			if($.isFunction(this.onHideCallback) && !$.isFunction(callback))
				this.onHideCallback(callback);
		}
		
		return this;
	},
	
	onInit: function(callback) {
		if($.isFunction(callback))
			this.onInitCallback = callback;
		else {
			if($.isFunction(this.onInitCallback) && !$.isFunction(callback))
				this.onInitCallback(callback);
		}
		
		return this;
	},
	
	showNav: function(bindEvents) {
		if(!Viewer.values.data)
			if($('.pageinfo.data', Viewer.containers.body).length > 0)
				if(typeof JSON == "object")
					Viewer.values.data = JSON.parse($('.pageinfo.data', Viewer.containers.body).html());
		
		if($('*[viewer-action="prev"]', Viewer.containers.nav).length > 0)
			Viewer.containers.prev = $('*[viewer-action="prev"]', Viewer.containers.nav);
		else
			Viewer.containers.prev = $('<a/>').addClass("left-arrow").attr('rel',"prev").attr('viewer-action',"prev").appendTo(Viewer.containers.nav);
		
		if(!Viewer.values.data || !Viewer.values.data.prev)
		{
			Viewer.containers.prev.remove();
			return false;
		}
		else
			Viewer.containers.prev.attr('href',Viewer.values.data.prev).attr('data-title',Viewer.values.data.prev_topic);
		
		Viewer.containers.prev.css({top:$(window).height()/2, left:0});
		if(bindEvents)
		{
			Viewer.containers.prev.off('click');
			Viewer.containers.prev.on('click', function(e) {
				e.stopPropagation();
				e.preventDefault();
				
				if(!Viewer.animInProgress)
					Viewer.changeHtml($(this).attr('href'), $(this).attr('data-title'), "prev");
			});
		}
		
		if($('*[viewer-action="next"]', Viewer.containers.nav).length > 0)
			Viewer.containers.next = $('*[viewer-action="next"]', Viewer.containers.nav);
		else
			Viewer.containers.next = $('<a/>').addClass("right-arrow").attr('rel',"next").attr('viewer-action',"next").appendTo(Viewer.containers.nav);
		
		if(!Viewer.values.data || !Viewer.values.data.next)
		{
			Viewer.containers.next.remove();
			return false;
		}
		else
			Viewer.containers.next.attr('href',Viewer.values.data.next).attr('data-title',Viewer.values.data.next_topic);
		
		Viewer.containers.next.css({top:$(window).height()/2, right:0});
		if(bindEvents)
		{
			Viewer.containers.next.off('click');
			Viewer.containers.next.on('click', function(e) {
				e.stopPropagation();
				e.preventDefault();
				
				if(!Viewer.animInProgress)
					Viewer.changeHtml($(this).attr('href'), $(this).attr('data-title'), "next");
			});
		}

	}
};

$(function() {
	Viewer.initialize({comments:false, topOffset:0, staticHeight:null, staticWidth:980});
});


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