Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // y88
- // tooltip add-on for forum.mista.ru
- // v1.33 (fix call funk)
- var msg_hash = new Array();
- var timeoutNewMessage;
- var last_n_old = 0;
- if (typeof last_n=="undefined") { // for archived
- last_n = 1000
- document.write("<scr"+"ipt type='text/javascript' language='javascript' src='/css/Subsys_JsHttpRequest.js'></sc"+"ript>");
- }
- for (var i=0; i<document.links.length; i++) attachLinkLivePreview(document.links[i]);
- var topic_id = 0;
- if (document.getElementById('topic_id')) {topic_id = '' + document.getElementById('topic_id').value}
- if (topic_id) {
- createToolTips();
- }
- // Drag ============
- drag_drop = {
- drag_obj: null,
- offsetx: 0,
- offsety: 0,
- initializeDrag: function(e){
- var evt = window.event || e;
- var el = evt.target || evt.srcElement;
- while (el) {
- if (el.id == "dragbar") break;
- el = el.parentNode;
- }
- if (el) {
- drag_drop.drag_obj = el.parentNode;
- if (drag_drop.drag_obj) {
- drag_drop.offsetx = parseInt(drag_drop.drag_obj.style.left) - evt.clientX;
- drag_drop.offsety = parseInt(drag_drop.drag_obj.style.top) - evt.clientY;
- }
- }else{
- drag_drop.drag_obj = null;
- }
- },
- end:0
- }
- addEvent(document, 'onmousedown', drag_drop.initializeDrag);
- addEvent(document, 'onmouseup', function() {drag_drop.drag_obj = null});
- addEvent(document, 'onmousemove', function(e) {
- if (drag_drop.drag_obj) {
- var evt = window.event || e;
- drag_drop.drag_obj.style.left = evt.clientX + drag_drop.offsetx + "px"
- drag_drop.drag_obj.style.top = evt.clientY + drag_drop.offsety + "px"
- }
- //return false; // not allow select text in IE
- })
- // ===========
- function attachLinkForEveryChild(obj) {
- if (obj.children) {
- for (var i=0; i<obj.children.length; i++) {
- var new_obj = obj.children[i];
- if ( new_obj.tagName == "A" ) {
- attachLinkLivePreview(new_obj);
- }
- attachLinkForEveryChild(new_obj);
- }
- }
- }
- function attachLinkLivePreview(elt) {
- // First detect if this is a topic reference.
- // <a href="topic.php?id=445933" class="agb">
- var p = elt.href.match(/topic\.php\?id=(\d+)$/i);
- if (!p) return;
- elt.href = elt.href + "&"; // change link
- // Create [?] element. Save to elt.
- with (document) {
- var span = createElement('span');
- span.innerHTML = ' <span style="color:#CCC; cursor:pointer; cursor:hand" '
- +'onmouseover="tooltip.scheduleShowTip(this,re_html,0,'+p[1]+')" '
- +'onmouseout="tooltip.scheduleHideTip(this)">[¿]</span>';
- elt.parentNode.insertBefore(span, elt);
- elt.parentNode.insertBefore(elt, span);
- elt = span;
- }
- }
- function createToolTips() {
- var t_id = topic_id;
- var table_messages = document.getElementById('table_messages');
- if (table_messages) {
- if (last_n_old!=last_n) { // exist new messages
- if (!msg_hash[t_id]) { msg_hash[t_id] = new Array() }
- // read all data and store to array
- for (msg_id=last_n_old; msg_id<=last_n; msg_id++) {
- // if (document.getElementById('message_'+msg_id)) {} // row для ajax нет!!!!
- if (document.getElementById('tdmsg'+msg_id)) { // cell
- var obj_tr_msg = document.getElementById('tdmsg'+msg_id).parentNode; // row
- // var msg_author = document.getElementById('tduser'+msg_id).innerHTML;
- var msg_author = obj_tr_msg.cells[0].innerHTML;
- var msg_date = '';
- var msg_text = document.getElementById('tdmsg'+msg_id).innerHTML;
- // fix IE bug width="100%"
- msg_text = msg_text.replace(/width="100\%"/gi, 'width="99%"')
- msg_hash[t_id][msg_id] = [msg_id,msg_author,msg_date,msg_text];
- }
- }
- // create tooltip
- for (msg_id=last_n_old; msg_id<=last_n; msg_id++) {
- var obj_td_msg = document.getElementById('tdmsg'+msg_id);
- if (obj_td_msg) { // cell
- var obj_tr_msg = obj_td_msg.parentNode; // row
- var msg_text = obj_td_msg.innerHTML;
- // 1. (<a href='#0' style='text-decoration:none'>0</a>)
- // (<a href='topic.php?id=444509&page=3#261' style='text-decoration:none'>261</a>)
- msg_text = msg_text.replace(/\(<a [^>]*>(\d+)<\/a>\)/gi, '<span onmouseover="tooltip.scheduleShowTip(this,re_html,$1)" onmouseout="tooltip.scheduleHideTip(this)"><a href=#$1 style="text-decoration:none">($1)</a></span>')
- // 2. <a rel="nofollow" href="#2" class="answer-link interlink" title="BuHu" rev="#218">218</a>
- // fix <a .*?> by hd1
- msg_text = msg_text.replace(/<a [^>]*>(\d+)<\/a>/gi, '<span onmouseover="tooltip.scheduleShowTip(this,re_html,$1)" onmouseout="tooltip.scheduleHideTip(this)"><a href=#$1 class="answer-link">($1)</a></span>')
- // 3. добавляем к URL картинок [?] +tooltip --------------------------
- // <a href="http://*.jpg" target="_blank" >http://*.jpg</a>
- msg_text = msg_text.replace(/(<a [^>]*href="(http:[^ ]*\.(jpg|gif|png))".*?<\/a>)/ig, '$1 <span style="COLOR: #ccc; CURSOR: hand" onmouseover="tooltip.scheduleShowTip(this,img_view,\'$2\')" onmouseout="tooltip.scheduleHideTip(this)">[¿]</span>')
- //obj_td_msg.innerHTML = msg_text; // FF only !!!
- //<td style="padding:10px;" id="tdmsg5" valign="top" bgcolor="#FDFDFD" class="leftbottomgray">
- obj_tr_msg.deleteCell(1);
- var td_m = obj_tr_msg.insertCell(1);
- td_m.id="tdmsg"+msg_id;
- td_m.vAlign="top";
- td_m.align="left";
- //td_m.padding="10px";
- td_m.bgColor="#FDFDFD";
- td_m.style.borderBottom="1px solid #EEE";
- td_m.style.borderLeft="1px solid #EEE";
- td_m.innerHTML = msg_text;
- attachLinkForEveryChild(td_m); // + add [?]
- }
- }
- last_n_old = last_n;
- }
- }
- // add shedule for new messages
- if (timeoutNewMessage) timeoutNewMessage=clearTimeout(timeoutNewMessage);
- timeoutNewMessage = setTimeout(createToolTips, 3000);
- }
- function nav_html(t_id) {
- // create navigations for future features
- return '<span style="cursor:pointer; color:gray" onmouseover="this.style.color=\'red\'" onmouseout="this.style.color=\'gray\'" onclick="getmessage(,'+t_id+',0,-2)">[← </span> <span style="cursor:pointer; color:gray" onmouseover="this.style.color=\'red\'" onmouseout="this.style.color=\'gray\'" onclick="getmessage(,'+t_id+',0,-1)"> « </span> <span style="cursor:pointer; color:gray" onmouseover="this.style.color=\'red\'" onmouseout="this.style.color=\'gray\'" onclick="getmessage(,'+t_id+',0,+1)"> » </span> <span style="cursor:pointer; color:gray" onmouseover="this.style.color=\'red\'" onmouseout="this.style.color=\'gray\'" onclick="getmessage(,'+t_id+',0,2)"> →]</span>';
- }
- function re_html(id,t_id) {
- var data_exist = 0;
- if (!t_id) {t_id = topic_id}
- if (msg_hash[t_id]) {
- if (msg_hash[t_id][id]) {
- data_exist = 1;
- return {
- subj: " "+msg_hash[t_id][id][1],
- text: ""+msg_hash[t_id][id][3]+""
- }
- }
- }
- if (!data_exist) {
- //ajax load
- getmessage_tooltip(id,t_id);
- return {subj:'wait ajax '+id,text:'loading...'}
- }
- }
- function img_view(img_url) {
- if (!img_url) return {subj:'error',text:'no image...'}
- return {
- subj: " <b>"+img_url+"</b>",
- text: "<img src='"+img_url+"'>"
- }
- }
- //функция для подгрузки сообщения
- function getmessage_tooltip(message_n, t_id) {
- var k = 1;
- if (!t_id) {t_id = topic_id}
- var direction = 0;
- var req = new Subsys_JsHttpRequest_Js();
- // Код, АВТОМАТИЧЕСКИ вызываемый при окончании загрузки.
- req.onreadystatechange = function() {
- if (req.readyState == 4) {
- if (req.responseJS){
- var response = req.responseJS.client_action;
- if ((response==1) || (response==2)) {
- var author = 'ajax:: ERROR';
- var message = req.responseJS.message_html;
- if (response==1) { // ok
- message = message.replace(/<span.*\]<\/span>/gi, ''); // delete top navigations
- //<BR><B>user_name</B>:message text
- author = '';
- message = message.replace(/\r/g, ''); // fix FF
- if (message.match(/<BR><B>(.+)<\/B>:(.+)/i)) {
- author = "<b>"+RegExp.$1+"</b>";
- message = RegExp.$2;
- }
- }
- if (!msg_hash[t_id]) { msg_hash[t_id] = new Array() }
- msg_hash[t_id][message_n] = [message_n, author, '', message];
- // not work time of time ???
- document.getElementById('dragbar').innerHTML = msg_hash[t_id][message_n][1] + tooltip.html_close;
- document.getElementById('databar').innerHTML = msg_hash[t_id][message_n][3];
- // var dragbar = document.getElementById('dragbar');
- // if (dragbar) {
- // dragbar.removeChild(dragbar.firstChild);
- // var span = document.createElement("span");span.innerHTML = msg_hash[t_id][message_n][1] + tooltip.html_close;
- // dragbar.appendChild(span);
- // }
- // var databar = document.getElementById('databar');
- // if (databar) {
- // databar.removeChild(databar.firstChild);
- // var span = document.createElement("span");span.innerHTML = msg_hash[t_id][message_n][3];
- // databar.appendChild(span);
- // }
- }
- }
- }
- }
- req.caching = false; // Запрещаем кэширование
- req.open('POST', 'ajax_getmessage.php', true);
- // Посылаем данные запроса (задаются в виде хэша).
- req.send({ showall:1, k:k, topic_id:t_id, message_n:message_n, direction:direction});
- }
- /* TOOLTOP */
- tooltip = {
- TIP_TIME: 300,
- OUT_TIME: 300,
- divTip: null,
- html:
- '<div id=mainbar class="gensmall" style="position:absolute; background:#FFFFE1; border:1px solid #000000; width:630px; font-weight:normal; overflow:hidden">'+
- '<div id=dragbar style="cursor: move; background:white; padding:4px; border-bottom:1px solid silver"><span><b>Подождите...</b></span></div>' +
- '<div id=databar style="padding:4px"><span>Идет ajax загрузка.<br/>Это может занять некоторое время.</span></div>' +
- '</div>',
- html_close:
- '<span onclick="tooltip.hideTip(this.parentNode.parentNode)" style="POSITION: absolute; RIGHT: 3px; TOP: 3px; cursor:hand; cursor:pointer">'+
- '<b> x </b>' +
- '</span>',
- scheduleShowTip: function(elt, funk, arg1, arg2) {
- var th = this;
- if (th.timeoutTip) th.timeoutTip=clearTimeout(th.timeoutTip);
- if (th.timeoutMouseOut) th.timeoutMouseOut=clearTimeout(th.timeoutMouseOut);
- if (th.divTip && th.divTip.elt == elt) return; // already shown
- th.timeoutTip = setTimeout(function() { th.timeoutTip=null; th.showTip(elt, funk, arg1, arg2) }, th.TIP_TIME);
- },
- scheduleHideTip: function() {
- var th = this;
- if (th.timeoutTip) th.timeoutTip=clearTimeout(th.timeoutTip);
- if (th.timeoutMouseOut) th.timeoutMouseOut=clearTimeout(th.timeoutMouseOut);
- th.timeoutMouseOut = setTimeout(function() { th.timeoutMouseOut=null; th.hideTip() }, th.OUT_TIME);
- },
- showTip: function(elt, funk, arg1, arg2) {
- var th = this;
- th.locked = false;
- th.hideTip();
- // Create tip.
- var span = document.createElement("div"); span.innerHTML = th.html;
- th.divTip = span.childNodes[0];
- th.divTip.elt = elt;
- // Position tip.
- document.body.appendChild(th.divTip);
- var coord = th.getAbsPos(elt);
- var width = th.divTip.offsetWidth;
- var docWidth = document.body.scrollWidth;
- if (coord.x + width > docWidth) coord.x = docWidth - width - 100; // отступ справа
- if (coord.x < 0) coord.x = 0;
- th.divTip.style.left = coord.x + 40 + "px"; // отступ слева
- th.divTip.style.top = (coord.y + elt.offsetHeight + 1) + "px";
- // Events for tip.
- addEvent(th.divTip, 'onmouseover', function(e) {
- th.scheduleShowTip(elt); //, funk, arg1, arg2
- return true;
- })
- addEvent(th.divTip, 'onmouseout', function() {
- if (th.inConfirm) return;
- th.scheduleHideTip();
- return true;
- })
- if (funk) {
- // Fill the tip. ++++++
- var divSubj = th.divTip.childNodes[0];
- var divText = th.divTip.childNodes[1];
- var out = funk(arg1, arg2);
- divSubj.innerHTML = '<span>' + out.subj + th.html_close + '</span>';
- divText.innerHTML = '<span>' + out.text + '</span>';
- }
- },
- hideTip: function() {
- var th = this;
- if (th.locked) return;
- if (!th.divTip) return;
- th.divTip.parentNode.removeChild(th.divTip);
- th.divTip = null;
- },
- getAbsPos: function(p) {
- var s = { x:0, y:0 };
- while (p.offsetParent) {
- s.x += p.offsetLeft;
- s.y += p.offsetTop;
- p = p.offsetParent;
- }
- return s;
- },
- end:0
- }
- // Cross-browser addEventListener()/attachEvent() replacement.
- // addEvent(th.divTip, 'onmouseover', function(e) {
- function addEvent(elt, name, handler, atEnd) {
- name = name.replace(/^(on)?/, 'on');
- var prev = elt[name];
- var tmp = '__tmp';
- elt[name] = function(e) {
- if (!e) e = window.event;
- var result;
- if (!atEnd) {
- elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null; // delete() does not work in IE 5.0 (???!!!)
- if (result === false) return result;
- }
- if (prev) {
- elt[tmp] = prev; result = elt[tmp](e); elt[tmp] = null;
- }
- if (atEnd && result !== false) {
- elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null;
- }
- return result;
- }
- return handler;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement