MediaWiki:Edittools.js: Difference between revisions

From MandrakeWiki
Jump to navigation Jump to search
(Created page with "→‎<source lang="javascript"><nowiki> Top of Javascript: // Drop down box for the Special character menu in MediaWiki:Edittools // will be called from [[MediaWiki:Common.js...")
 
No edit summary
Line 1: Line 1:
/* <source lang="javascript"><nowiki> Top of Javascript */
// <source lang="javascript">
// Drop down box for the Special character menu in [[MediaWiki:Edittools]]
/*
// will be called from [[MediaWiki:Common.js]].
  EditTools support: add a selector, change into true buttons, enable for all text input fields
// Adapted from:
  If enabled in preferences, the script puts the buttons into the WikiEditor Toolbar
// http://commons.wikimedia.org/wiki/MediaWiki:Edittools.js
  The special characters to insert are defined at [[MediaWiki:Edittools]].
 
*/
// Switch for people that don't want it
if (load_edittools == true)
/*jshint curly:false */
{
/*global importStylesheetURI:false, jQuery:false, oldEdittools:false */
 
function addCharSubsetMenu() {
( function ( $ ) {
var specialchars = document.getElementById('specialchars');
"use strict";
 
var EditTools, insertTags;
if (specialchars) {
var menu = "<select style=\"display:inline\" onChange=\"chooseCharSubset(selectedIndex)\">";
importStylesheetURI('//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css');
menu += "<option>Standard</option>";
window.insertTags = insertTags = function (tagOpen, tagClose, sampleText) {
menu += "<option>Latin</option>";
    var $txtarea = EditTools.getTextArea();
menu += "<option>Greek</option>";
    if ($txtarea.length !== 1) return;
menu += "<option>Cyrillic</option>";
menu += "<option>IPA</option>";
    /* Usability initiative compatibility */
menu += "<option>Arabic</option>";
    if (typeof $.fn.textSelection !== 'undefined') {
menu += "<option>Catalan</option>";
      $txtarea.textSelection('encapsulateSelection', {
menu += "<option>Czech</option>";
        'pre': tagOpen,
menu += "<option>Devanāgarī</option>";
        'peri': sampleText,
menu += "<option>Esperanto</option>";
        'post': tagClose
menu += "<option>Estonian</option>";
      });
menu += "<option>French</option>";
      return;
menu += "<option>German</option>";
    }
menu += "<option>Hawaiian</option>";
};
menu += "<option>Hebrew</option>";
window.EditTools = EditTools = {
menu += "<option>Hungarian</option>";
  createSelector: function () {
menu += "<option>Icelandic</option>";
    var $spec = $('#specialchars');
menu += "<option>Italian</option>";
    var $sb = $('#specialchars p.specialbasic');
menu += "<option>Latvian</option>";
menu += "<option>Lithuanian</option>";
    // Only care if there is more than one
menu += "<option>Maltese</option>";
    if (!$spec.length || $sb.length <= 1) return;
menu += "<option>Old English</option>";
menu += "<option>Pinyin</option>";
    var $sel = $('<select>');
menu += "<option>Polish</option>";
menu += "<option>Portuguese</option>";
    $sel.change(function () {
menu += "<option>Romaji</option>";
      EditTools.chooseCharSubset();
menu += "<option>Romanian</option>";
    });
menu += "<option>Scandinavian</option>";
menu += "<option>Serbian</option>";
    $sb.each(function (i) {
menu += "<option>Spanish</option>";
      var id = $(this).attr('id').replace(/.([0-9A-F][0-9A-F])/g, '%$1').replace(/_/g, ' ');
menu += "<option>Turkish</option>";
      $sel.append('<option value='+ i +'>' + decodeURIComponent (id) + '</option>');
menu += "<option>Vietnamese</option>";
    });
menu += "<option>Welsh</option>";
menu += "<option>Yiddish</option>";
    $spec.prepend($sel);
menu += "</select>";
specialchars.innerHTML = menu + specialchars.innerHTML;
    this.chooseCharSubset();
 
  },
// Standard-CharSubset
chooseCharSubset(0);
  chooseCharSubset: function () {
}
    var $sb = $('#specialchars p.specialbasic');
}
 
    var id = $('#specialchars select').val();
// CharSubset selection
function chooseCharSubset(s) {
    var $wanted = $sb.eq(id);
var l = document.getElementById('specialchars').getElement sByTagName('p');
    this.makeButtons($wanted);
for (var i = 0; i < l.length ; i++) {
l[i].style.display = i == s ? 'inline' : 'none';
    $sb.hide();
// l[i].style.visibility = i == s ? 'visible' : 'hidden';
}
    $wanted.css('display', 'inline');
}
 
  },
// Menu insertion
if (window.addEventListener)  
  bindOnClick: function ($button, _this) {
window.addEventListener("load", addCharSubsetMenu, false);
    var onclick = _this.getAttribute("onclick");
else if (window.attachEvent)  
window.attachEvent("onload", addCharSubsetMenu);
    // if onclick is not a function, it's not IE7, so use setAttribute
 
    if('function' !== typeof onclick) {
}
        $button[0].setAttribute('onclick', onclick); // for FF,IE8,Chrome
/* Bottom of Javascript </nowiki></source>*/
    // if onclick is a function, use the IE7 method and call onclick() in the anonymous function
    } else {
        $button[0].onclick = function() {
            onclick();
        }; // for IE7
    }
  },
  makeButtons: function ($wanted) {
    var $links = $wanted.find('a');
    var _this = this;
    $links.each(function () {
      var $button = $('<button type="button">');
      $button.text($(this).text());
      _this.bindOnClick($button, this);
      $(this).replaceWith($button);
      $(this).blur();
    });
    $wanted.contents().not('button').remove();
  },
  makeToolbarButtons: function () {
    var _this = this;
    // Add  Edittool section
    $('#wpTextbox1').wikiEditor('addToToolbar', {
      'sections': {
        'Edittools': {
          'type': 'booklet',
          'label': 'Edittools',
          'pages': {
            'Edittools1': {
              'layout': 'characters',
              'label': 'Edittools2'
            }
          }
        }
      }
    });
    var $section = $('.page-Edittools1 div');
    var $links = $('#specialchars p.specialbasic').eq(0).find('a');
    $links.each(function () {
      var $button = $('<span>');
      $button.text($(this).text());
      _this.bindOnClick($button, this);
      $section.append($button);
    });
    $('.mw-editTools').remove();
  },
  last_active_textfield: null,
  enableForAllFields: function () {
    $('textarea, input').focus(function () {
      EditTools.last_active_textfield = this.id;
    });
  },
  getTextArea: function () {
    var $txtarea = {};
    if (EditTools.last_active_textfield !== null) $txtarea = $('#' + EditTools.last_active_textfield).eq(0);
    if ($txtarea.length !== 1) {
      $txtarea = $('#bodyContent textarea').eq(0);
    }
    return $txtarea;
  },
  registerTextField : function (evt)
  {
    var e = evt || window.event;
    var node = e.target || e.srcElement;
    if (!node) return;
    EditTools.last_active_textfield = node.id;
    return true;
  },
  setup: function () {
    //Decide whether to use the toolbar or the bottom div
    if ( ( typeof oldEdittools !== 'undefined' && oldEdittools === true ) || $('#wpUploadDescription').length || !$.wikiEditor || !$.wikiEditor.isSupported()) {
      EditTools.createSelector();
      EditTools.enableForAllFields();
    } else {
      EditTools.makeToolbarButtons();
      EditTools.enableForAllFields();
    }
  }
};
$(document).ready(function () {
    if ( $('#specialchars').length !== 1 ) return; // Don't do anything if no edittools present.
    EditTools.setup();
});
// </source>
}( jQuery ));

Revision as of 16:17, 13 March 2012

// <source lang="javascript">
/*
  EditTools support: add a selector, change into true buttons, enable for all text input fields
  If enabled in preferences, the script puts the buttons into the WikiEditor Toolbar
  The special characters to insert are defined at [[MediaWiki:Edittools]].
*/
 
/*jshint curly:false */
/*global importStylesheetURI:false, jQuery:false, oldEdittools:false */
 
( function ( $ ) {
"use strict";
var EditTools, insertTags;
 
importStylesheetURI('//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css');
window.insertTags = insertTags = function (tagOpen, tagClose, sampleText) {
    var $txtarea = EditTools.getTextArea();
    if ($txtarea.length !== 1) return;
 
    /* Usability initiative compatibility */
    if (typeof $.fn.textSelection !== 'undefined') {
      $txtarea.textSelection('encapsulateSelection', {
        'pre': tagOpen,
        'peri': sampleText,
        'post': tagClose
      });
      return;
    }
};
window.EditTools = EditTools = {
  createSelector: function () {
    var $spec = $('#specialchars');
    var $sb = $('#specialchars p.specialbasic');
 
    // Only care if there is more than one
    if (!$spec.length || $sb.length <= 1) return;
 
    var $sel = $('<select>');
 
    $sel.change(function () {
      EditTools.chooseCharSubset();
    });
 
    $sb.each(function (i) {
      var id = $(this).attr('id').replace(/.([0-9A-F][0-9A-F])/g, '%$1').replace(/_/g, ' ');
      $sel.append('<option value='+ i +'>' + decodeURIComponent (id) + '</option>');
    });
 
    $spec.prepend($sel);
 
    this.chooseCharSubset();
  },
 
  chooseCharSubset: function () {
    var $sb = $('#specialchars p.specialbasic');
 
    var id = $('#specialchars select').val();
 
    var $wanted = $sb.eq(id);
    this.makeButtons($wanted);
 
    $sb.hide();
 
    $wanted.css('display', 'inline');
 
  },
 
  bindOnClick: function ($button, _this) {
    var onclick = _this.getAttribute("onclick");
 
    // if onclick is not a function, it's not IE7, so use setAttribute
    if('function' !== typeof onclick) { 
        $button[0].setAttribute('onclick', onclick); // for FF,IE8,Chrome
 
    // if onclick is a function, use the IE7 method and call onclick() in the anonymous function
    } else {
        $button[0].onclick = function() {
            onclick();
        }; // for IE7
    }
  },
 
  makeButtons: function ($wanted) {
    var $links = $wanted.find('a');
    var _this = this;
 
    $links.each(function () {
      var $button = $('<button type="button">');
      $button.text($(this).text());
 
      _this.bindOnClick($button, this);
 
      $(this).replaceWith($button);
      $(this).blur();
    });
    $wanted.contents().not('button').remove();
  },
  makeToolbarButtons: function () {
    var _this = this;
 
    // Add  Edittool section
    $('#wpTextbox1').wikiEditor('addToToolbar', {
      'sections': {
        'Edittools': {
          'type': 'booklet',
          'label': 'Edittools',
          'pages': {
            'Edittools1': {
              'layout': 'characters',
              'label': 'Edittools2'
            }
          }
        }
      }
    });
 
 
    var $section = $('.page-Edittools1 div');
    var $links = $('#specialchars p.specialbasic').eq(0).find('a');
    $links.each(function () {
      var $button = $('<span>');
      $button.text($(this).text());
 
      _this.bindOnClick($button, this);
      $section.append($button);
    });
    $('.mw-editTools').remove();
  },
 
  last_active_textfield: null,
 
  enableForAllFields: function () {
    $('textarea, input').focus(function () {
      EditTools.last_active_textfield = this.id;
    });
  },
 
  getTextArea: function () {
    var $txtarea = {};
    if (EditTools.last_active_textfield !== null) $txtarea = $('#' + EditTools.last_active_textfield).eq(0);
    if ($txtarea.length !== 1) {
      $txtarea = $('#bodyContent textarea').eq(0);
    }
    return $txtarea;
  },
 
  registerTextField : function (evt)
  {
    var e = evt || window.event;
    var node = e.target || e.srcElement;
    if (!node) return;
    EditTools.last_active_textfield = node.id;
    return true;
  },
 
  setup: function () {
    //Decide whether to use the toolbar or the bottom div
    if ( ( typeof oldEdittools !== 'undefined' && oldEdittools === true ) || $('#wpUploadDescription').length || !$.wikiEditor || !$.wikiEditor.isSupported()) {
      EditTools.createSelector();
      EditTools.enableForAllFields();
    } else {
      EditTools.makeToolbarButtons();
      EditTools.enableForAllFields();
    }
  }
};
$(document).ready(function () {
    if ( $('#specialchars').length !== 1 ) return; // Don't do anything if no edittools present.
    EditTools.setup();
});
// </source>
}( jQuery ));