Jump to content

User:Magnus Manske/less edit clutter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Magnus Manske (talk | contribs) at 22:36, 10 March 2008. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if ( ( wgAction == "submit" || wgAction == "edit" ) && wgNamespaceNumber == 0 ) {
  addOnloadHook ( less_edit_clutter_init ) ;
}

var lec_parent ;
var lec_tb1 ;
var lec_original ;
var lec_tb_top ;
var lec_tb_ill ;
var lec_tb_cat ;
var lec_tb_bt ;
var lec_active ;


function less_edit_clutter_init () {
  lec_active = 1 ;
  less_edit_clutter_create_elements () ;
  less_edit_clutter_hook_button ( 'wpSave' ) ;
  less_edit_clutter_hook_button ( 'wpPreview' ) ;
  less_edit_clutter_hook_button ( 'wpDiff' ) ;
  less_edit_clutter_parse_main_textbox () ;
}

function less_edit_clutter_hook_button ( id ) {
  var b = document.getElementById ( id ) ;
  b.onclick = function () { less_edit_clutter_rejoin() ; return true ; } ;
}

function less_edit_clutter_rejoin () {
  if ( lec_active != 1 ) return ;
  v = lec_tb_top.value ;
  v = v.replace(/^\s+|\s+$/g,"") ;
  if ( v != '' ) v += "\n" ;
  v += lec_tb1.value ;
  v = v.replace(/^\s+|\s+$/g,"") ;
  if ( v != '' ) v += "\n\n" ;
  v += lec_tb_bt.value ;
  v = v.replace(/^\s+|\s+$/g,"") ;
  if ( v != '' ) v += "\n\n" ;

  // Categories
  n = lec_tb_cat.value.split ( "\n" ) ;
  n = n.sort() ;
  for ( i = 0 ; i < n.length ; i++ ) {
    s = n[i].replace(/^\s+|\s+$/g,"") ;
    if ( s == '' ) continue ;
    v += '[[Category:' + s + "]]\n" ;
  }
  v = v.replace(/^\s+|\s+$/g,"") ;
  if ( v != '' ) v += "\n\n" ;

  // Interlanguage links
  n = lec_tb_ill.value.split ( "\n" ) ;
  n = n.sort() ;
  for ( i = 0 ; i < n.length ; i++ ) {
    s = n[i].replace(/^\s+|\s+$/g,"") ;
    if ( s == '' ) continue ;
    v += '[[' + s + "]]\n" ;
  }
  v = v.replace(/^\s+|\s+$/g,"") ;

  lec_tb1.value = v ;
}

function less_edit_clutter_create_elements () {
  lec_tb1 = document.getElementById ( 'wpTextbox1' ) ;
  lec_parent = lec_tb1.parentNode ;

  lec_tb_top = document.createElement ( 'textarea' ) ;
  lec_tb_top.id = 'lec_tb_top' ;
  lec_tb_top.name = 'lec_tb_top' ;
  lec_tb_top.cols = 80 ;
  lec_tb_top.rows = 5 ;
  
  lec_tb_ill = document.createElement ( 'textarea' ) ;
  lec_tb_ill.id = 'lec_tb_ill' ;
  lec_tb_ill.name = 'lec_tb_ill' ;
  lec_tb_ill.cols = 30 ;
  lec_tb_ill.rows = 5 ;

  lec_tb_cat = document.createElement ( 'textarea' ) ;
  lec_tb_cat.id = 'lec_tb_cat' ;
  lec_tb_cat.name = 'lec_tb_cat' ;
  lec_tb_cat.cols = 30 ;
  lec_tb_cat.rows = 5 ;

  lec_tb_bt = document.createElement ( 'textarea' ) ;
  lec_tb_bt.id = 'lec_tb_bt' ;
  lec_tb_bt.name = 'lec_tb_bt' ;
  lec_tb_bt.cols = 30 ;
  lec_tb_bt.rows = 5 ;

  lec_bottom_table = document.createElement ( 'table' ) ;
  lec_bottom_tbody = document.createElement ( 'tbody' ) ;
  lec_bottom_tr = document.createElement ( 'tr' ) ;
  lec_bottom_td1 = document.createElement ( 'td' ) ;
  lec_bottom_td2 = document.createElement ( 'td' ) ;
  lec_bottom_td3 = document.createElement ( 'td' ) ;

  lec_bottom_td1.style.width = '40%' ;
  lec_bottom_td2.style.width = '30%' ;
  lec_bottom_td3.style.width = '30%' ;

  lec_bottom_td1.appendChild ( document.createTextNode ( 'End templates and magic words' ) ) ;
  lec_bottom_td1.appendChild ( document.createElement ( 'br' ) ) ;
  lec_bottom_td1.appendChild ( lec_tb_bt ) ;

  lec_bottom_td2.appendChild ( document.createTextNode ( 'Categories' ) ) ;
  lec_bottom_td2.appendChild ( document.createElement ( 'br' ) ) ;
  lec_bottom_td2.appendChild ( lec_tb_cat ) ;

  lec_bottom_td3.appendChild ( document.createTextNode ( 'Interlanguage links' ) ) ;
  lec_bottom_td3.appendChild ( document.createElement ( 'br' ) ) ;
  lec_bottom_td3.appendChild ( lec_tb_ill ) ;

  lec_bottom_tr.appendChild ( lec_bottom_td1 ) ;
  lec_bottom_tr.appendChild ( lec_bottom_td2 ) ;
  lec_bottom_tr.appendChild ( lec_bottom_td3 ) ;
  lec_bottom_tbody.appendChild ( lec_bottom_tr ) ;
  lec_bottom_table.appendChild ( lec_bottom_tbody ) ;

  var a = document.createElement ( 'a' ) ;
  a.href = '#' ;
  a.appendChild ( document.createTextNode ( "reset to standard edit box" ) ) ;
  a.onclick = function () {
    u = document.getElementById('lec_div_top') ;
    u.parentNode.removeChild ( u ) ;
    u = document.getElementById('lec_div_bottom') ;
    u.parentNode.removeChild ( u ) ;
    lec_tb1.value = lec_original ;
    lec_active = 0 ;
  }

  var top = document.createElement ( 'div' ) ;
  top.id = 'lec_div_top' ;

  top.appendChild ( document.createTextNode ( "Header templates and images" ) ) ;
  top.appendChild ( lec_tb_top ) ;
  top.appendChild ( document.createTextNode ( "Main text (" ) ) ;
  top.appendChild ( a ) ;
  top.appendChild ( document.createTextNode ( ")" ) ) ;

  var bottom = document.createElement ( 'div' ) ;
  bottom.id = 'lec_div_bottom' ;

  bottom.appendChild ( document.createTextNode ( 'Elements at the end' ) ) ;
  bottom.appendChild ( lec_bottom_table ) ;

  lec_parent.insertBefore ( bottom , lec_tb1.nextSibling ) ;
  lec_parent.insertBefore ( top , lec_tb1 ) ;

  lec_bottom_table.id = 'lec_tb_bottom' ;
  lec_bottom_table.style.width = '100%' ;
  lec_bottom_table.style.cellSpacing = '2' ;
  lec_bottom_table.style.cellPadding = '2' ;


  less_edit_clutter_append_show_hide ( 'lec_tb_top' ) ;
  less_edit_clutter_append_show_hide ( 'lec_tb_bottom' ) ;
}

function less_edit_clutter_toggle_show_hide ( id , a ) {
  var x = document.getElementById ( id ) ;
  if ( x.style.display == 'none' ) {
    x.style.display = '' ;
    while ( a.firstChild ) a.removeChild ( a.firstChild ) ;
    a.appendChild (  document.createTextNode ( 'hide' ) ) ;
  } else {
    x.style.display = 'none' ;
    while ( a.firstChild ) a.removeChild ( a.firstChild ) ;
    a.appendChild ( document.createTextNode ( 'show' ) ) ;
  }
}

function less_edit_clutter_append_show_hide ( id ) {
  var x = document.getElementById ( id ) ;
  var n = document.createElement ( 'span' ) ;

  var a = document.createElement ( 'a' ) ;
  a.href = '#' ;
  a.onclick = function () { less_edit_clutter_toggle_show_hide ( id , this ) ; return false ; }
  a.appendChild ( document.createTextNode ( 'hide' ) ) ;

  n.appendChild ( document.createTextNode ( ' (' ) ) ;
  n.appendChild ( a ) ;
  n.appendChild ( document.createTextNode ( ')' ) ) ;
  n.appendChild ( document.createElement ( 'br' ) ) ;

  x.parentNode.insertBefore ( n , x ) ;
//  x.style.display = 'none' ;
}

function less_edit_clutter_parse_main_textbox () {
  lec_original = lec_tb1.value ;
  var lines = lec_original.split("\n") ;
  var in_template = 0 ;
  var begin_templates = "" ;

  // Begin of text
  while ( lines.length > 0 ) {
    var no_ws = lines[0].replace(/^\s+|\s+$/g,"") ;
    var lc = no_ws.toLowerCase() ;
    if ( lc == "" ) {
      lines.shift() ;
      if ( begin_templates != '' ) begin_templates += "\n" ;
    } else if ( lc.match(/^\{\{[^\{\}]*\}\}$/) ) {
      lines.shift() ;
      begin_templates = begin_templates + no_ws + "\n" ;
    } else if ( lc.match(/^\{\{[^\{\}]*$/) ) {
      begin_templates = begin_templates + lines.shift() + "\n" ;
      in_template++ ;
    } else if ( in_template && lc.match(/^[^\}\{]*\}\}\s*$/) ) {
      begin_templates = begin_templates + lines.shift() + "\n" ;
      in_template-- ;
    } else if ( in_template > 0 ) {
      begin_templates = begin_templates + lines.shift() + "\n" ;
    } else if ( lc.match(/^[-_]+$/) || lc.match(/^\s*\<hr\s*\/*\>\s*$/) ) {
      begin_templates = begin_templates + lines.shift() + "\n" ;
    } else if ( lc.match(/^\[\[image\:.*\]\]$/) ) {
      begin_templates = begin_templates + lines.shift() + "\n" ;
    } else break ;
  }

  begin_templates = begin_templates.replace(/^\s+|\s+$/g,"") ;


  var categories = new Array () ;
  var languages = new Array () ;
  var end_templates = "" ;
  
  // End of text
  in_template = 0 ;
  end_templates_append = "" ;
  while ( lines.length > 0 ) {
    var lp = lines.length - 1 ;
    var no_ws = lines[lp].replace(/^\s+|\s+$/g,"") ;
    var lc = no_ws.toLowerCase() ;
    if ( lc == "" ) {
      lines.pop() ;
      if ( end_templates != '' ) end_templates = "\n" + end_templates ;
    } else if ( lc.substr ( 0 , 11 ) == "[[category:" ) {
      lines.pop() ;
      var s = no_ws.substr ( 11 ) ;
      s = s.replace ( /\]\]$/ , "" ) ;
      categories.push ( s ) ;
    } else if ( lc.match(/^\[\[\w+\:.*\]\]$/) ) {
      lines.pop() ;
      var s = no_ws.substr ( 2 ) ;
      s = s.replace ( /\]\]$/ , "" ) ;
      languages.push ( s ) ;
    } else if ( lc.match(/^\{\{defaultsort\s*[\|\:].*\}\}$/) ) {
      end_templates_append += "\n\n" + lines.pop() ;
    } else if ( lc.match(/^\{\{.*\}\}$/) ) {
      end_templates = lines.pop() + "\n" + end_templates ;
    } else if ( lc.match(/^[^\{\}]*\}\}$/) ) {
      end_templates = lines.pop() + "\n" + end_templates ;
      in_template++ ;
    } else if ( lc.match(/^\{\{[^\{\}]*$/) ) {
      end_templates = lines.pop() + "\n" + end_templates ;
      in_template-- ;
    } else if ( in_template > 0 ) {
      end_templates = lines.pop() + "\n" + end_templates ;
    } else break ;
  }

  var oet ;
  do {
    oet = end_templates ;
    end_templates = end_templates.split("\n\n\n").join("\n\n") ;
  } while ( end_templates != oet ) ;
  end_templates = end_templates.replace(/^\s+|\s+$/g,"") ;
  end_templates_append = end_templates_append.replace(/^\s+|\s+$/g,"") ;
  if ( end_templates != '' && end_templates_append != '' ) end_templates += "\n\n" ;
  end_templates += end_templates_append ;

  // Set boxes
  lec_tb_top.value = begin_templates ;
  lec_tb_cat.value = categories.sort().join("\n") ;
  lec_tb_ill.value = languages.sort().join("\n") ;
  lec_tb_bt.value = end_templates ;
  lec_tb1.value = lines.join("\n") ;
}