Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // y88
  2. // tooltip add-on for forum.mista.ru
  3. // v1.33 (fix call funk)
  4.  
  5. var msg_hash = new Array();
  6. var timeoutNewMessage;
  7. var last_n_old = 0;
  8. if (typeof last_n=="undefined") {  // for archived
  9.   last_n = 1000
  10.   document.write("<scr"+"ipt type='text/javascript' language='javascript' src='/css/Subsys_JsHttpRequest.js'></sc"+"ript>");
  11. }
  12.  
  13. for (var i=0; i<document.links.length; i++) attachLinkLivePreview(document.links[i]);
  14.  
  15. var topic_id = 0;
  16. if (document.getElementById('topic_id')) {topic_id = '' + document.getElementById('topic_id').value}
  17. if (topic_id) {
  18.   createToolTips();
  19. }
  20.  
  21.  
  22. // Drag ============
  23. drag_drop = {
  24.     drag_obj:       null,
  25.     offsetx:        0,
  26.     offsety:        0,
  27.     initializeDrag: function(e){
  28.         var evt = window.event || e;
  29.         var el  = evt.target || evt.srcElement;
  30.         while (el) {
  31.           if (el.id == "dragbar") break;
  32.           el = el.parentNode;
  33.         }
  34.         if (el) {
  35.           drag_drop.drag_obj = el.parentNode;
  36.           if (drag_drop.drag_obj) {
  37.             drag_drop.offsetx  = parseInt(drag_drop.drag_obj.style.left) - evt.clientX;
  38.             drag_drop.offsety  = parseInt(drag_drop.drag_obj.style.top)  - evt.clientY;
  39.           }
  40.         }else{
  41.           drag_drop.drag_obj = null;
  42.         }
  43.     },
  44.     end:0
  45. }
  46. addEvent(document, 'onmousedown', drag_drop.initializeDrag);
  47. addEvent(document, 'onmouseup',   function() {drag_drop.drag_obj = null});
  48. addEvent(document, 'onmousemove', function(e) {
  49.      if (drag_drop.drag_obj) {
  50.        var evt = window.event || e;
  51.        drag_drop.drag_obj.style.left = evt.clientX + drag_drop.offsetx + "px"
  52.        drag_drop.drag_obj.style.top  = evt.clientY + drag_drop.offsety + "px"
  53.      }
  54.      //return false;   // not allow select text in IE
  55.     })
  56. // ===========
  57.  
  58.  
  59.  
  60.  
  61. function attachLinkForEveryChild(obj) {
  62.   if (obj.children) {
  63.     for (var i=0; i<obj.children.length; i++) {
  64.       var new_obj = obj.children[i];
  65.       if ( new_obj.tagName == "A" ) {
  66.         attachLinkLivePreview(new_obj);
  67.       }
  68.       attachLinkForEveryChild(new_obj);
  69.     }
  70.   }
  71. }
  72.  
  73. function attachLinkLivePreview(elt) {
  74.   // First detect if this is a topic reference.
  75.   // <a href="topic.php?id=445933" class="agb">
  76.   var p = elt.href.match(/topic\.php\?id=(\d+)$/i);
  77.   if (!p) return;
  78.   elt.href = elt.href + "&"; // change link
  79.   // Create [?] element. Save to elt.
  80.   with (document) {
  81.     var span = createElement('span');
  82.     span.innerHTML = ' <span style="color:#CCC; cursor:pointer; cursor:hand" '
  83.     +'onmouseover="tooltip.scheduleShowTip(this,re_html,0,'+p[1]+')" '
  84.     +'onmouseout="tooltip.scheduleHideTip(this)">[¿]</span>';
  85.     elt.parentNode.insertBefore(span, elt);
  86.     elt.parentNode.insertBefore(elt, span);
  87.     elt = span;
  88.   }
  89. }
  90.  
  91.  
  92.  
  93. function createToolTips() {
  94.  
  95. var t_id = topic_id;
  96. var table_messages = document.getElementById('table_messages');
  97. if (table_messages) {
  98. if (last_n_old!=last_n) { // exist new messages
  99.  
  100. if (!msg_hash[t_id]) { msg_hash[t_id] = new Array() }
  101.  
  102. // read all data and store to array
  103. for (msg_id=last_n_old; msg_id<=last_n; msg_id++) {
  104. //  if (document.getElementById('message_'+msg_id)) {} // row для ajax нет!!!!
  105.   if (document.getElementById('tdmsg'+msg_id)) { // cell
  106.     var obj_tr_msg = document.getElementById('tdmsg'+msg_id).parentNode; // row
  107. //    var msg_author = document.getElementById('tduser'+msg_id).innerHTML;
  108.     var msg_author = obj_tr_msg.cells[0].innerHTML;
  109.     var msg_date   = '';
  110.     var msg_text   = document.getElementById('tdmsg'+msg_id).innerHTML;
  111.     // fix IE bug width="100%"
  112.     msg_text = msg_text.replace(/width="100\%"/gi, 'width="99%"')
  113.  
  114.     msg_hash[t_id][msg_id] = [msg_id,msg_author,msg_date,msg_text];
  115.   }
  116. }
  117.  
  118. // create tooltip
  119. for (msg_id=last_n_old; msg_id<=last_n; msg_id++) {
  120.   var obj_td_msg = document.getElementById('tdmsg'+msg_id);
  121.   if (obj_td_msg) { // cell
  122.     var obj_tr_msg = obj_td_msg.parentNode; // row
  123.     var msg_text = obj_td_msg.innerHTML;
  124.     // 1. (<a href='#0' style='text-decoration:none'>0</a>)
  125.     //    (<a href='topic.php?id=444509&page=3#261' style='text-decoration:none'>261</a>)
  126.     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>')
  127.  
  128.     // 2. <a rel="nofollow" href="#2" class="answer-link interlink" title="BuHu" rev="#218">218</a>
  129.     // fix <a .*?> by hd1
  130.     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>')
  131.  
  132.     // 3. добавляем к URL картинок [?] +tooltip --------------------------
  133.     // <a href="http://*.jpg" target="_blank" >http://*.jpg</a>
  134.     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>')
  135.  
  136.     //obj_td_msg.innerHTML = msg_text; // FF only !!!
  137.  
  138.     //<td style="padding:10px;" id="tdmsg5" valign="top" bgcolor="#FDFDFD" class="leftbottomgray">
  139.     obj_tr_msg.deleteCell(1);
  140.     var td_m = obj_tr_msg.insertCell(1);
  141.     td_m.id="tdmsg"+msg_id;
  142.     td_m.vAlign="top";
  143.     td_m.align="left";
  144.     //td_m.padding="10px";
  145.     td_m.bgColor="#FDFDFD";
  146.  
  147.     td_m.style.borderBottom="1px solid #EEE";
  148.     td_m.style.borderLeft="1px solid #EEE";
  149.  
  150.     td_m.innerHTML = msg_text;
  151.  
  152.     attachLinkForEveryChild(td_m); // + add [?]
  153.   }
  154. }
  155.  
  156. last_n_old = last_n;
  157. }
  158. }
  159. // add shedule for new messages
  160. if (timeoutNewMessage) timeoutNewMessage=clearTimeout(timeoutNewMessage);
  161. timeoutNewMessage = setTimeout(createToolTips, 3000);
  162. }
  163.  
  164.  
  165. function nav_html(t_id) {
  166.   // create navigations for future features
  167.   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>';
  168. }
  169.  
  170.  
  171. function re_html(id,t_id) {
  172.   var data_exist = 0;
  173.   if (!t_id) {t_id = topic_id}
  174.   if (msg_hash[t_id]) {
  175.    if (msg_hash[t_id][id]) {
  176.     data_exist = 1;
  177.     return {
  178.       subj: "      "+msg_hash[t_id][id][1],
  179.       text: ""+msg_hash[t_id][id][3]+""
  180.     }
  181.    }
  182.   }
  183.   if (!data_exist) {
  184.     //ajax load
  185.     getmessage_tooltip(id,t_id);
  186.     return {subj:'wait ajax '+id,text:'loading...'}
  187.   }
  188. }
  189.  
  190.  
  191. function img_view(img_url) {
  192.   if (!img_url) return {subj:'error',text:'no image...'}
  193.   return {
  194.     subj: "      <b>"+img_url+"</b>",
  195.     text: "<img src='"+img_url+"'>"
  196.   }
  197. }
  198.  
  199.  
  200. //функция для подгрузки сообщения
  201. function getmessage_tooltip(message_n, t_id) {
  202.   var k = 1;
  203.   if (!t_id) {t_id = topic_id}
  204.   var direction = 0;
  205.  
  206.   var req = new Subsys_JsHttpRequest_Js();
  207.   // Код, АВТОМАТИЧЕСКИ вызываемый при окончании загрузки.
  208.   req.onreadystatechange = function() {
  209.     if (req.readyState == 4) {
  210.       if (req.responseJS){
  211.         var response = req.responseJS.client_action;
  212.         if ((response==1) || (response==2)) {
  213.           var author = 'ajax::  ERROR';
  214.           var message = req.responseJS.message_html;
  215.  
  216.           if (response==1) { // ok
  217.             message = message.replace(/<span.*\]<\/span>/gi, ''); // delete top navigations
  218.  
  219.             //<BR><B>user_name</B>:message text
  220.             author = '';
  221.             message = message.replace(/\r/g, ''); // fix FF
  222.             if (message.match(/<BR><B>(.+)<\/B>:(.+)/i)) {
  223.               author  = "<b>"+RegExp.$1+"</b>";
  224.               message = RegExp.$2;
  225.             }
  226.           }
  227.           if (!msg_hash[t_id]) { msg_hash[t_id] = new Array() }
  228.           msg_hash[t_id][message_n] = [message_n, author, '', message];
  229.  
  230.           // not work time of time ???
  231.           document.getElementById('dragbar').innerHTML = msg_hash[t_id][message_n][1] + tooltip.html_close;
  232.           document.getElementById('databar').innerHTML = msg_hash[t_id][message_n][3];
  233.  
  234. //          var dragbar =  document.getElementById('dragbar');
  235. //          if (dragbar) {
  236. //            dragbar.removeChild(dragbar.firstChild);
  237. //            var span = document.createElement("span");span.innerHTML = msg_hash[t_id][message_n][1] + tooltip.html_close;
  238. //            dragbar.appendChild(span);
  239. //          }
  240. //          var databar =  document.getElementById('databar');
  241. //          if (databar) {
  242. //            databar.removeChild(databar.firstChild);
  243. //            var span = document.createElement("span");span.innerHTML = msg_hash[t_id][message_n][3];
  244. //            databar.appendChild(span);
  245. //          }
  246.         }
  247.       }
  248.     }
  249.   }
  250.  
  251.   req.caching = false; // Запрещаем кэширование
  252.   req.open('POST', 'ajax_getmessage.php', true);
  253.  
  254.   // Посылаем данные запроса (задаются в виде хэша).
  255.   req.send({ showall:1, k:k, topic_id:t_id, message_n:message_n, direction:direction});
  256. }
  257.  
  258.  
  259.  
  260. /*     TOOLTOP     */
  261. tooltip = {
  262.         TIP_TIME:          300,
  263.         OUT_TIME:          300,
  264.         divTip:            null,
  265.         html:
  266.                 '<div id=mainbar class="gensmall" style="position:absolute; background:#FFFFE1; border:1px solid #000000; width:630px; font-weight:normal; overflow:hidden">'+
  267.                         '<div id=dragbar style="cursor: move; background:white; padding:4px; border-bottom:1px solid silver"><span><b>Подождите...</b></span></div>' +
  268.                         '<div id=databar style="padding:4px"><span>Идет ajax загрузка.<br/>Это может занять некоторое время.</span></div>' +
  269.                 '</div>',
  270.         html_close:
  271.                 '<span onclick="tooltip.hideTip(this.parentNode.parentNode)" style="POSITION: absolute; RIGHT: 3px; TOP: 3px; cursor:hand; cursor:pointer">'+
  272.                         '<b> x </b>' +
  273.                 '</span>',
  274.  
  275.         scheduleShowTip: function(elt, funk, arg1, arg2) {
  276.                 var th = this;
  277.                 if (th.timeoutTip) th.timeoutTip=clearTimeout(th.timeoutTip);
  278.                 if (th.timeoutMouseOut) th.timeoutMouseOut=clearTimeout(th.timeoutMouseOut);
  279.                 if (th.divTip && th.divTip.elt == elt) return; // already shown
  280.                 th.timeoutTip = setTimeout(function() { th.timeoutTip=null; th.showTip(elt, funk, arg1, arg2) }, th.TIP_TIME);
  281.         },
  282.  
  283.         scheduleHideTip: function() {
  284.                 var th = this;
  285.                 if (th.timeoutTip) th.timeoutTip=clearTimeout(th.timeoutTip);
  286.                 if (th.timeoutMouseOut) th.timeoutMouseOut=clearTimeout(th.timeoutMouseOut);
  287.                 th.timeoutMouseOut = setTimeout(function() { th.timeoutMouseOut=null; th.hideTip() }, th.OUT_TIME);
  288.         },
  289.  
  290.         showTip: function(elt, funk, arg1, arg2) {
  291.                 var th = this;
  292.                 th.locked = false;
  293.                 th.hideTip();
  294.                 // Create tip.
  295.                 var span = document.createElement("div"); span.innerHTML = th.html;
  296.                 th.divTip = span.childNodes[0];
  297.                 th.divTip.elt = elt;
  298.  
  299.                 // Position tip.
  300.                 document.body.appendChild(th.divTip);
  301.                 var coord = th.getAbsPos(elt);
  302.                 var width = th.divTip.offsetWidth;
  303.                 var docWidth = document.body.scrollWidth;
  304.                 if (coord.x + width > docWidth) coord.x = docWidth - width - 100;   // отступ справа
  305.                 if (coord.x < 0) coord.x = 0;
  306.                 th.divTip.style.left = coord.x + 40 + "px";     // отступ слева
  307.                 th.divTip.style.top = (coord.y + elt.offsetHeight + 1) + "px";
  308.                 // Events for tip.
  309.                 addEvent(th.divTip, 'onmouseover', function(e) {
  310.                         th.scheduleShowTip(elt); //, funk, arg1, arg2
  311.                         return true;
  312.                 })
  313.                 addEvent(th.divTip, 'onmouseout', function() {
  314.                         if (th.inConfirm) return;
  315.                         th.scheduleHideTip();
  316.                         return true;
  317.                 })
  318.  
  319.                 if (funk) {
  320.                 // Fill the tip. ++++++
  321.                 var divSubj = th.divTip.childNodes[0];
  322.                 var divText = th.divTip.childNodes[1];
  323.                 var out = funk(arg1, arg2);
  324.                 divSubj.innerHTML = '<span>' + out.subj + th.html_close + '</span>';
  325.                 divText.innerHTML = '<span>' + out.text + '</span>';
  326.                 }
  327.         },
  328.  
  329.         hideTip: function() {
  330.                 var th = this;
  331.                 if (th.locked) return;
  332.                 if (!th.divTip) return;
  333.                 th.divTip.parentNode.removeChild(th.divTip);
  334.                 th.divTip = null;
  335.         },
  336.         getAbsPos: function(p) {
  337.                 var s = { x:0, y:0 };
  338.                 while (p.offsetParent) {
  339.                         s.x += p.offsetLeft;
  340.                         s.y += p.offsetTop;
  341.                         p = p.offsetParent;
  342.                 }
  343.                 return s;
  344.         },
  345.  
  346.         end:0
  347. }
  348.  
  349. // Cross-browser addEventListener()/attachEvent() replacement.
  350. //       addEvent(th.divTip, 'onmouseover', function(e) {
  351. function addEvent(elt, name, handler, atEnd) {
  352.   name = name.replace(/^(on)?/, 'on');
  353.   var prev = elt[name];
  354.   var tmp = '__tmp';
  355.   elt[name] = function(e) {
  356.     if (!e) e = window.event;
  357.     var result;
  358.     if (!atEnd) {
  359.       elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null; // delete() does not work in IE 5.0 (???!!!)
  360.       if (result === false) return result;
  361.     }
  362.     if (prev) {
  363.       elt[tmp] = prev; result = elt[tmp](e); elt[tmp] = null;
  364.     }
  365.     if (atEnd && result !== false) {
  366.       elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null;
  367.     }
  368.     return result;
  369.   }
  370.   return handler;
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement