Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Initialize namespaces.
- if (typeof Deckbox == "undefined") Deckbox = {};
- Deckbox.ui = Deckbox.ui || {};
- /**
- * Main tooltip type. Will be initialized for both the image tooltips and for the wow tooltips, or simple text tooltips.
- */
- Deckbox.ui.Tooltip = function(className, type) {
- this.el = document['c' + 'reateElement']('div');
- this.el.className = className + ' ' + type;
- this.type = type;
- this.el.style.display = 'none';
- document.body['a' + 'ppendChild'](this.el);
- this.tooltips = {};
- };
- Deckbox.ui.Tooltip['p' + 'rototype'] = {
- _padContent: function(content) {
- return "<table><tr><td>" + content + "</td><th style='background-position: right top;'></th></tr><tr>" +
- "<th style='background-position: left bottom;'/><th style='background-position: right bottom;'/></tr></table>";
- },
- showWow: function(posX, posY, content, url, el) {
- /* IE does NOT escape quotes apparently. */
- url = url['r' + 'eplace'](/"/g, "%22");
- /* Problematic with routes on server. */
- url = url['r' + 'eplace'](/\?/g, "");
- if (this.tooltips[url] && this.tooltips[url].content) {
- content = this._padContent(this.tooltips[url].content);
- } else {
- content = this._padContent('Loading...');
- this.tooltips[url] = this.tooltips[url] || {el: el};
- Deckbox._.loadJS(url);
- /* Remeber these for when (if) the register call wants to show the tooltip. */
- this.posX = posX; this.posY = posY;
- }
- this.el.style.width = '';
- this.el['i' + 'nnerHTML'] = content;
- this.el.style.display = '';
- this.el.style.width = (20 + Math.min(330, this.el['c' + 'hildNodes'][0].offsetWidth)) + 'px';
- this.move(posX, posY);
- },
- showText: function(posX, posY, text) {
- this.el['i' + 'nnerHTML'] = text;
- this.el.style.display = '';
- this.move(posX, posY);
- },
- showImage: function(posX, posY, image) {
- if (image.complete) {
- this.el['i' + 'nnerHTML'] = '';
- this.el['a' + 'ppendChild'](image);
- } else {
- this.el['i' + 'nnerHTML'] = 'Loading...';
- image.onload = function() {
- var self = Deckbox._.tooltip('image');
- self.el['i' + 'nnerHTML'] = '';
- image.onload = null;
- self.el['a' + 'ppendChild'](image);
- self.move(posX, posY);
- }
- }
- this.el.style.display = '';
- this.move(posX, posY);
- },
- hide: function() {
- this.el.style.display = 'none';
- },
- move: function(posX, posY) {
- // The tooltip should be offset to the right so that it's not exactly next to the mouse.
- posX += 15;
- posY -= this.el.offsetHeight / 3;
- // Remeber these for when (if) the register call wants to show the tooltip.
- this.posX = posX;
- this.posY = posY;
- if (this.el.style.display == 'none') return;
- var pos = Deckbox._.fitToScreen(posX, posY, this.el);
- this.el.style.top = pos[1] + "px";
- this.el.style.left = pos[0] + "px";
- },
- register: function(url, content) {
- this.tooltips[url].content = content;
- if (this.tooltips[url].el._shown) {
- this.el.style.width = '';
- this.el['i' + 'nnerHTML'] = this._padContent(content);
- this.el.style.width = (20 + Math.min(330, this.el['c' + 'hildNodes'][0].offsetWidth)) + 'px';
- this.move(this.posX, this.posY);
- }
- }
- };
- Deckbox.ui.Tooltip.hide = function() {
- Deckbox._.tooltip('image').hide();
- Deckbox._.tooltip('wow').hide();
- Deckbox._.tooltip('text').hide();
- };
- Deckbox._ = {
- onDocumentLoad: function(callback) {
- if (window.addEventListener) {
- window.addEventListener("load", callback, false);
- } else {
- window.attachEvent && window.attachEvent("onload", callback);
- }
- },
- preloadImg: function(link) {
- var img = document['c' + 'reateElement']('img');
- img.style.display = "none"
- img.style.width = "1px"
- img.style.height = "1px"
- img.src = link.href + '/tooltip';
- return img;
- },
- pointerX: function(event) {
- var docElement = document.documentElement,
- body = document.body || { scrollLeft: 0 };
- return event.pageX ||
- (event.clientX +
- (docElement.scrollLeft || body.scrollLeft) -
- (docElement.clientLeft || 0));
- },
- pointerY: function(event) {
- var docElement = document.documentElement,
- body = document.body || { scrollTop: 0 };
- return event.pageY ||
- (event.clientY +
- (docElement.scrollTop || body.scrollTop) -
- (docElement.clientTop || 0));
- },
- scrollOffsets: function() {
- return [
- window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
- window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
- ];
- },
- viewportSize: function() {
- var ua = navigator.userAgent, rootElement;
- if (ua.indexOf('AppleWebKit/') > -1 && !document.evaluate) {
- rootElement = document;
- } else if (Object['p' + 'rototype']['t' + 'oString']['c' + 'all'](window.opera) == '[object Opera]' && window.parseFloat(window.opera.version()) < 9.5) {
- rootElement = document.body;
- } else {
- rootElement = document.documentElement;
- }
- /* IE8 in quirks mode returns 0 for these sizes. */
- var size = [rootElement['clientWidth'], rootElement['clientHeight']];
- if (size[1] == 0) {
- return [document.body['clientWidth'], document.body['clientHeight']];
- } else {
- return size;
- }
- },
- fitToScreen: function(posX, posY, el) {
- var scroll = Deckbox._.scrollOffsets(), viewport = Deckbox._.viewportSize();
- /* decide if wee need to switch sides for the tooltip */
- /* too big for X */
- if ((el.offsetWidth + posX) >= (viewport[0] - 15) ) {
- posX = posX - el.offsetWidth - 20;
- }
- /* If it's too high, we move it down. */
- if (posY - scroll[1] < 0) {
- posY += scroll[1] - posY + 5;
- }
- /* If it's too low, we move it up. */
- if (posY + el.offsetHeight - scroll[1] > viewport[1]) {
- posY -= posY + el.offsetHeight + 5 - scroll[1] - viewport[1];
- }
- return [posX, posY];
- },
- addEvent: function(obj, type, fn) {
- if (obj.addEventListener) {
- if (type == 'mousewheel') obj.addEventListener('DOMMouseScroll', fn, false);
- obj.addEventListener( type, fn, false );
- } else if (obj.attachEvent) {
- obj["e"+type+fn] = fn;
- obj[type+fn] = function() { obj["e"+type+fn]( window.event ); };
- obj.attachEvent( "on"+type, obj[type+fn] );
- }
- },
- removeEvent: function(obj, type, fn) {
- if (obj.removeEventListener) {
- if(type == 'mousewheel') obj.removeEventListener('DOMMouseScroll', fn, false);
- obj.removeEventListener( type, fn, false );
- } else if (obj.detachEvent) {
- obj.detachEvent( "on"+type, obj[type+fn] );
- obj[type+fn] = null;
- obj["e"+type+fn] = null;
- }
- },
- loadJS: function(url) {
- var s = document['c' + 'reateElement']('s' + 'cript');
- s['s' + 'etAttribute']("type", "text/javascript");
- s['s' + 'etAttribute']("src", url);
- document['g' + 'etElementsByTagName']("head")[0]['a' + 'ppendChild'](s);
- },
- loadCSS: function(url) {
- var s = document['c' + 'reateElement']("link");
- s.type = "text/css";
- s.rel = "stylesheet";
- s.href = url;
- document['g' + 'etElementsByTagName']("head")[0]['a' + 'ppendChild'](s);
- },
- needsTooltip: function(el) {
- if (el.getAttribute('data-tt')) return true;
- var href;
- if (!el || !(el.tagName == 'A') || !(href = el.getAttribute('href'))) return false;
- if (el.className.match('no_tooltip')) return false;
- return href.match(/^https?:\/\/[^\/]*\/(mtg|wow|whi)\/.+/);
- },
- tooltip: function(which) {
- if (which == 'image') return this._iT = this._iT || new Deckbox.ui.Tooltip('deckbox_i_tooltip', 'image');
- if (which == 'wow') return this._wT = this._wT || new Deckbox.ui.Tooltip('deckbox_tooltip', 'wow');
- if (which == 'text') return this._tT = this._tT || new Deckbox.ui.Tooltip('deckbox_t_tooltip', 'text');
- },
- target: function(event) {
- var target = event.target || event.srcElement || document;
- /* check if target is a textnode (safari) */
- if (target.nodeType == 3) target = target['p' + 'arentNode'];
- return target;
- }
- };
- /**
- * Bind the listeners.
- */
- (function() {
- function onmouseover(event) {
- var el = Deckbox._.target(event);
- if (Deckbox._.needsTooltip(el)) {
- var no = el.getAttribute('data-nott'), url, img,
- posX = Deckbox._.pointerX(event), posY = Deckbox._.pointerY(event);
- if (!no) {
- el._shown = true;
- if (url = el.getAttribute('data-tt')) {
- showImage(el, url, posX, posY);
- } else if (el.href.match('/(mtg|whi)/')) {
- showImage(el, el.href + '/tooltip', posX, posY);
- } else {
- Deckbox._.tooltip('wow').showWow(posX, posY, null, el.href + '/tooltip', el);
- }
- }
- }
- }
- function showImage(el, url, posX, posY) {
- var img = document['c' + 'reateElement']('img');
- url = url['r' + 'eplace'](/\?/g, ""); /* Problematic with routes on server. */
- img.src = url;
- setTimeout(function() {
- if (el._shown) Deckbox._.tooltip('image').showImage(posX, posY, img);
- }, 200);
- }
- function onmousemove(event) {
- var el = Deckbox._.target(event), posX = Deckbox._.pointerX(event), posY = Deckbox._.pointerY(event);
- if (Deckbox._.needsTooltip(el)) {
- Deckbox._.tooltip('image').move(posX, posY);
- Deckbox._.tooltip('wow').move(posX, posY, el.href);
- }
- }
- function onmouseout(event) {
- var el = Deckbox._.target(event);
- if (Deckbox._.needsTooltip(el)) {
- el._shown = false;
- Deckbox._.tooltip('image').hide();
- Deckbox._.tooltip('wow').hide();
- }
- }
- function click(event) {
- Deckbox._.tooltip('image').hide();
- Deckbox._.tooltip('wow').hide();
- }
- Deckbox._.addEvent(document, 'mouseover', onmouseover);
- Deckbox._.addEvent(document, 'mousemove', onmousemove);
- Deckbox._.addEvent(document, 'mouseout', onmouseout);
- Deckbox._.addEvent(document, 'click', click);
- var protocol = (document.location.protocol == 'https:') ? 'https:' : 'http:';
- Deckbox._.loadCSS(protocol + '//deckbox.org/assets/external/deckbox_tooltip.css');
- /* IE needs more shit */
- if (!!window.attachEvent && !(Object['p' + 'rototype']['t' + 'oString']['c' + 'all'](window.opera) == '[object Opera]')) {
- Deckbox._.loadCSS(protocol + '//deckbox.org/assets/external/deckbox_tooltip_ie.css');
- }
- /* Preload the tooltip images. */
- Deckbox._.onDocumentLoad(function() {
- return;
- var allLinks = document['g' + 'etElementsByTagName']('a');
- for (var i = 0; i < allLinks.length; i ++) {
- var link = allLinks[i];
- if (Deckbox._.needsTooltip(link)) {
- document.body['a' + 'ppendChild'](Deckbox._.preloadImg(link));
- }
- }
- });
- })();
- /**
- * Initialize namespaces.
- */
- if (typeof Deckbox == "undefined") Deckbox = {};
- Deckbox.utils = Deckbox.utils || {};
- Deckbox.utils.DeckParser = {
- game: 'mtg',
- initialize: function(game, postElement, postClass, generalTag) {
- this.game = game;
- var posts = document['g' + 'etElementsByTagName'](postElement);
- for (var i = 0; i < posts.length; i++ ) {
- if (posts[i].className && (posts[i].className.indexOf(postClass) > -1)) this.parsePost(posts[i], generalTag);
- }
- },
- initializeMyfreeforum: function(game) {
- this.initialize(game, 'span', 'postbody');
- },
- initializePhpbb: function(game) {
- this.initialize(game, 'div', 'postbody');
- },
- initializeInvisionfree: function(game) {
- this.initialize(game, 'div', 'postcolor');
- },
- initializeZetaboards: function(game) {
- this.initialize(game, 'td', 'c_post');
- },
- initializeWeebly: function() {
- this.initialize('mtg', 'div', 'paragraph');
- },
- initializeMybb: function() {
- this.initialize('mtg', 'div', 'post_body', 'mtg');
- },
- parsePost: function(element, generalTag) {
- var self = this;
- if (generalTag) {
- this.parseTag(element, generalTag, function(text) { return self.parseDeck(text); });
- } else {
- this.parseTag(element, 'deck', function(text) { return self.parseDeck(text); });
- this.parseTag(element, 'card', function(text) { return self.linkify(text); });
- }
- },
- parseTag: function(element, tag, callback) {
- var html = element['i' + 'nnerHTML'], begin, end, result = "";
- // Work from the end so that a hanging beginning tag screws up less.
- while(-1 != (begin = html.toLowerCase().lastIndexOf('[' + tag + ']'))) {
- if (-1 == (end = html.toLowerCase().indexOf('[/' + tag + ']', begin))) break;
- /* We add 2 / 3 to tag length to account for '[', and '/]'. */
- var text = html['s' + 'ubstring'](begin + tag.length + 2, end), replacement = callback(text);
- result = replacement + html['s' + 'ubstring'](end + tag.length + 3) + result;
- html = html['s' + 'ubstring'](0, begin);
- }
- element['i' + 'nnerHTML'] = html + result;
- },
- parseDeck: function(html) {
- var lines = html['s' + 'plit'](/<[bB][rR] ?\/?>/), result = '';
- for (var i = 0; i < lines.length; i++) {
- var line = lines[i];
- line = line['r' + 'eplace'](/^\s*/, "")['r' + 'eplace'](/( )*/, "")['r' + 'eplace'](/\s*$/, "");
- if (line.toLowerCase().indexOf("sideboard") == 0 || line.toLowerCase().indexOf("maindeck") == 0 || line.match( /^\s*(\/\/.*)?\s*$/ )) continue;
- var count;
- // ignore extra info for lists from apprentice
- line = line['r' + 'eplace'](/^SB: /, "")['r' + 'eplace'](/\[.*?\]/, "" );
- if (/^\d+x?\s+/.test(line)) {
- var name = line['r' + 'eplace'](/^\d+\s*(x\s+)?/, "");
- count = parseInt(line['s' + 'ubstr'](0, line.length - name.length));
- line = name;
- }
- lines[i] = (count ? count + ' ' : '') + this.linkify(line);
- }
- return lines.join('<br/>');
- },
- linkify: function(name) {
- return '<a href="https://deckbox.org/' + this.game + '/' + name + '">' + name + '</a>';
- }
- }
Add Comment
Please, Sign In to add comment