includes/clientside/static/rank-manager.js
changeset 628 ab6f55abb17e
parent 563 0103428e2179
child 651 ce9d78d7251d
equal deleted inserted replaced
627:460e483987ab 628:ab6f55abb17e
     2  * Creates a control that can be used to edit a rank.
     2  * Creates a control that can be used to edit a rank.
     3  */
     3  */
     4 
     4 
     5 var RankEditorControl = function(rankdata)
     5 var RankEditorControl = function(rankdata)
     6 {
     6 {
     7   this.rankdata = rankdata;
     7   this.rankdata = ( typeof(rankdata) == 'object' ) ? rankdata : {};
     8   
     8   
     9   // have the browser parse CSS for us and use an anchor to be as close
     9   // have the browser parse CSS for us and use an anchor to be as close
    10   // as possible in calculating CSS
    10   // as possible in calculating CSS
    11   
    11   
    12   // this is kind of a hack as it relies on setAttribute/getAttribute in
    12   // this is kind of a hack as it relies on setAttribute/getAttribute in
    23   {
    23   {
    24     var editor = document.createElement('div');
    24     var editor = document.createElement('div');
    25     editor.className = 'tblholder';
    25     editor.className = 'tblholder';
    26     // stash this editor instance in the parent div for later function calls
    26     // stash this editor instance in the parent div for later function calls
    27     editor.editor = this;
    27     editor.editor = this;
       
    28     this.wrapperdiv = editor;
       
    29     editor.style.width = '100%';
    28     
    30     
    29     // tables suck.
    31     // tables suck.
    30     var table = document.createElement('table');
    32     var table = document.createElement('table');
    31     table.setAttribute('cellspacing', '1');
    33     table.setAttribute('cellspacing', '1');
    32     table.setAttribute('cellpadding', '4');
    34     table.setAttribute('cellpadding', '4');
    46       var th_head_string = 'acpur_th_create_rank';
    48       var th_head_string = 'acpur_th_create_rank';
    47       var th_head_data = { };
    49       var th_head_data = { };
    48     }
    50     }
    49     th_head.appendChild(document.createTextNode($lang.get(th_head_string, th_head_data)));
    51     th_head.appendChild(document.createTextNode($lang.get(th_head_string, th_head_data)));
    50     tr_head.appendChild(th_head);
    52     tr_head.appendChild(th_head);
       
    53     this.th_head = th_head;
    51     table.appendChild(tr_head);
    54     table.appendChild(tr_head);
    52     
    55     
    53     // row: rank title
    56     // row: rank title
    54     var tr_title = document.createElement('tr');
    57     var tr_title = document.createElement('tr');
    55     var td_title_l = document.createElement('td');
    58     var td_title_l = document.createElement('td');
    62     // field: rank title
    65     // field: rank title
    63     var f_rank_title = document.createElement('input');
    66     var f_rank_title = document.createElement('input');
    64     f_rank_title.type = 'text';
    67     f_rank_title.type = 'text';
    65     f_rank_title.size = '30';
    68     f_rank_title.size = '30';
    66     f_rank_title.value = ( this.editing ) ? this.rankdata.rank_title : '';
    69     f_rank_title.value = ( this.editing ) ? this.rankdata.rank_title : '';
       
    70     f_rank_title.editor = this;
       
    71     f_rank_title.onkeyup = function()
       
    72     {
       
    73       this.editor.renderPreview();
       
    74     }
    67     this.f_rank_title = f_rank_title;
    75     this.f_rank_title = f_rank_title;
    68     td_title_f.appendChild(f_rank_title);
    76     td_title_f.appendChild(f_rank_title);
    69     
    77     
    70     tr_title.appendChild(td_title_l);
    78     tr_title.appendChild(td_title_l);
    71     tr_title.appendChild(td_title_f);
    79     tr_title.appendChild(td_title_f);
    88     f_basic_bold.checked = ( this.style_sim_obj.style.fontWeight == 'bold' ) ? true : false;
    96     f_basic_bold.checked = ( this.style_sim_obj.style.fontWeight == 'bold' ) ? true : false;
    89     f_basic_bold.editor = this;
    97     f_basic_bold.editor = this;
    90     f_basic_bold.onclick = function()
    98     f_basic_bold.onclick = function()
    91     {
    99     {
    92       this.editor.style_sim_obj.style.fontWeight = ( this.checked ) ? 'bold' : null;
   100       this.editor.style_sim_obj.style.fontWeight = ( this.checked ) ? 'bold' : null;
       
   101       this.editor.renderPreview();
    93     }
   102     }
    94     l_basic_bold.style.fontWeight = 'bold';
   103     l_basic_bold.style.fontWeight = 'bold';
    95     l_basic_bold.appendChild(f_basic_bold);
   104     l_basic_bold.appendChild(f_basic_bold);
    96     l_basic_bold.appendChild(document.createTextNode(' '));
   105     l_basic_bold.appendChild(document.createTextNode(' '));
    97     l_basic_bold.appendChild(document.createTextNode($lang.get('acpur_field_style_basic_bold')));
   106     l_basic_bold.appendChild(document.createTextNode($lang.get('acpur_field_style_basic_bold')));
   103     f_basic_italic.checked = ( this.style_sim_obj.style.fontStyle == 'italic' ) ? true : false;
   112     f_basic_italic.checked = ( this.style_sim_obj.style.fontStyle == 'italic' ) ? true : false;
   104     f_basic_italic.editor = this;
   113     f_basic_italic.editor = this;
   105     f_basic_italic.onclick = function()
   114     f_basic_italic.onclick = function()
   106     {
   115     {
   107       this.editor.style_sim_obj.style.fontStyle = ( this.checked ) ? 'italic' : null;
   116       this.editor.style_sim_obj.style.fontStyle = ( this.checked ) ? 'italic' : null;
       
   117       this.editor.renderPreview();
   108     }
   118     }
   109     l_basic_italic.style.fontStyle = 'italic';
   119     l_basic_italic.style.fontStyle = 'italic';
   110     l_basic_italic.appendChild(f_basic_italic);
   120     l_basic_italic.appendChild(f_basic_italic);
   111     l_basic_italic.appendChild(document.createTextNode(' '));
   121     l_basic_italic.appendChild(document.createTextNode(' '));
   112     l_basic_italic.appendChild(document.createTextNode($lang.get('acpur_field_style_basic_italic')));
   122     l_basic_italic.appendChild(document.createTextNode($lang.get('acpur_field_style_basic_italic')));
   118     f_basic_underline.checked = ( this.style_sim_obj.style.textDecoration == 'underline' ) ? true : false;
   128     f_basic_underline.checked = ( this.style_sim_obj.style.textDecoration == 'underline' ) ? true : false;
   119     f_basic_underline.editor = this;
   129     f_basic_underline.editor = this;
   120     f_basic_underline.onclick = function()
   130     f_basic_underline.onclick = function()
   121     {
   131     {
   122       this.editor.style_sim_obj.style.textDecoration = ( this.checked ) ? 'underline' : null;
   132       this.editor.style_sim_obj.style.textDecoration = ( this.checked ) ? 'underline' : null;
       
   133       this.editor.renderPreview();
   123     }
   134     }
   124     l_basic_underline.style.textDecoration = 'underline';
   135     l_basic_underline.style.textDecoration = 'underline';
   125     l_basic_underline.appendChild(f_basic_underline);
   136     l_basic_underline.appendChild(f_basic_underline);
   126     l_basic_underline.appendChild(document.createTextNode(' '));
   137     l_basic_underline.appendChild(document.createTextNode(' '));
   127     l_basic_underline.appendChild(document.createTextNode($lang.get('acpur_field_style_basic_underline')));
   138     l_basic_underline.appendChild(document.createTextNode($lang.get('acpur_field_style_basic_underline')));
   150     var f_rank_color = document.createElement('input');
   161     var f_rank_color = document.createElement('input');
   151     f_rank_color.type = 'text';
   162     f_rank_color.type = 'text';
   152     f_rank_color.size = '7';
   163     f_rank_color.size = '7';
   153     f_rank_color.value = ( this.editing ) ? this.rgb2hex(this.style_sim_obj.style.color) : '';
   164     f_rank_color.value = ( this.editing ) ? this.rgb2hex(this.style_sim_obj.style.color) : '';
   154     f_rank_color.style.backgroundColor = this.style_sim_obj.style.color;
   165     f_rank_color.style.backgroundColor = this.style_sim_obj.style.color;
       
   166     f_rank_color.editor = this;
   155     this.f_rank_color = f_rank_color;
   167     this.f_rank_color = f_rank_color;
   156     f_rank_color.onkeyup = function(e)
   168     f_rank_color.onkeyup = function(e)
   157     {
   169     {
   158       if ( !e.keyCode )
   170       if ( !e.keyCode )
   159         e = window.event;
   171         e = window.event;
   166         this.value = this.value.substr(0, 6);
   178         this.value = this.value.substr(0, 6);
   167       }
   179       }
   168       if ( this.value.length == 6 || this.value.length == 3 )
   180       if ( this.value.length == 6 || this.value.length == 3 )
   169       {
   181       {
   170         this.style.backgroundColor = '#' + this.value;
   182         this.style.backgroundColor = '#' + this.value;
       
   183         this.editor.style_sim_obj.style.color = '#' + this.value;
       
   184         this.style.color = '#' + this.editor.determineLightness(this.value);
       
   185         this.editor.renderPreview();
       
   186       }
       
   187       else if ( this.value.length == 0 )
       
   188       {
       
   189         this.style.backgroundColor = null;
       
   190         this.editor.style_sim_obj.style.color = null;
       
   191         this.editor.renderPreview();
   171       }
   192       }
   172     }
   193     }
   173     td_color_f.appendChild(f_rank_color);
   194     td_color_f.appendChild(f_rank_color);
   174     
   195     
   175     tr_color.appendChild(td_color_l);
   196     tr_color.appendChild(td_color_l);
   176     tr_color.appendChild(td_color_f);
   197     tr_color.appendChild(td_color_f);
   177     table.appendChild(tr_color);
   198     table.appendChild(tr_color);
   178     
   199     
       
   200     // "field": preview
       
   201     var tr_preview = document.createElement('tr');
       
   202     var td_preview_l = document.createElement('td');
       
   203     td_preview_l.className = 'row2';
       
   204     td_preview_l.appendChild(document.createTextNode($lang.get('acpur_field_preview')));
       
   205     tr_preview.appendChild(td_preview_l);
       
   206     
       
   207     var td_preview_f = document.createElement('td');
       
   208     td_preview_f.className = 'row2';
       
   209     var div_preview = document.createElement('a');
       
   210     this.preview_div = div_preview;
       
   211     div_preview.style.fontSize = 'x-large';
       
   212     div_preview.appendChild(document.createTextNode(''));
       
   213     div_preview.firstChild.nodeValue = ( this.editing ) ? this.rankdata.rank_title : '';
       
   214     td_preview_f.appendChild(div_preview);
       
   215     tr_preview.appendChild(td_preview_f);
       
   216     
       
   217     table.appendChild(tr_preview);
       
   218     
       
   219     // submit button
       
   220     var tr_submit = document.createElement('tr');
       
   221     var th_submit = document.createElement('th');
       
   222     th_submit.className = 'subhead';
       
   223     th_submit.setAttribute('colspan', '2');
       
   224     var btn_submit = document.createElement('input');
       
   225     btn_submit.type = 'submit';
       
   226     btn_submit.value = ( this.editing ) ? $lang.get('acpur_btn_save') : $lang.get('acpur_btn_create_submit');
       
   227     btn_submit.editor = this;
       
   228     btn_submit.style.fontWeight = 'bold';
       
   229     btn_submit.onclick = function(e)
       
   230     {
       
   231       this.editor.submitEvent(e);
       
   232     }
       
   233     this.btn_submit = btn_submit;
       
   234     th_submit.appendChild(btn_submit);
       
   235     
       
   236     // delete button
       
   237     if ( this.editing )
       
   238     {
       
   239       var btn_delete = document.createElement('input');
       
   240       btn_delete.type = 'button';
       
   241       btn_delete.value = $lang.get('acpur_btn_delete');
       
   242       btn_delete.editor = this;
       
   243       btn_delete.onclick = function(e)
       
   244       {
       
   245         this.editor.deleteEvent(e);
       
   246       }
       
   247       th_submit.appendChild(document.createTextNode(' '));
       
   248       th_submit.appendChild(btn_delete);
       
   249     }
       
   250     
       
   251     tr_submit.appendChild(th_submit);
       
   252     
       
   253     table.appendChild(tr_submit);
       
   254     
       
   255     // render preview
       
   256     this.renderPreview();
       
   257     
   179     // finalize the editor table
   258     // finalize the editor table
   180     editor.appendChild(table);
   259     editor.appendChild(table);
   181     
   260     
   182     // stash rendered editor
   261     // stash rendered editor
   183     this.editordiv = editor;
   262     this.editordiv = editor;
   184     
   263     
   185     // send output
   264     // send output
   186     return editor;
   265     return editor;
   187   }
   266   }
   188   
   267   
       
   268   /**
       
   269    * Takes the existing editor div and transforms the necessary elements so that it goes from "create" mode to "edit" mode
       
   270    * @param object Edit data - same format as the rankdata parameter to the constructor, but we should only need rank_id
       
   271    */
       
   272   
       
   273   this.transformToEditor = function(rankdata)
       
   274   {
       
   275     // we need a rank ID
       
   276     if ( typeof(rankdata.rank_id) != 'number' )
       
   277       return false;
       
   278     
       
   279     if ( this.editing )
       
   280       return false;
       
   281     
       
   282     this.editing = true;
       
   283     
       
   284     this.rankdata = rankdata;
       
   285     this.rankdata.rank_title = this.f_rank_title.value;
       
   286     this.rankdata.rank_style = this.getCSS();
       
   287     
       
   288     // transform various controls
       
   289     this.th_head.firstChild.nodeValue = $lang.get('acpur_th_edit_rank', {
       
   290         rank_title: $lang.get(this.rankdata.rank_title)
       
   291       });
       
   292     this.btn_submit.value = $lang.get('acpur_btn_save');
       
   293     
       
   294     // add the delete button
       
   295     var th_submit = this.btn_submit.parentNode;
       
   296     
       
   297     var btn_delete = document.createElement('input');
       
   298     btn_delete.type = 'button';
       
   299     btn_delete.value = $lang.get('acpur_btn_delete');
       
   300     btn_delete.editor = this;
       
   301     btn_delete.onclick = function(e)
       
   302     {
       
   303       this.editor.deleteEvent(e);
       
   304     }
       
   305     th_submit.appendChild(document.createTextNode(' '));
       
   306     th_submit.appendChild(btn_delete);
       
   307     
       
   308     return true;
       
   309   }
       
   310   
       
   311   /**
       
   312    * Takes a hex color, averages the three channels, and returns either 'ffffff' or '000000' depending on the luminosity of the color.
       
   313    * @param string
       
   314    * @return string
       
   315    */
       
   316   
       
   317   this.determineLightness = function(hexval)
       
   318   {
       
   319     var rgb = this.hex2rgb(hexval);
       
   320     var lumin = ( rgb[0] + rgb[1] + rgb[2] ) / 3;
       
   321     return ( lumin > 60 ) ? '000000' : 'ffffff';
       
   322   }
       
   323   
   189   this.getJSONDataset = function()
   324   this.getJSONDataset = function()
   190   {
   325   {
   191     
   326     
   192   }
   327   }
   193   
   328   
   194   this.getCSS = function()
   329   this.getCSS = function()
   195   {
   330   {
   196     
   331     return this.style_sim_obj.getAttribute('style');
       
   332   }
       
   333   
       
   334   this.renderPreview = function()
       
   335   {
       
   336     if ( !this.preview_div )
       
   337       return false;
       
   338     var color = ( this.style_sim_obj.style.color ) ? '#' + this.rgb2hex(this.style_sim_obj.style.color) : null;
       
   339     this.preview_div.style.color = color;
       
   340     this.preview_div.style.fontWeight = this.style_sim_obj.style.fontWeight;
       
   341     this.preview_div.style.fontStyle = this.style_sim_obj.style.fontStyle;
       
   342     this.preview_div.style.textDecoration = this.style_sim_obj.style.textDecoration;
       
   343     this.preview_div.firstChild.nodeValue = $lang.get(this.f_rank_title.value);
       
   344   }
       
   345   
       
   346   this.submitEvent = function(e)
       
   347   {
       
   348     if ( this.onsubmit )
       
   349     {
       
   350       this.onsubmit(e);
       
   351     }
       
   352     else
       
   353     {
       
   354       window.console.error('RankEditorControl: no onsubmit event specified');
       
   355     }
       
   356   }
       
   357   
       
   358   this.deleteEvent = function(e)
       
   359   {
       
   360     if ( this.ondelete )
       
   361     {
       
   362       this.ondelete(e);
       
   363     }
       
   364     else
       
   365     {
       
   366       window.console.error('RankEditorControl: no ondelete event specified');
       
   367     }
   197   }
   368   }
   198   
   369   
   199   /**
   370   /**
   200    * Converts a parenthetical color specification (rgb(x, y, z)) to hex form (xxyyzz)
   371    * Converts a parenthetical color specification (rgb(x, y, z)) to hex form (xxyyzz)
   201    * @param string
   372    * @param string
   216     if ( b.length < 2 )
   387     if ( b.length < 2 )
   217       b = '0' + b;
   388       b = '0' + b;
   218     
   389     
   219     return r + g + b;
   390     return r + g + b;
   220   }
   391   }
       
   392   
       
   393   /**
       
   394    * Get red, green, and blue values for the given hex color
       
   395    * @param string
       
   396    * @return array (numbered, e.g. not an object
       
   397    */
       
   398   
       
   399   this.hex2rgb = function(hex)
       
   400   {
       
   401     hex = hex.replace(/^#/, '');
       
   402     if ( hex.length != 3 && hex.length != 6 )
       
   403     {
       
   404       return hex;
       
   405     }
       
   406     if ( hex.length == 3 )
       
   407     {
       
   408       // is there a better way to do this?
       
   409       hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2);
       
   410     }
       
   411     hex = [ hex.substr(0, 2), hex.substr(2, 2), hex.substr(4, 2) ];
       
   412     var red = parseInt(hex[0], 16);
       
   413     var green = parseInt(hex[1], 16);
       
   414     var blue = parseInt(hex[2], 16);
       
   415     return [red, green, blue];
       
   416   }
   221 }
   417 }
   222 
   418 
   223 /**
   419 /**
   224  * Perform request for editable rank data and draw editor
   420  * Perform request for editable rank data and draw editor
   225  */
   421  */
   226 
   422 
   227 function ajaxInitRankEdit(rank_id)
   423 function ajaxInitRankEdit(rank_id)
   228 {
   424 {
   229   var editor = new RankEditorControl({ rank_title: 'Foo', rank_id: rank_id, rank_style: 'color: #ff0000; font-weight: bold;' });
   425   load_component('messagebox');
   230   var ren
   426   var json_packet = {
       
   427     mode: 'get_rank',
       
   428     rank_id: rank_id
       
   429   };
       
   430   json_packet = ajaxEscape(toJSONString(json_packet));
       
   431   ajaxPost(makeUrlNS('Admin', 'UserRanks/action.json'), 'r=' + json_packet, function()
       
   432     {
       
   433       if ( ajax.readyState == 4 && ajax.status == 200 )
       
   434       {
       
   435         var response = String(ajax.responseText + '');
       
   436         if ( response.substr(0, 1) != '{' )
       
   437         {
       
   438           handle_invalid_json(ajax.responseText);
       
   439           return false;
       
   440         }
       
   441         try
       
   442         {
       
   443           var response = parseJSON(ajax.responseText);
       
   444         }
       
   445         catch(e)
       
   446         {
       
   447           handle_invalid_json(ajax.responseText);
       
   448         }
       
   449         var editor = new RankEditorControl(response);
       
   450         editor.onsubmit = ajaxRankEditHandleSaveExisting;
       
   451         editor.ondelete = ajaxRankEditHandleDelete;
       
   452         var container = document.getElementById('admin_ranks_container_right');
       
   453         container.innerHTML = '';
       
   454         container.appendChild(editor.render());
       
   455       }
       
   456     }, true);
       
   457 }
       
   458 
       
   459 function ajaxInitRankCreate()
       
   460 {
       
   461   load_component('messagebox');
       
   462   var editor = new RankEditorControl();
       
   463   editor.onsubmit = ajaxRankEditHandleSaveNew;
   231   var container = document.getElementById('admin_ranks_container_right');
   464   var container = document.getElementById('admin_ranks_container_right');
   232   container.innerHTML = '';
   465   container.innerHTML = '';
   233   container.appendChild(editor.render());
   466   container.appendChild(editor.render());
   234 }
   467 }
       
   468 
       
   469 function ajaxRankEditHandleSave(editor, switch_new)
       
   470 {
       
   471   var whitey = whiteOutElement(editor.wrapperdiv);
       
   472   
       
   473   // pack it up, ...
       
   474   var json_packet = {
       
   475     mode: ( switch_new ) ? 'create_rank' : 'save_rank',
       
   476     rank_title: editor.f_rank_title.value,
       
   477     rank_style: editor.getCSS()
       
   478   }
       
   479   if ( !switch_new )
       
   480   {
       
   481     json_packet.rank_id = editor.rankdata.rank_id;
       
   482   }
       
   483   /// ... pack it in
       
   484   var json_packet = ajaxEscape(toJSONString(json_packet));
       
   485   
       
   486   ajaxPost(makeUrlNS('Admin', 'UserRanks/action.json'), 'r=' + json_packet, function()
       
   487     {
       
   488       if ( ajax.readyState == 4 && ajax.status == 200 )
       
   489       {
       
   490         var response = String(ajax.responseText + '');
       
   491         if ( response.substr(0, 1) != '{' )
       
   492         {
       
   493           handle_invalid_json(ajax.responseText);
       
   494           return false;
       
   495         }
       
   496         try
       
   497         {
       
   498           var response = parseJSON(ajax.responseText);
       
   499         }
       
   500         catch(e)
       
   501         {
       
   502           handle_invalid_json(ajax.responseText);
       
   503         }
       
   504         if ( response.mode == 'success' )
       
   505         {
       
   506           whiteOutReportSuccess(whitey);
       
   507           if ( switch_new )
       
   508           {
       
   509             //
       
   510             // we have a few more things to do with a newly created rank.
       
   511             //
       
   512             
       
   513             // 1. transform editor
       
   514             editor.transformToEditor(response);
       
   515             editor.onsubmit = ajaxRankEditHandleSaveExisting;
       
   516             editor.ondelete = ajaxRankEditHandleDelete;
       
   517             
       
   518             // 2. append the new rank to the list
       
   519             var create_link = document.getElementById('rankadmin_createlink');
       
   520             if ( create_link )
       
   521             {
       
   522               var parent = create_link.parentNode;
       
   523               var edit_link = document.createElement('a');
       
   524               edit_link.href = '#rank_edit:' + response.rank_id;
       
   525               edit_link.className = 'rankadmin-editlink';
       
   526               edit_link.setAttribute('style', editor.getCSS());
       
   527               edit_link.id = 'rankadmin_editlink_' + response.rank_id;
       
   528               edit_link.rank_id = response.rank_id;
       
   529               edit_link.appendChild(document.createTextNode($lang.get(editor.f_rank_title.value)));
       
   530               parent.insertBefore(edit_link, create_link);
       
   531               edit_link.onclick = function()
       
   532               {
       
   533                 ajaxInitRankEdit(this.rank_id);
       
   534               }
       
   535             }
       
   536           }
       
   537           else
       
   538           {
       
   539             // update the rank title on the left
       
   540             var edit_link = document.getElementById('rankadmin_editlink_' + editor.rankdata.rank_id);
       
   541             if ( edit_link )
       
   542             {
       
   543               edit_link.firstChild.nodeValue = $lang.get(editor.f_rank_title.value);
       
   544               edit_link.setAttribute('style', editor.getCSS());
       
   545             }
       
   546           }
       
   547         }
       
   548         else
       
   549         {
       
   550           whitey.parentNode.removeChild(whitey);
       
   551           miniPromptMessage({
       
   552               title: $lang.get('acpur_err_save_failed_title'),
       
   553               message: response.error,
       
   554               buttons: [
       
   555                 {
       
   556                   text: $lang.get('etc_ok'),
       
   557                   color: 'red',
       
   558                   style: {
       
   559                     fontWeight: 'bold'
       
   560                   },
       
   561                   onclick: function()
       
   562                   {
       
   563                     miniPromptDestroy(this);
       
   564                   }
       
   565                 }
       
   566               ]
       
   567           });
       
   568         }
       
   569       }
       
   570     }, true);
       
   571 }
       
   572 
       
   573 var ajaxRankEditHandleSaveExisting = function()
       
   574 {
       
   575   ajaxRankEditHandleSave(this, false);
       
   576 }
       
   577 
       
   578 var ajaxRankEditHandleSaveNew = function()
       
   579 {
       
   580   ajaxRankEditHandleSave(this, true);
       
   581 }
       
   582 
       
   583 var ajaxRankEditHandleDelete = function()
       
   584 {
       
   585   var mp = miniPromptMessage({
       
   586       title: $lang.get('acpur_msg_rank_delete_confirm_title'),
       
   587       message: $lang.get('acpur_msg_rank_delete_confirm_body'),
       
   588       buttons: [
       
   589         {
       
   590           text: $lang.get('acpur_btn_delete'),
       
   591           color: 'red',
       
   592           style: {
       
   593             fontWeight: 'bold'
       
   594           },
       
   595           onclick: function()
       
   596           {
       
   597             var parent = miniPromptGetParent(this);
       
   598             var editor = parent.editor;
       
   599             setTimeout(function()
       
   600               {
       
   601                 ajaxRankEditDeleteConfirmed(editor);
       
   602               }, 1000);
       
   603             miniPromptDestroy(parent);
       
   604           }
       
   605         },
       
   606         {
       
   607           text: $lang.get('etc_cancel'),
       
   608           onclick: function()
       
   609           {
       
   610             miniPromptDestroy(this);
       
   611           }
       
   612         }
       
   613       ]
       
   614     });
       
   615   console.debug(mp);
       
   616   mp.editor = this;
       
   617 }
       
   618 
       
   619 function ajaxRankEditDeleteConfirmed(editor)
       
   620 {
       
   621   var whitey = whiteOutElement(editor.wrapperdiv);
       
   622   
       
   623   load_component('SpryEffects');
       
   624   
       
   625   var json_packet = {
       
   626     mode: 'delete_rank',
       
   627     rank_id: editor.rankdata.rank_id
       
   628   };
       
   629   var rank_id = editor.rankdata.rank_id;
       
   630   
       
   631   json_packet = ajaxEscape(toJSONString(json_packet));
       
   632   ajaxPost(makeUrlNS('Admin', 'UserRanks/action.json'), 'r=' + json_packet, function()
       
   633     {
       
   634       if ( ajax.readyState == 4 && ajax.status == 200 )
       
   635       {
       
   636         var response = String(ajax.responseText + '');
       
   637         if ( response.substr(0, 1) != '{' )
       
   638         {
       
   639           handle_invalid_json(ajax.responseText);
       
   640           return false;
       
   641         }
       
   642         try
       
   643         {
       
   644           var response = parseJSON(ajax.responseText);
       
   645         }
       
   646         catch(e)
       
   647         {
       
   648           handle_invalid_json(ajax.responseText);
       
   649         }
       
   650         if ( response.mode == 'success' )
       
   651         {
       
   652           // the deletion was successful, report success and kill off the editor
       
   653           whiteOutReportSuccess(whitey);
       
   654           setTimeout(function()
       
   655             {
       
   656               // nuke the rank title on the left
       
   657               var edit_link = document.getElementById('rankadmin_editlink_' + editor.rankdata.rank_id);
       
   658               if ( edit_link )
       
   659               {
       
   660                 edit_link.parentNode.removeChild(edit_link);
       
   661               }
       
   662               // collapse and destroy the editor
       
   663               new Spry.Effect.Blind(editor.wrapperdiv, { duration: 500, finish: function()
       
   664                   {
       
   665                     // when the animation finishes, nuke the whole thing
       
   666                     var container = document.getElementById('admin_ranks_container_right');
       
   667                     container.innerHTML = $lang.get('acpur_msg_select_rank');
       
   668                   }
       
   669                 }).start();
       
   670             }, 1500);
       
   671         }
       
   672         else
       
   673         {
       
   674           whitey.parentNode.removeChild(whitey);
       
   675           miniPromptMessage({
       
   676               title: $lang.get('acpur_err_delete_failed_title'),
       
   677               message: response.error,
       
   678               buttons: [
       
   679                 {
       
   680                   text: $lang.get('etc_ok'),
       
   681                   color: 'red',
       
   682                   style: {
       
   683                     fontWeight: 'bold'
       
   684                   },
       
   685                   onclick: function()
       
   686                   {
       
   687                     miniPromptDestroy(this);
       
   688                   }
       
   689                 }
       
   690               ]
       
   691           });
       
   692         }
       
   693       }
       
   694     }, true);
       
   695 }