Benutzer:Schnark/toolbar-basic.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
//deprecated
/**************************************************************************************************
*  "Einfache" Konfiguration der neuen Toolbar, Dokumentation siehe                                *
*             [[Benutzer Diskussion:Schnark/toolbar.js]]                                          *
*  Autor: [[Benutzer:Schnark]] ([http://de.wikipedia.org/wiki/Benutzer:Schnark])                  *
*  aufbauend auf [[commons:User:Krinkle/insertVectorButtons.js]] von [[Benutzer:Krinkle]]         *
*                                                                                                 *
*  "Simple" configuration for the new toolbar, German documentation on                            *
*             [[Benutzer Diskussion:Schnark/toolbar.js]]                                          *
*  author: [[Benutzer:Schnark]] ([http://de.wikipedia.org/wiki/Benutzer:Schnark])                 *
*  using ideas from [[commons:User:Krinkle/insertVectorButtons.js]] by [[Benutzer:Krinkle]]       *
*****************************************************************************************<nowiki>**/

/***************************************
*  Part 1: project independent things  *
***************************************/

/**********************************
*    general global variables     *
**********************************/
if (typeof usersignature == 'undefined') var usersignature = '--~~~~'; //user-configurable signature
if (typeof newtoolbar_debug == 'undefined') newtoolbar_debug = false; //debug?

var newtoolbar_standardbar = null;
if (typeof newtoolbar_load_standard == 'undefined') newtoolbar_load_standard = true;
var wgAction = mw.config.get('wgAction');
if ((newtoolbar_load_standard) && (wgAction == 'edit' || wgAction == 'submit'))  //load standard bar if not turned off by user
   mw.loader.load(((mw.config.get('wgServer') == 'http://localhost') ? 'http://localhost/testwiki/index.php' : 'http://de.wikipedia.org/w/index.php') +
                                                       '?action=raw&ctype=text/css&title=Benutzer:Schnark/toolbar.js/standardbar.js');

/**********************************
*         interal functions       *
**********************************/

//create an id from a given name (probably not necessary) or use default
function newtoolbar_ID (name, default_val) {
  return (name != '') ? name.replace(/ /g, '_').replace(/[^-_a-zA-Z0-9]/g, 'x') : default_val;
}

//put things together to an action, ownline and func are optional
function newtoolbar_action ( preText, sampleText, postText, ownline, func ) {
  var action = {};
  var opt = { pre: preText, peri: sampleText, post: postText };
  if (ownline) opt.ownline = true;
  if (typeof func == 'function') {
     action = {
           type: 'callback',
           execute: function() {
                    $('#wpTextbox1').textSelection('encapsulateSelection', opt);
                    func(); }};

  } else {
    action =  {
          type: 'encapsulate',
          options: opt };
  }
  return action;
}

//call an external function
function newtoolbar_externFunction ( type, code, where, param ) {
  var ret = true;
  var funcs = newtoolbar_externFunctions[type];
  switch (typeof funcs[code]) {
    case 'function': funcs[code](where, param); break;
    case 'string': newtoolbar_externDelayed(type, funcs[code], code, where, param); break;
    default: ret = false;
  }
  return ret;
}

//call an external function which is not yet loaded
function newtoolbar_externDelayed ( type, script, code, where, param ) {
  switch (type) { //if possible insert a "container"
    case 'section': newtoolbar_addMySection(param, true); break; //true is just a guess
    case 'group': newtoolbar_addMyGroup(where, param); break;
    case 'chars': newtoolbar_addMyChars(where, param, []); break;
  }
  newtoolbar_externFunctions[type][code] = [where, param];
  importScript(script);
}

//append CSS for a select
//list = [[item1, icon1], [item2, icon2], ...]
function newtoolbar_CSSSelect ( where, name, list ) {
  if (where.indexOf('/') == -1) where += '/';
  var css1 = '.wikiEditor-ui-toolbar .section[rel=' + newtoolbar_ID(where.split('/')[0], 'main') +
             '] .group[rel=' + newtoolbar_ID(where.split('/')[1], 'insert') +
             '] .tool-select[rel=' + newtoolbar_ID(name) +
             '] .options .option[rel=';
  var css2 = ']:before {content:url(';
  var css3 = ')" "; }';
  var css = '';
  for (var i = 0; i < list.length; i++) {
      css += css1 + newtoolbar_ID(list[i][0]) + css2 + list[i][1] + css3;
  }
  appendCSS(css);
}

//insert a comment
function newtoolbar_addComment( comment ) {
  return function(){ $('#wpSummary').val($('#wpSummary').val()+comment); }
}

/*****************************************************
*                  addToToolbar                      *
*****************************************************/

//add a section
//type: true - toolbar, false - booklet
function newtoolbar_addMySection( name, type ) {
  var id = newtoolbar_ID(name);
  var newSection = {sections: {} };
  newSection.sections[id] = {
              type: type ? 'toolbar' : 'booklet',
              label: name   };
  if (!type) newSection.sections[id].pages = { 'x': {}} //HACK
  $('#wpTextbox1').wikiEditor('addToToolbar', newSection);
  if (!type) newtoolbar_remove(name+'/x', false); //HACK
  if (type) { //insert empty group
     var newGroup = {section : id, groups: {'insert': {}} };
     $('#wpTextbox1').wikiEditor('addToToolbar', newGroup); }
}

//add a group to a toolbar (section with type = true)
function newtoolbar_addMyGroup ( where, name ) {
  var id = newtoolbar_ID (name);
  if (name.charAt(0) == ' ') name = ''; //if name starts with a space don't show the name
  var newGroup = {section : newtoolbar_ID(where, 'main'), groups: {} };
  newGroup.groups[id] = {label: name};
  $('#wpTextbox1').wikiEditor('addToToolbar', newGroup);
}

//add a table with characters to a booklet (section with type = false)
//or into an existing characters table
//rtl is optional
//chars = [z1, z2, ...]
//z_i = 'x' or = ['show', 'insert'] or = ['pre', 'peri', 'post'] or = ['pre', 'peri', 'post', 'tooltip']
function newtoolbar_addMyChars (where, name, chars, rtl) {
  var id = newtoolbar_ID (name);
  var c = [];
  for (var i = 0; i < chars.length; i++) {
      if (typeof chars[i] == 'object' && chars[i].length == 3) {
         c.push({label: chars[i][0] + chars[i][1] + chars[i][2], action: {type: 'encapsulate', options:
                                     { pre: chars[i][0], peri: chars[i][1], post: chars[i][2] }}});
      } else if (typeof chars[i] == 'object' && chars[i].length == 4) {
         var l = chars[i][0] + chars[i][1] + chars[i][2];
         l = l.replace(/&/g, 'ξ').replace(/</g, '‹').replace(/>/g, '›').replace(/"/g, '″'); //&...; works in principal
                                                                                            //but I'm to stupid at the moment
         if (chars[i][3] != '') l = "<span style='padding:0px; margin:0px; border:0px;' title='" + chars[i][3] + "'>" + l + '</span>'; 
         c.push({label: l, action: {type: 'encapsulate',
                           options: { pre: chars[i][0], peri: chars[i][1], post: chars[i][2] }}}); 
      } else {
         c.push(chars[i]);
      }
  }
  var newChars;
  if (where.indexOf('/') == -1) {
     newChars = {section: newtoolbar_ID(where, 'characters'), pages: {} };
     newChars.pages[id] = {label: name, layout: 'characters', characters: c};
     if (rtl) newChars.pages[id].direction = 'rtl';
  } else {
     newChars = {section: newtoolbar_ID(where.split('/')[0], 'characters'), page: newtoolbar_ID(where.split('/')[1], 'symbols'), characters: c };
  }
  $('#wpTextbox1').wikiEditor('addToToolbar', newChars);
}

//add a table to a booklet (section with type = false)
//table = [[head1, head2, ...], [1.1, 1.2, ...], ...]
function newtoolbar_addMyTable (where, name, table) {
  var id = newtoolbar_ID (name);
  var head = [];
  for (var i = 0; i < table[0].length; i++) {
      head.push({text: table[0][i]});
  }
  var rows = [];
  for (var i = 1; i < table.length; i++) {
      var row = {};
      for (var j = 0; j < table[i].length; j++) {
          row['a'+j] = {html: table[i][j]};
      }
      rows.push(row);
  }
  var newTable = {section: newtoolbar_ID(where, 'help'), pages: {} };
  newTable.pages[id] = {label: name, layout: 'table', headings: head, rows: rows};
  $('#wpTextbox1').wikiEditor('addToToolbar', newTable);
}

//add a button to a toolbar (section with type = true)
function newtoolbar_addMyButton( where, icon, tooltip, pre, peri, post, ownline, func ) {
  var id = newtoolbar_ID (tooltip);
  if (where.indexOf('/') == -1) where += '/';
  var newButton = {section: newtoolbar_ID(where.split('/')[0], 'main'),
                   group: newtoolbar_ID(where.split('/')[1], 'insert'), tools: {}};
  newButton.tools[id] = {
             label: tooltip,
             type: 'button',
             icon: icon,
             action: newtoolbar_action(pre, peri, post, ownline, func) }
  $('#wpTextbox1').wikiEditor('addToToolbar', newButton);
}

//add a select to a toolbar (section with type = true)
//list = [[name, pre, peri, post], ...]
// bzw.   [name, pre, peri, post, ownline, funktion]
function newtoolbar_addMySelect ( where, name, list ) {
  var id = newtoolbar_ID (name);
  if (where.indexOf('/') == -1) where += '/';
  var newList = {section: newtoolbar_ID(where.split('/')[0], 'main'), group: newtoolbar_ID(where.split('/')[1], 'insert'), tools: {}};
  var list_ = {};

  for ( var i = 0; i < list.length; i++) {
      var item = list[i];
      list_[newtoolbar_ID(item[0])] = {label: item[0], action: newtoolbar_action(item[1], item[2], item[3], item[4], item[5])};
  }
  newList.tools[id] = {
             label: name,
             type: 'select',
             list: list_ }
  $('#wpTextbox1').wikiEditor('addToToolbar', newList);
}

//add standard tool
function newtoolbar_addInternalTool ( where, section, group, name ) {
  var ret = false;
  if (newtoolbar_standardbar) {
     var tool = newtoolbar_standardbar.toolbar[section].groups[group].tools[name];
     if (tool.filters) tool.filters = []; //remove filters
     if (where.indexOf('/') == -1) where += '/';
     var newtool = {section: newtoolbar_ID(where.split('/')[0], 'main'),
                    group: newtoolbar_ID(where.split('/')[1], 'insert'), tools: {}};
     newtool.tools[name] = tool;
     $('#wpTextbox1').wikiEditor('addToToolbar', newtool);
     ret = true; }
  return ret;
}

/*****************************************************
*               removeFromToolbar                    *
*****************************************************/

//remove something
// type = true for toolbar, = false for booklet
function newtoolbar_remove( thing, type ) {
  var obj = {section: newtoolbar_ID(thing.split('/')[0], type ? 'main' : 'characters')};
  if (thing.indexOf('/') > -1) obj[type ? 'group' : 'page'] = newtoolbar_ID(thing.split('/')[1], 'insert');
  if (type && thing.lastIndexOf('/') != thing.indexOf('/'))
     obj.tool = newtoolbar_ID(thing.split('/')[2], newtoolbar_ID('Toolbar erweitern')); //'Toolbar erweitern' is deprecated
  if (!type && thing.lastIndexOf('/') != thing.indexOf('/')) obj.character = thing.split('/')[2];
  $('#wpTextbox1').wikiEditor('removeFromToolbar', obj);
  if (obj.tool) $('div[rel=' + obj.section +
                   '] > div[rel=' + obj.group +
                   '] > span[rel=' + obj.tool + ']').remove(); //HACK
}

/*************************************
*        configuration               *
*************************************/

//run user defined configuration
if ( typeof $j != 'undefined' && typeof $.fn.wikiEditor != 'undefined'
     && (wgAction == 'edit' || wgAction == 'submit')
   ) {
     if (typeof newtoolbar_config == 'function') {
        if ( typeof newtoolbar_delay == 'number' && newtoolbar_delay > 0) { //deprecated
           $(function () { window.setTimeout('newtoolbar_config()', newtoolbar_delay);  });
        } else {
           $(newtoolbar_config);
        }
     } else $(newtoolbar_standard);
}

//</nowiki>