plugins/ajim/shoutbox.js
changeset 0 db23957ad1c7
child 1 f7b24c744747
equal deleted inserted replaced
-1:000000000000 0:db23957ad1c7
       
     1 var ajim_last_refresh = 0;
       
     2 
       
     3 window.ajim_init = function()
       
     4 {
       
     5   window.ajim_authed = ( auth_level >= USER_LEVEL_CHPREF && ajim_can_mod );
       
     6   // have it immediately pull in whatever comments the server wants us to pull
       
     7   ajim_watch(true);
       
     8   ajim_setup_textarea();
       
     9 }
       
    10 
       
    11 addOnloadHook(ajim_init);
       
    12 
       
    13 window.ajim_watch = function(immediate)
       
    14 {
       
    15   ajim_send_request({
       
    16       mode: 'watch',
       
    17       lastrefresh: immediate ? 0 : ajim_last_refresh
       
    18   });
       
    19 }
       
    20 
       
    21 window.ajim_send_request = function(request)
       
    22 {
       
    23   request = ajaxEscape(toJSONString(request));
       
    24   ajaxPost(makeUrlNS('Special', 'AjimJson'), 'r=' + request, function()
       
    25     {
       
    26       if ( ajax.readyState == 4 && ajax.status == 200 )
       
    27       {
       
    28         var response = String(ajax.responseText + '');
       
    29         if ( !check_json_response(response) )
       
    30         {
       
    31           ajim_handle_invalid_json(response);
       
    32           return false;
       
    33         }
       
    34         response = parseJSON(response);
       
    35         ajim_handle_response(response);
       
    36       }
       
    37     });
       
    38 }
       
    39 
       
    40 window.ajim_handle_invalid_json = function(response)
       
    41 {
       
    42   ajim_fail_message_list();
       
    43   load_component('l10n');
       
    44   var messages = document.getElementById('ajim_messages');
       
    45   messages.innerHTML = $lang.get('ajim_err_bad_json') + '<br />';
       
    46   messages.appendChild(document.createTextNode(response));
       
    47 }
       
    48 
       
    49 window.ajim_handle_response = function(response)
       
    50 {
       
    51   switch(response.mode)
       
    52   {
       
    53     case 'messages':
       
    54       ajim_show_message_list();
       
    55       
       
    56       var noposts = document.getElementById('ajim_noposts');
       
    57       if ( noposts )
       
    58         noposts.parentNode.removeChild(noposts);
       
    59       for ( var i = response.messages.length - 1; i >= 0; i-- )
       
    60       {
       
    61         ajim_inject_message(response.messages[i]);
       
    62       }
       
    63       if ( response.now )
       
    64       {
       
    65         window.ajim_last_refresh = response.now;
       
    66         ajim_watch();
       
    67       }
       
    68       break;
       
    69     case 'delete':
       
    70       var message = document.getElementById('ajim_message:' + response.message_id);
       
    71       if ( message )
       
    72       {
       
    73         message.parentNode.removeChild(message);
       
    74       }
       
    75       ajim_show_message_list();
       
    76       break;
       
    77     case 'update':
       
    78       var message = document.getElementById('ajim_message:' + response.message_id);
       
    79       if ( message )
       
    80       {
       
    81         var inner = getElementsByClassName(message, 'div', 'ajim_message_inner')[0];
       
    82         inner.innerHTML = response.message_html;
       
    83         message.meta.message_src = response.message;
       
    84       }
       
    85       ajim_show_message_list();
       
    86       break;
       
    87     case 'error':
       
    88       ajim_fail_message_list();
       
    89       ajim_show_error(response.error);
       
    90       break;
       
    91   }
       
    92 }
       
    93 
       
    94 window.ajim_draw_message = function(message)
       
    95 {
       
    96   var div = document.createElement('div');
       
    97   div.id = 'ajim_message:' + message.message_id;
       
    98   div.className = 'ajim_message';
       
    99   div.meta = {
       
   100     message_id: message.message_id,
       
   101     message_src: message.message
       
   102   };
       
   103   
       
   104   // deleted message?
       
   105   if ( message.message == '' )
       
   106     return div;
       
   107   
       
   108   // *sigh* actually draw a message.
       
   109   
       
   110   // mod buttons
       
   111     var modbuttons = document.createElement('div');
       
   112     modbuttons.className = 'ajim_modbuttons';
       
   113     if ( !window.ajim_authed && ( ( message.user_id != 1 && message.user_id != ajim_user_id ) || message.user_id == 1 ) )
       
   114       modbuttons.style.display = 'none';
       
   115     
       
   116     var edbtn = document.createElement('a');
       
   117     edbtn.href = '#';
       
   118     edbtn.className = 'ajim_btn_edit';
       
   119     edbtn.appendChild(document.createTextNode(ajim_str_edit));
       
   120     edbtn.onclick = ajim_handle_click_edit;
       
   121     modbuttons.appendChild(edbtn);
       
   122     
       
   123     modbuttons.appendChild(document.createTextNode(' | '));
       
   124     
       
   125     var delbtn = document.createElement('a');
       
   126     delbtn.href = '#';
       
   127     delbtn.className = 'ajim_btn_delete';
       
   128     delbtn.appendChild(document.createTextNode(ajim_str_delete));
       
   129     delbtn.onclick = ajim_handle_click_delete;
       
   130     modbuttons.appendChild(delbtn);
       
   131     
       
   132     div.appendChild(modbuttons);
       
   133     
       
   134   // username
       
   135     var el = message.user_id == 1 ? 'span' : 'a';
       
   136     var username = document.createElement(el);
       
   137     username.appendChild(document.createTextNode(message.username));
       
   138     username.title = message.rank_info.rank_title;
       
   139     username.setAttribute('style', message.rank_info.rank_style);
       
   140     if ( message.user_id != 1 )
       
   141       username.href = makeUrlNS('User', message.username);
       
   142     div.appendChild(username);
       
   143     
       
   144   // message header (does a clear: both)
       
   145     var messagehead = document.createElement('div');
       
   146     messagehead.className = 'ajim_messagehead';
       
   147     div.appendChild(messagehead);
       
   148     
       
   149   // date
       
   150     var date = document.createElement('div');
       
   151     date.className = 'ajim_timestamp';
       
   152     date.appendChild(document.createTextNode(message.human_time));
       
   153     div.appendChild(date);
       
   154     
       
   155   // *finally* message
       
   156     var msgdata = document.createElement('div');
       
   157     msgdata.innerHTML = message.message_html;
       
   158     msgdata.className = 'ajim_message_inner';
       
   159     div.appendChild(msgdata);
       
   160   
       
   161   // message footer
       
   162     var messagefoot = document.createElement('div');
       
   163     messagefoot.className = 'ajim_messagefoot';
       
   164     div.appendChild(messagefoot);
       
   165   
       
   166   return div;
       
   167 }
       
   168 
       
   169 window.ajim_inject_message = function(message)
       
   170 {
       
   171   var drawn = ajim_draw_message(message);
       
   172   var existing = document.getElementById('ajim_message:' + message.message_id);
       
   173   if ( existing )
       
   174   {
       
   175     insertAfter(existing.parentNode, drawn, existing);
       
   176     existing.parentNode.removeChild(existing);
       
   177   }
       
   178   else
       
   179   {
       
   180     ajim_inject_object(drawn);
       
   181   }
       
   182 }
       
   183 
       
   184 window.ajim_inject_object = function(element)
       
   185 {
       
   186   var error = document.getElementById('ajim_error');
       
   187   insertAfter(error.parentNode, element, error);
       
   188 }
       
   189 
       
   190 window.ajim_show_error = function(message)
       
   191 {
       
   192   if ( window.ajim_error_timeout )
       
   193     window.clearTimeout(ajim_error_timeout);
       
   194   var error = document.getElementById('ajim_error');
       
   195   error.innerHTML = '';
       
   196   error.appendChild(document.createTextNode(message));
       
   197   window.ajim_error_timeout = window.setTimeout(ajim_clear_error, 5000);
       
   198 }
       
   199 
       
   200 window.ajim_clear_error = function()
       
   201 {
       
   202   var error = document.getElementById('ajim_error');
       
   203   error.innerHTML = '';
       
   204 }
       
   205 
       
   206 window.ajim_handle_click_edit = function()
       
   207 {
       
   208   if ( this.editing )
       
   209   {
       
   210     this.editing = false;
       
   211     
       
   212     this.firstChild.nodeValue = $lang.get('ajim_btn_edit');
       
   213     this.nextSibling.nextSibling.firstChild.nodeValue = $lang.get('ajim_btn_delete');
       
   214     
       
   215     var newsrc = this.ta.value;
       
   216     this.ta.parentNode.removeChild(this.ta);
       
   217     this.inner.style.display = 'block';
       
   218     
       
   219     if ( newsrc == '' )
       
   220       ajim_show_error($lang.get('ajim_err_no_post'));
       
   221     
       
   222     ajim_hide_message_list();
       
   223     
       
   224     ajim_send_request({
       
   225         mode: 'update',
       
   226         message_id: this.parentNode.parentNode.meta.message_id,
       
   227         message: newsrc
       
   228       });
       
   229   }
       
   230   else
       
   231   {
       
   232     load_component('l10n');
       
   233     this.firstChild.nodeValue = $lang.get('ajim_btn_save');
       
   234     this.nextSibling.nextSibling.firstChild.nodeValue = $lang.get('etc_cancel').toLowerCase();
       
   235     this.editing = true;
       
   236     
       
   237     var src = this.parentNode.parentNode.meta.message_src;
       
   238     var inner = this.parentNode.nextSibling.nextSibling.nextSibling.nextSibling;
       
   239     
       
   240     var ta = document.createElement('textarea');
       
   241     ta.value = src;
       
   242     ta.style.fontSize = '100%';
       
   243     ta.style.width = '93%';
       
   244     inner.style.display = 'none';
       
   245     this.parentNode.parentNode.appendChild(ta);
       
   246     this.ta = ta;
       
   247     this.inner = inner;
       
   248   }
       
   249   return false;
       
   250 }
       
   251 
       
   252 window.ajim_handle_click_delete = function()
       
   253 {
       
   254   var editlink = this.previousSibling.previousSibling;
       
   255   if ( editlink.editing )
       
   256   {
       
   257     editlink.editing = false;
       
   258     
       
   259     editlink.firstChild.nodeValue = $lang.get('ajim_btn_edit');
       
   260     this.firstChild.nodeValue = $lang.get('ajim_btn_delete');
       
   261     
       
   262     editlink.ta.parentNode.removeChild(editlink.ta);
       
   263     editlink.inner.style.display = 'block';
       
   264   }
       
   265   else
       
   266   {
       
   267     ajim_hide_message_list();
       
   268     var message_id = this.parentNode.parentNode.meta.message_id;
       
   269     ajim_send_request({
       
   270         mode: 'delete',
       
   271         message_id: message_id
       
   272       });
       
   273   }
       
   274   return false;
       
   275 }
       
   276 
       
   277 window.ajim_handle_click_mod = function()
       
   278 {
       
   279   if ( auth_level >= USER_LEVEL_CHPREF )
       
   280     return false;
       
   281   load_component('login');
       
   282   
       
   283   ajaxLogonInit(function(k)
       
   284     {
       
   285       ajaxLoginReplaceSIDInline(k, false, USER_LEVEL_CHPREF);
       
   286       window.setTimeout(function()
       
   287         {
       
   288           mb_current_obj.destroy();
       
   289           ajim_enable_mod_tools();
       
   290         }, 500);
       
   291     }, USER_LEVEL_CHPREF);
       
   292   
       
   293   return false;
       
   294 }
       
   295 
       
   296 window.ajim_enable_mod_tools = function()
       
   297 {
       
   298   var mods = document.getElementsByClassName('div', 'ajim_modbuttons');
       
   299   for ( var i = 0; i < mods.length; i++ )
       
   300   {
       
   301     mods[i].style.display = 'block';
       
   302   }
       
   303   document.getElementById('ajim_mod').innerHTML = '';
       
   304 }
       
   305 
       
   306 window.ajim_setup_textarea = function()
       
   307 {
       
   308   var ta = document.getElementById('ajim_message');
       
   309   if ( !ta )
       
   310     return false;
       
   311   
       
   312   ta.shift = false;
       
   313   ta.onkeypress = function(e)
       
   314   {
       
   315     if ( !e )
       
   316       e = window.event;
       
   317     if ( !e )
       
   318       return true;
       
   319     if ( typeof(e.keyCode) == undefined )
       
   320       return true;
       
   321     if ( e.keyCode == 13 )
       
   322     {
       
   323       if ( !this.shift )
       
   324       {
       
   325         ajim_submit_message();
       
   326         e.preventDefault();
       
   327         return false;
       
   328       }
       
   329     }
       
   330   }
       
   331   ta.onkeydown = function(e)
       
   332   {
       
   333     if ( !e )
       
   334       e = window.event;
       
   335     if ( !e )
       
   336       return true;
       
   337     if ( !e.keyCode )
       
   338       return true;
       
   339     if ( e.keyCode == 16 )
       
   340     {
       
   341       this.shift = true;
       
   342     }
       
   343   }
       
   344   ta.onkeyup = function(e)
       
   345   {
       
   346     if ( !e )
       
   347       e = window.event;
       
   348     if ( !e )
       
   349       return true;
       
   350     if ( !e.keyCode )
       
   351       return true;
       
   352     if ( e.keyCode == 16 )
       
   353     {
       
   354       this.shift = false;
       
   355     }
       
   356   }
       
   357 }
       
   358 
       
   359 window.ajim_submit_message = function()
       
   360 {
       
   361   var ta = document.getElementById('ajim_message');
       
   362   if ( ta.value == '' )
       
   363   {
       
   364     load_component('l10n');
       
   365     ajim_show_error($lang.get('ajim_err_no_post'));
       
   366     return false;
       
   367   }
       
   368   
       
   369   var user = document.getElementById('ajim_nickname');
       
   370   if ( user.value == '' )
       
   371     user.value = 'Guest';
       
   372   
       
   373   ajim_hide_message_list();
       
   374   
       
   375   ajim_send_request({
       
   376       mode: 'submit',
       
   377       user: user.value,
       
   378       message: ta.value
       
   379     });
       
   380   
       
   381   ta.value = '';
       
   382 }
       
   383 
       
   384 window.ajim_hide_message_list = function()
       
   385 {
       
   386   var messages = document.getElementById('ajim_messages');
       
   387   window.ajim_submit_whitey = whiteOutElement(messages);
       
   388 }
       
   389 
       
   390 window.ajim_show_message_list = function()
       
   391 {
       
   392   if ( window.ajim_submit_whitey )
       
   393     whiteOutReportSuccess(window.ajim_submit_whitey);
       
   394   window.ajim_submit_whitey = false;
       
   395 }
       
   396 
       
   397 window.ajim_fail_message_list = function()
       
   398 {
       
   399   if ( window.ajim_submit_whitey )
       
   400     whiteOutReportFailure(window.ajim_submit_whitey);
       
   401   window.ajim_submit_whitey = false;
       
   402 }