jquery.textSelection: Add ability to register custom implementation
authorDerk-Jan Hartman <hartman.wiki@gmail.com>
Mon, 28 Jul 2014 20:28:29 +0000 (22:28 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 23 Oct 2014 02:58:09 +0000 (03:58 +0100)
At times we replace the textarea with a different implementation, for
instance in the former iframe code of WikiEditor and now in CodeEditor.

Obviously only one implementation of this API can be in control at a
time, but in order to disable/enable which one exactly is currently
determined by the existence of function in the core WikiEditor
context. This is a remnant of when this code was still in WikiEditor.

I added two commands, "register" and "unregister", that take an object
that has alternative function implementations for one or more commands.

Bug: 29328
Change-Id: I14492572f7eb9bbd1af68872dbfef5159126f107

resources/src/jquery/jquery.textSelection.js

index 8d440fd..632769b 100644 (file)
@@ -24,8 +24,9 @@
 
        $.fn.textSelection = function ( command, options ) {
                var fn,
+                       alternateFn,
                        context,
-                       hasWikiEditorSurface, // The alt edit surface needs to implement the WikiEditor API
+                       hasWikiEditor,
                        needSave,
                        retval;
 
                        }
                };
 
+               alternateFn = $( this ).data( 'jquery.textSelection' );
+
                // Apply defaults
                switch ( command ) {
                        //case 'getContents': // no params
                                        force: false // Force a scroll even if the caret position is already visible
                                }, options );
                                break;
+                       case 'register':
+                               if ( alternateFn ) {
+                                       throw new Error( 'Another textSelection API was already registered' );
+                               }
+                               $( this ).data( 'jquery.textSelection', options );
+                               // No need to update alternateFn as this command only stores the options.
+                               // A command that uses it will set it again.
+                               return;
+                       case 'unregister':
+                               $( this ).removeData( 'jquery.textSelection' );
+                               return;
                }
 
                context = $( this ).data( 'wikiEditor-context' );
-               hasWikiEditorSurface = ( context !== undefined && context.$iframe !== undefined );
+               hasWikiEditor = ( context !== undefined && context.$iframe !== undefined );
 
                // IE selection restore voodoo
                needSave = false;
-               if ( hasWikiEditorSurface && context.savedSelection !== null ) {
+               if ( hasWikiEditor && context.savedSelection !== null ) {
                        context.fn.restoreSelection();
                        needSave = true;
                }
-               retval = ( hasWikiEditorSurface && context.fn[command] !== undefined ? context.fn : fn )[command].call( this, options );
-               if ( hasWikiEditorSurface && needSave ) {
+               retval = ( alternateFn && alternateFn[command] || fn[command] ).call( this, options );
+               if ( hasWikiEditor && needSave ) {
                        context.fn.saveSelection();
                }