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 } |