Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #247 from IDotD/idrinth
Browse files Browse the repository at this point in the history
Idrinth
  • Loading branch information
w20k authored Jan 11, 2017
2 parents 5aff145 + e7d8702 commit e28b664
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 94 deletions.
1 change: 0 additions & 1 deletion src/mods/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,6 @@ idrinth.chat = {
return;
}
idrinth.chat.updateTimeout = window.setTimeout ( idrinth.chat.refreshChats, 1500 );
idrinth.chat.refreshMembers ();
},
startLoginCallback: function ( data ) {
if ( !data ) {
Expand Down
221 changes: 157 additions & 64 deletions src/mods/names.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,138 @@
idrinth.names = {
load: function ( added ) {
'use strict';
idrinth.core.ajax.runHome (
'users-service/' + added,
function ( text ) {
idrinth.names.import ( text );
}
);
},
import: function ( data ) {
/**
* @type {object}
*/
users: { },
/**
* @type {object}
*/
classes: { },
/**
* @type {object}
*/
guilds: { },
/**
* a timeout
* @type {object}
*/
ownTimeout: null,
/**
* @type {Number}
*/
counter: 0,
/**
*
* @param {HTMLElement} element
* @returns {string}
*/
parse: function ( element ) {
'use strict';
data = JSON.parse ( data );
if ( !data ) {
return;
}
if ( data.users ) {
idrinth.names.users = data.users;
}
if ( data.guilds ) {
idrinth.names.guilds = data.guilds;
if ( element.getAttribute ( 'dotdxname' ) ) {
return ( element.getAttribute ( 'dotdxname' ) );
}
if ( data.classes ) {
idrinth.names.classes = data.classes;
if ( element.getAttribute ( 'username' ) ) {
return ( element.getAttribute ( 'username' ) );
}
return ( element.innerHTML ).replace ( /(<([^>]+)>)/ig, "" );
},
/**
* the method being run to handle data im- and export
* @returns {undefined}
*/
run: function ( ) {
'use strict';
/**
*
* @param {string} added the path-segment defining the data returned
* @returns {undefined}
*/
var load = function ( added ) {
/**
*
* @param {string} data
* @returns {undefined}
*/
var importNames = function ( data ) {
data = JSON.parse ( data );
if ( !data ) {
return;
}
if ( data.users ) {
idrinth.names.users = data.users;
}
if ( data.guilds ) {
idrinth.names.guilds = data.guilds;
}
if ( data.classes ) {
idrinth.names.classes = data.classes;
}
};
idrinth.core.ajax.runHome (
'users-service/' + added,
importNames
);
};
/**
* adds names from elements on the page
* @returns {undefined}
*/
var add = function ( ) {
/**
*
* @param {HTMLElement} element
* @returns {undefined}
*/
var processName = function ( element ) {
var name = '';
try {
name = idrinth.names.parse ( element );
} catch ( e ) {
return;
}
if ( !name ) {
return;
}
if ( !element.getAttribute ( 'data-idrinth-parsed' ) && idrinth.ui.childOf ( element, 'chat_message_window' ) ) {
element.setAttribute ( 'data-idrinth-parsed', '1' );
}
if ( !idrinth.names.users[name.toLowerCase ( )] && name.length > 0 ) {
idrinth.names.users[name.toLowerCase ()] = { };
idrinth.core.ajax.runHome ( 'users-service/add/' + encodeURIComponent ( name ) + '/' );
}
};
var el = document.getElementsByClassName ( 'username' );
for (var count = el.length - 1; count >= 0; count--) {
processName ( el[count] );
}
};
try {
if ( idrinth.names.counter % 300 === 0 || Object.keys ( idrinth.names.users ).length === 0 ) {
idrinth.names.load ( Object.keys ( idrinth.names.classes ).length === 0 ? 'init/' : 'get/' );
load ( Object.keys ( idrinth.names.classes ).length === 0 ? 'init/' : 'get/' );
} else if ( Object.keys ( idrinth.names.users ).length > 0 ) {
idrinth.names.add ( );
add ( );
}
} catch ( e ) {
idrinth.core.log ( e );
}
idrinth.names.counter = idrinth.names.counter + 1;
idrinth.names.ownTimeout = window.setTimeout ( idrinth.names.run, 6666 );
},
users: { },
classes: { },
guilds: { },
ownTimeout: null,
add: function ( ) {
var processName = function ( element ) {
var name = '';
try {
name = idrinth.names.parse ( element );
} catch ( e ) {
return;
}
if ( !name ) {
return;
}
if ( element.getAttribute ( 'onmouseover' ) !== 'idrinth.ui.showTooltip()(this);' && idrinth.ui.childOf ( element, 'chat_message_window' ) ) {
element.setAttribute ( 'onmouseover', 'idrinth.ui.showTooltip(this);' );
}
if ( !idrinth.names.users[name.toLowerCase ( )] && name.length > 0 ) {
idrinth.names.users[name.toLowerCase ()] = { };
idrinth.core.ajax.runHome ( 'users-service/add/' + encodeURIComponent ( name ) + '/' );
}
};
var el = document.getElementsByClassName ( 'username' );
for (var count = el.length - 1; count >= 0; count--) {
processName ( el[count] );
}
},
parse: function ( element ) {
'use strict';
if ( element.getAttribute ( 'dotdxname' ) ) {
return ( element.getAttribute ( 'dotdxname' ) );
}
if ( element.getAttribute ( 'username' ) ) {
return ( element.getAttribute ( 'username' ) );
}
return ( element.innerHTML ).replace ( /(<([^>]+)>)/ig, "" );
},
/**
* initialises the module
* @returns {undefined}
*/
start: function ( ) {
'use strict';
/**
* creates the tooltip-element
* @returns {undefined}
*/
var build = function ( ) {
'use strict';
/**
*
* @param {string} name
* @returns {Array}
*/
function getServerPart ( name ) {
return [ {
css: 'idrinth-line idrinth-tooltip-header',
Expand Down Expand Up @@ -172,10 +225,50 @@ idrinth.names = {
} );
idrinth.ui.body.appendChild ( idrinth.ui.tooltip );
};
/**
* shows the tooltip if the element has a known name
* @param {HTMLElement} element
* @returns {undefined}
*/
var showTooltip = function ( element ) {
/**
*
* @param {object} set
* @param {HTMLElement} element
* @param {Boolean} world
* @returns {undefined}
*/
function tooltip ( set, element, world ) {
if ( !set ) {
idrinth.ui.updateClassesList ( element, [ 'idrinth-hide' ], [ ] );
return;
}
var baseUrl = 'https://dotd.idrinth.de/' + ( world ? 'world-kongregate' : 'kongregate' );
idrinth.ui.updateClassesList ( idrinth.ui.tooltip, [ ], [ 'idrinth-hide' ] );
idrinth.ui.updateClassesList ( element, [ ], [ 'idrinth-hide' ] );
element.childNodes[0].setAttribute ( 'href', baseUrl + '/summoner/' + set.id + '/' );
element.childNodes[0].innerHTML = set.name;
element.childNodes[1].childNodes[1].innerHTML = set.level + ' (' + set['7day'] + '/week, ' + set['30day'] + '/month)';
element.childNodes[1].childNodes[3].innerHTML = idrinth.names.classes[set.class];
element.childNodes[2].childNodes[1].setAttribute ( 'href', baseUrl + '/guild/' + set.guildId + '/' );
element.childNodes[2].childNodes[1].innerHTML = idrinth.names.guilds[world ? 'world' : 'kongregate'][set.guildId];
element.childNodes[3].childNodes[1].innerHTML = set.updated;
element.childNodes[3].setAttribute ( 'style', ( new Date () ) - ( new Date ( set.updated ) ) > 86400000 ? 'color:#aa0000;' : '' );
}
idrinth.names.isHovering = false;
var name = idrinth.names.parse ( element ).toLowerCase ( );
if ( idrinth.settings.get ( "names" ) && idrinth.ui.tooltip && idrinth.names.users[name] ) {
window.clearTimeout ( idrinth.ui.tooltipTO );
idrinth.ui.tooltip.setAttribute ( 'style', idrinth.ui.getElementPositioning ( element, -200, -100 ) );
tooltip ( idrinth.names.users[name].kongregate, idrinth.ui.tooltip.firstChild, false );
tooltip ( idrinth.names.users[name].world, idrinth.ui.tooltip.lastChild, true );
idrinth.ui.setTooltipTimeout ();
}
};
if ( idrinth.platform === 'kongregate' ) {
idrinth.core.multibind.add ( 'mouseover', '.chat_message_window .username', showTooltip );
idrinth.names.ownTimeout = window.setTimeout ( idrinth.names.run, 10000 );
build ();
}
},
counter: 0
}
};
29 changes: 0 additions & 29 deletions src/mods/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,35 +256,6 @@ idrinth.ui = {
}
idrinth.ui.body.appendChild ( idrinth.ui.buildElement ( mod ) );
},
showTooltip: function ( element ) {
'use strict';
function tooltip ( set, element, world ) {
if ( !set ) {
idrinth.ui.updateClassesList ( element, [ 'idrinth-hide' ], [ ] );
return;
}
var baseUrl = 'https://dotd.idrinth.de/' + ( world ? 'world-kongregate' : 'kongregate' );
idrinth.ui.updateClassesList ( idrinth.ui.tooltip, [ ], [ 'idrinth-hide' ] );
idrinth.ui.updateClassesList ( element, [ ], [ 'idrinth-hide' ] );
element.childNodes[0].setAttribute ( 'href', baseUrl + '/summoner/' + set.id + '/' );
element.childNodes[0].innerHTML = set.name;
element.childNodes[1].childNodes[1].innerHTML = set.level + ' (' + set['7day'] + '/week, ' + set['30day'] + '/month)';
element.childNodes[1].childNodes[3].innerHTML = idrinth.names.classes[set.class];
element.childNodes[2].childNodes[1].setAttribute ( 'href', baseUrl + '/guild/' + set.guildId + '/' );
element.childNodes[2].childNodes[1].innerHTML = idrinth.names.guilds[world ? 'world' : 'kongregate'][set.guildId];
element.childNodes[3].childNodes[1].innerHTML = set.updated;
element.childNodes[3].setAttribute ( 'style', ( new Date () ) - ( new Date ( set.updated ) ) > 86400000 ? 'color:#aa0000;' : '' );
}
idrinth.names.isHovering = false;
var name = idrinth.names.parse ( element ).toLowerCase ( );
if ( idrinth.settings.get ( "names" ) && idrinth.ui.tooltip && idrinth.names.users[name] ) {
window.clearTimeout ( idrinth.ui.tooltipTO );
idrinth.ui.tooltip.setAttribute ( 'style', idrinth.ui.getElementPositioning ( element, -200, -100 ) );
tooltip ( idrinth.names.users[name].kongregate, idrinth.ui.tooltip.firstChild, false );
tooltip ( idrinth.names.users[name].world, idrinth.ui.tooltip.lastChild, true );
idrinth.ui.setTooltipTimeout ();
}
},
matchesCss: function ( element, selector ) {
while ( element && element !== document ) {
if ( typeof element.matches === 'function' && element.matches ( selector ) ) {
Expand Down

0 comments on commit e28b664

Please sign in to comment.