Jump to content

User:PerfektesChaos/js/editorContent/d.js

From Wikipedia, the free encyclopedia
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.
/// User:PerfektesChaos/js/editorContent/d.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
//  Run advanced editor appropriate for current content
// ResourceLoader:  compatible;
//    dependencies: user, mediawiki.util
//    namespaces:   >= 0
//    doc:          w:en:PerfektesChaos/js/editorContent
/// Fingerprint: #0#0#
/// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA)
/// <nowiki>
/* global window: false                                                */
/* jshint forin: false,
          bitwise:true, curly:true, eqeqeq:true, latedef:true,
          laxbreak:true,
          nocomma:true, strict:true, undef:true, unused:true           */



( function ( mw, $ ) {
   "use strict";
   var EdiCt    =  "editorContent",
       Version  =  -1.3;
   if ( typeof mw.libs[ EdiCt ]  !==  "object"  ||
        ! mw.libs[ EdiCt ] ) {
      mw.libs[ EdiCt ]  =  { };
   }
   mw.libs[ EdiCt ].type  =  EdiCt;
   EdiCt                  =  mw.libs[ EdiCt ];
   EdiCt.signature        = "ext.gadget." + EdiCt.type;



   // Requires: JavaScript  1.3



   if ( mw.loader.getState( EdiCt.signature )  !==  "ready" ) {
      // 2017-03-01
      EdiCt.vsn      =  Version;
      EdiCt.support  =  "User:PerfektesChaos/js/" + EdiCt.type;
      EdiCt.doc      =  "[[w:en:" + EdiCt.support + "]]";
      EdiCt.config   =  { CodeEditor:   { css:        "\\.css$",
                                          javascript: "\\.js$",
                                          lua:        [ "/#s[^/]*/#M:",
                                                        2 ]
                                       },
                         VisualEditor: false,
                         WikEd:        true
                       };
      EdiCt.wikis   =  { dewiki: { s: "Spielwiese" },
                         enwiki: { s: "sandbox" }
                       };
   }



   EdiCt.fire  =  function () {
      // Start processing, if initially appropriate
      // Precondition:
      //    User settings available
      // Uses:
      //    >  .config
      //    >  .config.VisualEditor
      //    >< .opt
      //     < .g.model
      //     < .g.stuff
      //     < .g.scope
      //    .furnish()
      //    mw.config.get()
      //    .fulfil()
      //    .visualEditor()
      //    (.fit)
      // Remark: Used as event handler -- 'this' is not EdiCt
      // 2014-08-01 PerfektesChaos@de.wikipedia
      var env  =  mw.config.get( [ "wgAction",
                                   "wgPageContentModel",
                                   "wgPageName" ] );
      if ( typeof EdiCt.opt  !==  "object" ) {
         EdiCt.opt  =  false;
      }
      EdiCt.furnish( EdiCt.config, EdiCt.opt );
      EdiCt.g.stuff  =  env.wgPageName;
      EdiCt.g.model  =  env.wgPageContentModel;
//    EdiCt.visualEditor();
      EdiCt.g.scope  =  env.wgAction;
      if ( EdiCt.g.scope === "edit"  ||
           EdiCt.g.scope === "submit" ) {
         EdiCt.fit();
      }
   };   // .fire()



   EdiCt.fit  =  function () {
      // Run editors
      // Precondition:
      //    Really in edit mode
      // Uses:
      //    this
      //    >  .config
      //    >  .g.model
      //    >  .g.nsN
      //    >  .config.WikEd
      //    mw.loader.using()
      //    mw.config.get()
      //    (.codeEditor)
      //    (.wikEd)
      // 2013-09-08 PerfektesChaos@de.wikipedia
      var cnf  =  EdiCt.config;
      if ( this.g.model !== "wikitext"
           ||   ( this.g.nsN > 0
                  &&   this.g.nsN % 2  ===  0
                  &&   this.g.nsN !== 828 ) ) {
         mw.loader.using( [ "jquery.client" ],
                          EdiCt.codeEditor );
      }
      if ( cnf.WikEd ) {
         if ( ! mw.config.get( "wgCodeEditorCurrentLanguage" ) ) {
            mw.loader.using( [ "jquery.client" ],
                             EdiCt.wikEd );
         }
      }
   };   // .fit()



   EdiCt.flip  =  function ( apply ) {
      // Adapt string to local environment
      // Precondition:
      //    apply  -- string
      // Postcondition:
      //    Return adapted string
      // Uses:
      //    this
      //    >  .wikis
      //    mw.config.get()
      // 2013-03-22 PerfektesChaos@de.wikipedia
      var r  =  apply,
          env;
//console.log("editorContent.flip()",r)
      if ( typeof r  ===  "string" ) {
         if ( r.indexOf( "#" )  >=  0 ) {
            if ( r.indexOf( "#M" )  >=  0 ) {
               env  =  mw.config.get( "wgFormattedNamespaces" );
               if ( env ) {
                  r  =  r.replace( /#M/g,  env[ "828" ] );
               }
            }
            if ( r.indexOf( "#s" )  >=  0 ) {
               env  =  mw.config.get( "wgDBname" );
               env  =  this.wikis[ env ];
               if ( env ) {
                  r  =  r.replace( /#s/g,  env.s );
               }
            }
         }
      }
      return r;
   };   // .flip()



   EdiCt.fulfil  =  function ( apply ) {
      // Evaluate rule
      // Precondition:
      //    apply  -- rule
      // Postcondition:
      //    Return true iff rule matches
      // Uses:
      //    this
      //    >  .g.nsN
      //    >  .g.stuff
      // 2013-03-22 PerfektesChaos@de.wikipedia
      var r   =  false,
          re  =  false,
          i, n;
      switch ( typeof apply ) {
         case "boolean" :
            r  =  apply;
            break;
         case "string" :
            re  =  apply;
            break;
         case "object" :
            if ( apply ) {
               n  =  apply.length;
               if ( n ) {
                  for ( i = 1;  i < n;  i++ ) {
                     if ( apply[ i ] === this.g.nsN ) {
                        re  =  apply[ 0 ];
                        break;   // for i
                     }
                  }   // for i
               }
            }
            break;
      }   // switch  typeof apply
      if ( re ) {
         re  =  new RegExp( re );
         r   =  re.test( this.g.stuff );
      }
      return r;
   };   // .fulfil()



   EdiCt.furnish  =  function ( already, add ) {
      // Merge user options and defaults
      // Precondition:
      //    already  -- .config or its sub
      //    add      -- .opt or its sub
      // Uses:
      //    this
      //    .furnish()
      // 2013-03-22 PerfektesChaos@de.wikipedia
      var p, v;
      if ( typeof add === "object"  &&  add ) {
         for ( p in add ) {
            v  =  add[ p ];
            if ( typeof already[ p ] === "object" ) {
               if ( v ) {
                  this.furnish( already[ p ], v );
               } else {
                  already[ p ]  =  false;
               }
            } else if ( already[ p ] === false  ||  v !== true ) {
               already[ p ]  =  v;
            }
         }   // for p in .opt
      }
   };   // .furnish()



   EdiCt.codeEditor  =  function () {
      // Deal with CodeEditor
      // Uses:
      //    mw.loader.using()
      //    .codeEditorStart()
      //    (.codeEditorDisable)
      // Remark: Used as event handler -- 'this' is not EdiCt
      // 2014-08-01 PerfektesChaos@de.wikipedia
      EdiCt.g.client  =  $.client.profile();
      if ( ! EdiCt.g.client ) {
         EdiCt.g.client  =  { name: "?" };
      }
      if ( "chrome firefox".indexOf( EdiCt.g.client.name ) < 0 ) {
         mw.loader.using( [ "mediawiki.cookie" ],
                          EdiCt.codeEditorDisable );
      } else {
         EdiCt.codeEditorStart();
      }
   };   // .codeEditor()



   EdiCt.codeEditorDisable  =  function () {
      // Disable CodeEditor, if ever activated
      // Uses:
      //    >  mw.util.$content
      //    mw.hook()
      //    jQuery.removeCookie()  :: mediawiki.cookie
      // Remark: Used as event handler -- 'this' is not EdiCt
      // 2014-08-01 PerfektesChaos@de.wikipedia
      mw.hook( "codeEditor.configure" ).add( function () {
             var context = mw.util.$content.find( "#wpTextbox1" )
                                           .data( "wikiEditor-context" );
             if ( context
                  && context.codeEditor && context.api && context.fn ) {
                context.fn.disableCodeEditor();
                context.api.removeFromToolbar( context,
                                               { "section": "main",
                                                 "group":   "format",
                                                 "tools":   "codeEditor" } );
//              mw.cookie.set( "wikiEditor-0-codeEditor-enabled", null );
                $.removeCookie( "wikiEditor-0-codeEditor-enabled" );
             }
      } );
   };   // .codeEditorDisable()



   EdiCt.codeEditorStart  =  function () {
      // Run CodeEditor, if appropriate
      // Uses:
      //    this
      //    >  .config.CodeEditor
      //    mw.config.get()
      //    .flip()
      //    .fulfil()
      //    mw.config.set()
      //    mw.loader.load()
      // 2013-09-09 PerfektesChaos@de.wikipedia
      var mode, v,
          base    =  "|css|javascript|json|lua|",
          depend  =  [ "ext.codeEditor",
                       "ext.codeEditor.ace",
                       "jquery.ui",
                       "ext.wikiEditor" ],
          select  =  mw.config.get( "wgCodeEditorCurrentLanguage" );
      if ( ! select ) {
         for ( mode in this.config.CodeEditor ) {
            v  =  this.config.CodeEditor[ mode ];
            switch ( typeof v ) {
               case "string" :
                  if ( this.fulfil( this.flip( v ) ) ) {
                     select  =  mode;
                  }
                  break;
               case "object" :
                  if ( v ) {
                     if ( v.length ) {
                        v[ 0 ]  =  this.flip( v[ 0 ] );
                        if ( this.fulfil( v ) ) {
                           select  =  mode;
                        }
                     }
                  }
                  break;
            }   // switch  typeof CodeEditor[ mode ]
         }   // for mode in .CodeEditor
      }
      if ( select ) {
         select  =  select.toLowerCase();
         mw.config.set( "wgCodeEditorCurrentLanguage", select );
         if ( base.indexOf( "|" + select + "|" )  <  0 ) {
            depend.push( "ext.codeEditor.ace.modes" );
         }
         mw.loader.load( depend );
      }
      if ( typeof window.wikEd  ===  "object"   &&   window.wikEd ) {
         if ( window.wikEd.useWikEd ) {
            window.wikEd.turnedOn  =  false;
         }
      }
   };   // .codeEditorStart()



/*
   EdiCt.visualEditor  =  function () {
      // Remove VisualEditor link, if or where not desired
      // Uses:
      //    this
      //    >  .g.model
      //    >  .g.stuff
      //    >  .config.VisualEditor
      //    >  document
      //    mw.user.options.get()
      //    .visualEditorRemove()
      // 2013-05-19 PerfektesChaos@de.wikipedia
      var link, n, s,
          cnf  =  EdiCt.config;
      if ( mw.user.options.get( "visualeditor-enable" ) ) {   // "1"
         if ( this.g.model ) {
            link  =  ( this.g.model === "wikitext" );
         } else {   // ...2013-05
            s     =  this.g.stuff.toLowerCase();
            n     =  s.length;
            link  =  ( s.substr( n - 4 )  !==  ".css"  &&
                       s.substr( n - 3 )  !==  ".js" );
         }
         if ( ! link ) {
            if ( typeof cnf.VisualEditor  !==  "boolean" ) {
               link  =  this.fulfil( cnf.VisualEditor );
            }
         }
         if ( ! link ) {
            this.visualEditorRemove();
         }
      }
   };   // .visualEditor()



   EdiCt.visualEditorRemove  =  function () {
      // Hide VisualEditor link
      // Uses:
      //    mw.util.addCSS()
      // 2013-05-19 PerfektesChaos@de.wikipedia
      mw.util.addCSS( "#ca-edit{display:none;}" );
   };   // .visualEditorRemove()
*/



/*
            s  =  ' A[href*=".js?veaction=edit"]';
            s  =  "#p-views" + s + ", #p-cactions" + s
                  + "{display:none;}";
                           A[href*=".js?veaction=edit"]
                          {
                             display:none;
                          }
*/

//          p-views ./. ca-action   ...  focus ... grey out
            // DocumentEvent.createEvent("MutationEvent")
            // DOMNodeInserted
            // https://developer.mozilla.org/en-US/docs/DOM/MutationObserver?redirectlocale=en-US&redirectslug=DOM%2FDOM_Mutation_Observers
/*
   EdiCt.visualEditorRemove  =  function () {
      // Remove VisualEditor link
      // Uses:
      //    jQuery().find()
      //    jQuery().remove()
      // Remark: Used as event handler -- 'this' is not EdiCt
      // 2013-04-21 PerfektesChaos@de.wikipedia
mw.log({loud:true},".visualEditorRemove()",0);
      if ( $( window.document ).find( "#ca-editsource" ).length ) {
         $( window.document ).find( "#ca-edit" ).remove();
      }
//      "#ca-edit" "#ca-ve-edit" "#ca-editsource"
   };   // .visualEditorRemove()
*/



   EdiCt.wikEd  =  function () {
      // Run WikEd, if appropriate
      // Uses:
      //    >  jQuery.client
      //    >  .config.WikEd
      //     < .g.client
      //    .fulfil()
      //    mw.loader.load()
      // Remark: Used as event handler -- 'this' is not EdiCt
      // 2015-10-04 PerfektesChaos@de.wikipedia
      EdiCt.g.client  =  $.client.profile();
      if ( ! EdiCt.g.client ) {
         EdiCt.g.client  =  { };
      }
      if ( EdiCt.g.client.layout === "gecko" ) {
         if ( EdiCt.fulfil( EdiCt.config.WikEd ) ) {
            mw.loader.load( "https://en.wikipedia.org/w/index.php?title="
                            + "User:Cacycle/wikEd.js"
                            + "&action=raw"
                            + "&bcache=1&maxage=604801"
                            + "&ctype=text/javascript",
                            "text/javascript" );
         }
      }
   };   // .wikEd()



   function fire() {
      // Intialize application, check for meaningful usage
      // Uses:
      //    >  .signature
      //     < .g
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.config.get()
      //    mw.loader.using()
      //    (EdiCt.fire)
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var rls;
      if ( mw.loader.getState( EdiCt.signature )  !==  "ready" ) {
         rls = { };
         rls[ EdiCt.signature ] = "ready";
         mw.loader.state( rls );
         EdiCt.g  =  { nsN:  mw.config.get( "wgNamespaceNumber" ) };
         if ( EdiCt.g.nsN >= 0 ) {
            mw.loader.using( [ "user",
                               "user.options",
                               "mediawiki.util" ],
                             EdiCt.fire );
         }
      }
   }   // fire()



   fire();
}( window.mediaWiki, window.jQuery ) );



// Emacs
// Local Variables:
// coding: utf-8-dos
// fill-column: 80
// End:

/// EOF </nowiki>   editorContent/d.js