includes/clientside/static/editor.js
changeset 336 bfa2e9c23f03
parent 335 67bd3121a12e
child 348 87e08a6e4fec
equal deleted inserted replaced
335:67bd3121a12e 336:bfa2e9c23f03
    78           unselectAllButtonsMinor();
    78           unselectAllButtonsMinor();
    79           new messagebox(MB_OK | MB_ICONSTOP, $lang.get('editor_err_access_denied_title'), $lang.get('editor_err_access_denied_body'));
    79           new messagebox(MB_OK | MB_ICONSTOP, $lang.get('editor_err_access_denied_title'), $lang.get('editor_err_access_denied_body'));
    80           return false;
    80           return false;
    81         }
    81         }
    82         
    82         
    83         ajaxBuildEditor(response.src, (!response.auth_edit), response.time);
    83         // do we need to enter a captcha before saving the page?
       
    84         var captcha_hash = ( response.require_captcha ) ? response.captcha_id : false;
       
    85         
       
    86         ajaxBuildEditor(response.src, (!response.auth_edit), response.time, captcha_hash);
    84       }
    87       }
    85     });
    88     });
    86 }
    89 }
    87 
    90 
    88 function ajaxBuildEditor(content, readonly, timestamp)
    91 function ajaxBuildEditor(content, readonly, timestamp, captcha_hash)
    89 {
    92 {
    90   // Set flags
    93   // Set flags
    91   // We don't want the fancy confirmation framework to trigger if the user is only viewing the page source
    94   // We don't want the fancy confirmation framework to trigger if the user is only viewing the page source
    92   if ( !readonly )
    95   if ( !readonly )
    93   {
    96   {
   253     td2_2.appendChild(label);
   256     td2_2.appendChild(label);
   254     
   257     
   255     tr2.appendChild(td2_1);
   258     tr2.appendChild(td2_1);
   256     tr2.appendChild(td2_2);
   259     tr2.appendChild(td2_2);
   257     
   260     
       
   261     if ( captcha_hash )
       
   262     {
       
   263       // generate captcha field (effectively third row)
       
   264       var tr4 = document.createElement('tr');
       
   265       var td4_1 = document.createElement('td');
       
   266       var td4_2 = document.createElement('td');
       
   267       td4_1.className = 'row2';
       
   268       td4_2.className = 'row1';
       
   269       
       
   270       td4_1.appendChild(document.createTextNode($lang.get('editor_lbl_field_captcha')));
       
   271       td4_1.appendChild(document.createElement('br'));
       
   272       var small2 = document.createElement('small');
       
   273       small2.appendChild(document.createTextNode($lang.get('editor_msg_captcha_pleaseenter')));
       
   274       small2.appendChild(document.createElement('br'));
       
   275       small2.appendChild(document.createElement('br'));
       
   276       small2.appendChild(document.createTextNode($lang.get('editor_msg_captcha_blind')));
       
   277       td4_1.appendChild(small2);
       
   278       
       
   279       var img = document.createElement('img');
       
   280       img.src = makeUrlNS('Special', 'Captcha/' + captcha_hash);
       
   281       img._captchaHash = captcha_hash;
       
   282       img.id = 'enano_editor_captcha_img';
       
   283       img.onclick = function()
       
   284       {
       
   285         this.src = makeUrlNS('Special', 'Captcha/' + this._captchaHash + '/' + Math.floor(Math.random() * 100000));
       
   286       }
       
   287       img.style.cursor = 'pointer';
       
   288       td4_2.appendChild(img);
       
   289       td4_2.appendChild(document.createElement('br'));
       
   290       td4_2.appendChild(document.createTextNode($lang.get('editor_lbl_field_captcha_code') + ' '));
       
   291       var input = document.createElement('input');
       
   292       input.type = 'text';
       
   293       input.id = 'enano_editor_field_captcha';
       
   294       input._captchaHash = captcha_hash;
       
   295       input.size = '9';
       
   296       td4_2.appendChild(input);
       
   297       
       
   298       tr4.appendChild(td4_1);
       
   299       tr4.appendChild(td4_2);
       
   300     }
       
   301     
   258     // Third row: controls
   302     // Third row: controls
   259     var tr3 = document.createElement('tr');
   303     var tr3 = document.createElement('tr');
   260     var td3 = document.createElement('th');
   304     var td3 = document.createElement('th');
   261     td3.setAttribute('colspan', '2');
   305     td3.setAttribute('colspan', '2');
   262     td3.className = 'subhead';
   306     td3.className = 'subhead';
   300     td3.appendChild(btn_cancel);
   344     td3.appendChild(btn_cancel);
   301     tr3.appendChild(td3);
   345     tr3.appendChild(td3);
   302     
   346     
   303     metatable.appendChild(tr1);
   347     metatable.appendChild(tr1);
   304     metatable.appendChild(tr2);
   348     metatable.appendChild(tr2);
       
   349     if ( captcha_hash )
       
   350     {
       
   351       metatable.appendChild(tr4);
       
   352     }
   305     metatable.appendChild(tr3);
   353     metatable.appendChild(tr3);
   306   }
   354   }
   307   tblholder.appendChild(metatable);
   355   tblholder.appendChild(metatable);
   308   
   356   
   309   // Edit disclaimer/notice
   357   // Edit disclaimer/notice
   337 
   385 
   338 function ajaxEditorSave()
   386 function ajaxEditorSave()
   339 {
   387 {
   340   ajaxSetEditorLoading();
   388   ajaxSetEditorLoading();
   341   var ta_content = $('ajaxEditArea').getContent();
   389   var ta_content = $('ajaxEditArea').getContent();
       
   390   
       
   391   if ( ta_content == '' || ta_content == '<p></p>' || ta_content == '<p>&nbsp;</p>' )
       
   392   {
       
   393     new messagebox(MB_OK|MB_ICONSTOP, $lang.get('editor_err_no_text_title'), $lang.get('editor_err_no_text_body'));
       
   394     ajaxUnSetEditorLoading();
       
   395     return false;
       
   396   }
       
   397   
   342   var edit_summ = $('enano_editor_field_summary').object.value;
   398   var edit_summ = $('enano_editor_field_summary').object.value;
   343   if ( !edit_summ )
   399   if ( !edit_summ )
   344     edit_summ = '';
   400     edit_summ = '';
   345   var is_minor = ( $('enano_editor_field_minor').object.checked ) ? 1 : 0;
   401   var is_minor = ( $('enano_editor_field_minor').object.checked ) ? 1 : 0;
   346   var timestamp = $('ajaxEditArea').object._edTimestamp;
   402   var timestamp = $('ajaxEditArea').object._edTimestamp;
   349     src: ta_content,
   405     src: ta_content,
   350     summary: edit_summ,
   406     summary: edit_summ,
   351     minor_edit: is_minor,
   407     minor_edit: is_minor,
   352     time: timestamp
   408     time: timestamp
   353   };
   409   };
       
   410   
       
   411   // Do we need to add captcha info?
       
   412   if ( document.getElementById('enano_editor_field_captcha') )
       
   413   {
       
   414     var captcha_field = document.getElementById('enano_editor_field_captcha');
       
   415     if ( captcha_field.value == '' )
       
   416     {
       
   417       new messagebox(MB_OK|MB_ICONSTOP, $lang.get('editor_err_need_captcha_title'), $lang.get('editor_err_need_captcha_body'));
       
   418       ajaxUnSetEditorLoading();
       
   419       return false;
       
   420     }
       
   421     json_packet.captcha_code = captcha_field.value;
       
   422     json_packet.captcha_id = captcha_field._captchaHash;
       
   423   }
       
   424   
   354   json_packet = ajaxEscape(toJSONString(json_packet));
   425   json_packet = ajaxEscape(toJSONString(json_packet));
   355   ajaxPost(stdAjaxPrefix + '&_mode=savepage_json', 'r=' + json_packet, function()
   426   ajaxPost(stdAjaxPrefix + '&_mode=savepage_json', 'r=' + json_packet, function()
   356     {
   427     {
   357       if ( ajax.readyState == 4 )
   428       if ( ajax.readyState == 4 )
   358       {
   429       {
   372           return false;
   443           return false;
   373         }
   444         }
   374         // This will be used if the PageProcessor generated errors (usually security/permissions related)
   445         // This will be used if the PageProcessor generated errors (usually security/permissions related)
   375         if ( response.mode == 'errors' )
   446         if ( response.mode == 'errors' )
   376         {
   447         {
       
   448           // This will be true if the user entered a captcha code incorrectly, thus
       
   449           // invalidating the code and requiring a new image to be generated.
       
   450           if ( response.new_captcha )
       
   451           {
       
   452             // Generate the new captcha field
       
   453             var img = document.getElementById('enano_editor_captcha_img');
       
   454             var input = document.getElementById('enano_editor_field_captcha');
       
   455             if ( img && input )
       
   456             {
       
   457               img._captchaHash = response.new_captcha;
       
   458               input._captchaHash = response.new_captcha;
       
   459               img.src = makeUrlNS('Special', 'Captcha/' + response.new_captcha);
       
   460               input.value = '';
       
   461             }
       
   462           }
   377           var errors = '<ul><li>' + implode('</li><li>', response.errors) + '</li></ul>';
   463           var errors = '<ul><li>' + implode('</li><li>', response.errors) + '</li></ul>';
   378           new messagebox(MB_OK | MB_ICONSTOP, $lang.get('editor_err_save_title'), $lang.get('editor_err_save_body') + errors);
   464           new messagebox(MB_OK | MB_ICONSTOP, $lang.get('editor_err_save_title'), $lang.get('editor_err_save_body') + errors);
   379           return false;
   465           return false;
   380         }
   466         }
   381         // If someone else got to the page first, warn the user
   467         // If someone else got to the page first, warn the user
   390         {
   476         {
   391           // The save was successful; reset flags and make another request for the new page content
   477           // The save was successful; reset flags and make another request for the new page content
   392           setAjaxLoading();
   478           setAjaxLoading();
   393           editor_open = false;
   479           editor_open = false;
   394           enableUnload();
   480           enableUnload();
       
   481           changeOpac(0, 'ajaxEditContainer');
   395           ajaxGet(stdAjaxPrefix + '&_mode=getpage&noheaders', function()
   482           ajaxGet(stdAjaxPrefix + '&_mode=getpage&noheaders', function()
   396             {
   483             {
   397               if ( ajax.readyState == 4 )
   484               if ( ajax.readyState == 4 )
   398               {
   485               {
   399                 unsetAjaxLoading();
   486                 unsetAjaxLoading();
   400                 document.getElementById('ajaxEditContainer').innerHTML = '<div class="usermessage">' + $lang.get('editor_msg_saved') + '</div>' + ajax.responseText;
       
   401                 selectButtonMajor('article');
   487                 selectButtonMajor('article');
   402                 unselectAllButtonsMinor();
   488                 unselectAllButtonsMinor();
       
   489                 
       
   490                 document.getElementById('ajaxEditContainer').innerHTML = '<div class="usermessage">' + $lang.get('editor_msg_saved') + '</div>' + ajax.responseText;
       
   491                 opacity('ajaxEditContainer', 0, 100, 1000);
   403               }
   492               }
   404             });
   493             });
   405         }
   494         }
   406       }
   495       }
   407     }, true);
   496     }, true);