[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / porte_plume / javascript / jquery.markitup.js
index ab99b74..7567f50 100644 (file)
@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------------------
 // markItUp! Universal MarkUp Engine, JQuery plugin
-// v 1.1.12
+// v 1.1.14 ( c014800b - 02/06/2014 )
 // Dual licensed under the MIT and GPL licenses.
 // ----------------------------------------------------------------------------
-// Copyright (C) 2007-2011 Jay Salvat
+// Copyright (C) 2007-2012 Jay Salvat
 // http://markitup.jaysalvat.com/
 // ----------------------------------------------------------------------------
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // ----------------------------------------------------------------------------
 (function($) {
        $.fn.markItUp = function(settings, extraSettings) {
-               var options, ctrlKey, shiftKey, altKey;
-               ctrlKey = shiftKey = altKey = false;
-       
+               var method, params, options, ctrlKey, shiftKey, altKey; ctrlKey = shiftKey = altKey = false;
+
+               if (typeof settings == 'string') {
+                       method = settings;
+                       params = extraSettings;
+               } 
+
                options = {     id:                                             '',
                                        nameSpace:                              '',
                                        root:                                   '',
+                                       previewHandler:                 false,
                                        previewInWindow:                '', // 'width=800, height=600, resizable=yes, scrollbars=yes'
+                                       previewInElement:               '',
                                        previewAutoRefresh:             true,
                                        previewPosition:                'after',
                                        previewTemplatePath:    '~/templates/preview.html',
                                        previewParser:                  false,
                                        previewParserPath:              '',
                                        previewParserVar:               'data',
+                                       previewParserAjaxType:  'POST',
                                        resizeHandle:                   true,
                                        beforeInsert:                   '',
                                        afterInsert:                    '',
                        });
                }
 
+               // Quick patch to keep compatibility with jQuery 1.9
+               var uaMatch = function(ua) {
+                       ua = ua.toLowerCase();
+
+                       var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
+                               /(webkit)[ \/]([\w.]+)/.exec(ua) ||
+                               /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
+                               /(msie) ([\w.]+)/.exec(ua) ||
+                               ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
+                               [];
+
+                       return {
+                               browser: match[ 1 ] || "",
+                               version: match[ 2 ] || "0"
+                       };
+               };
+               var matched = uaMatch( navigator.userAgent );
+               var browser = {};
+
+               if (matched.browser) {
+                       browser[matched.browser] = true;
+                       browser.version = matched.version;
+               }
+               if (browser.chrome) {
+                       browser.webkit = true;
+               } else if (browser.webkit) {
+                       browser.safari = true;
+               }
+
                return this.each(function() {
                        var $$, textarea, levels, scrollPosition, caretPosition, caretOffset,
                                clicked, hash, header, footer, previewWindow, template, iFrame, abort;
                        options.previewParserPath = localize(options.previewParserPath);
                        options.previewTemplatePath = localize(options.previewTemplatePath);
 
+                       if (method) {
+                               switch(method) {
+                                       case 'remove':
+                                               remove();
+                                       break;
+                                       case 'insert':
+                                               markup(params);
+                                       break;
+                                       default: 
+                                               $.error('Method ' +  method + ' does not exist on jQuery.markItUp');
+                               }
+                               return;
+                       }
+
                        // apply the computed path to ~/
                        function localize(data, inText) {
                                if (inText) {
                                footer = $('<div class="markItUpFooter"></div>').insertAfter($$);
 
                                // add the resize handle after textarea
-                               if (options.resizeHandle === true && $.browser.safari !== true) {
+                               if (options.resizeHandle === true && browser.safari !== true) {
                                        resizeHandle = $('<div class="markItUpResizeHandle"></div>')
                                                .insertAfter($$)
-                                               .bind("mousedown", function(e) {
+                                               .bind("mousedown.markItUp", function(e) {
                                                        var h = $$.height(), y = e.clientY, mouseMove, mouseUp;
                                                        mouseMove = function(e) {
                                                                $$.css("height", Math.max(20, e.clientY+h-y)+"px");
                                                                return false;
                                                        };
                                                        mouseUp = function(e) {
-                                                               $("html").unbind("mousemove", mouseMove).unbind("mouseup", mouseUp);
+                                                               $("html").unbind("mousemove.markItUp", mouseMove).unbind("mouseup.markItUp", mouseUp);
                                                                return false;
                                                        };
-                                                       $("html").bind("mousemove", mouseMove).bind("mouseup", mouseUp);
+                                                       $("html").bind("mousemove.markItUp", mouseMove).bind("mouseup.markItUp", mouseUp);
                                        });
                                        footer.append(resizeHandle);
                                }
 
                                // listen key events
-                               $$.keydown(keyPressed).keyup(keyPressed);
+                               $$.bind('keydown.markItUp', keyPressed).bind('keyup', keyPressed);
                                
                                // bind an event to catch external calls
-                               $$.bind("insertion", function(e, settings) {
+                               $$.bind("insertion.markItUp", function(e, settings) {
                                        if (settings.target !== false) {
                                                get();
                                        }
                                });
 
                                // remember the last focus
-                               $$.focus(function() {
+                               $$.bind('focus.markItUp', function() {
                                        $.markItUp.focused = this;
                                });
+
+                               if (options.previewInElement) {
+                                       refreshPreview();
+                               }
                        }
 
                        // recursively build header with dropMenus from markupset
                                $('li:hover > ul', ul).css('display', 'block');
                                $.each(markupSet, function() {
                                        var button = this, t = '', title, li, j;
-                                       title = (button.key) ? (button.name||'')+' [Ctrl+'+button.key+']' : (button.name||'');
+                                       button.title ? title = (button.key) ? (button.title||'')+' [Ctrl+'+button.key+']' : (button.title||'') : title = (button.key) ? (button.name||'')+' [Ctrl+'+button.key+']' : (button.name||'');
                                        key   = (button.key) ? 'accesskey="'+button.key+'"' : '';
                                        if (button.separator) {
                                                li = $('<li class="markItUpSeparator">'+(button.separator||'')+'</li>').appendTo(ul);
                                                for (j = levels.length -1; j >= 0; j--) {
                                                        t += levels[j]+"-";
                                                }
-                                               li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>')
-                                               .bind("contextmenu", function() { // prevent contextmenu on mac and allow ctrl+click
-                                                       return false;
-                                               }).click(function() {
+                                               li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="#" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>')
+                                               .bind("contextmenu.markItUp", function() { // prevent contextmenu on mac and allow ctrl+click
                                                        return false;
-                                               }).bind("focusin", function(){
+                                               }).bind('click.markItUp', function(e) {
+                                                       e.preventDefault();
+                                               }).bind("focusin.markItUp", function(){
                             $$.focus();
-                                               }).mouseup(function() {
+                                               }).bind('mouseup', function(e) {
                                                        if (button.call) {
-                                                               eval(button.call)();
+                                                               eval(button.call)(e); // Pass the mouseup event to custom delegate
                                                        }
                                                        setTimeout(function() { markup(button) },1);
                                                        return false;
-                                               }).hover(function() {
+                                               }).bind('mouseenter.markItUp', function() {
                                                                $('> ul', this).show();
                                                                $(document).one('click', function() { // close dropmenu if click outside
                                                                                $('ul ul', header).hide();
                                                                        }
                                                                );
-                                                       }, function() {
+                                               }).bind('mouseleave.markItUp', function() {
                                                                $('> ul', this).hide();
-                                                       }
-                                               ).appendTo(ul);
+                                               }).appendTo(ul);
                                                if (button.dropMenu) {
                                                        levels.push(i);
                                                        $(li).addClass('markItUpDropMenu').append(dropMenus(button.dropMenu));
                                } else {
                                        string = string || selection;
 
-                                       var lines = selection.split(/\r?\n/), blocks = [];
+                                       var lines = [string], blocks = [];
                                        
-                                       for (var l=0; l < lines.length; l++) {
+                                       if (multiline === true) {
+                                               lines = string.split(/\r?\n/);
+                                       }
+                                       
+                                       for (var l = 0; l < lines.length; l++) {
                                                line = lines[l];
                                                var trailingSpaces;
                                                if (trailingSpaces = line.match(/ *$/)) {
                                block = openBlockWith + block + closeBlockWith;
 
                                return {        block:block, 
+                                                       openBlockWith:openBlockWith,
                                                        openWith:openWith, 
                                                        replaceWith:replaceWith, 
                                                        placeHolder:placeHolder,
-                                                       closeWith:closeWith
+                                                       closeWith:closeWith,
+                                                       closeBlockWith:closeBlockWith
                                        };
                        }
 
                                                        lines[i] = "";
                                                }
                                        }
+
                                        string = { block:lines.join('\n')};
                                        start = caretPosition;
-                                       len = string.block.length + (($.browser.opera) ? n-1 : 0);
+                                       len = string.block.length + ((browser.opera) ? n-1 : 0);
                                } else if (ctrlKey === true) {
                                        string = build(selection);
                                        start = caretPosition + string.openWith.length;
                                if ((selection === '' && string.replaceWith === '')) {
                                        caretOffset += fixOperaBug(string.block);
                                        
-                                       start = caretPosition + string.openWith.length;
-                                       len = string.block.length - string.openWith.length - string.closeWith.length;
+                                       start = caretPosition + string.openBlockWith.length + string.openWith.length;
+                                       len = string.block.length - string.openBlockWith.length - string.openWith.length - string.closeWith.length - string.closeBlockWith.length;
 
                                        caretOffset = $$.val().substring(caretPosition,  $$.val().length).length;
                                        caretOffset -= fixOperaBug($$.val().substring(0, caretPosition));
 
                        // Substract linefeed in Opera
                        function fixOperaBug(string) {
-                               if ($.browser.opera) {
+                               if (browser.opera) {
                                        return string.length - string.replace(/\n*/g, '').length;
                                }
                                return 0;
                        }
                        // Substract linefeed in IE
                        function fixIeBug(string) {
-                               if ($.browser.msie) {
+                               if (browser.msie) {
                                        return string.length - string.replace(/\r*/g, '').length;
                                }
                                return 0;
                        function set(start, len) {
                                if (textarea.createTextRange){
                                        // quick fix to make it work on Opera 9.5
-                                       if ($.browser.opera && $.browser.version >= 9.5 && len == 0) {
+                                       if (browser.opera && browser.version >= 9.5 && len == 0) {
                                                return false;
                                        }
                                        range = textarea.createTextRange();
                                scrollPosition = textarea.scrollTop;
                                if (document.selection) {
                                        selection = document.selection.createRange().text;
-                                       if ($.browser.msie) { // ie
+                                       if (browser.msie) { // ie
                                                var range = document.selection.createRange(), rangeCopy = range.duplicate();
                                                rangeCopy.moveToElementText(textarea);
                                                caretPosition = -1;
 
                        // open preview window
                        function preview() {
-                               if (!previewWindow || previewWindow.closed) {
+                               if (typeof options.previewHandler === 'function') {
+                                       previewWindow = true;
+                               } else if (options.previewInElement) {
+                                       previewWindow = $(options.previewInElement);
+                               } else if (!previewWindow || previewWindow.closed) {
                                        if (options.previewInWindow) {
                                                previewWindow = window.open('', 'preview', options.previewInWindow);
                                                $(window).unload(function() {
                                renderPreview();
                        }
 
-                       function renderPreview() {              
+                       function renderPreview() {
                                var phtml;
-                               if (options.previewParser && typeof options.previewParser === 'function') {
+                               if (options.previewHandler && typeof options.previewHandler === 'function') {
+                                       options.previewHandler( $$.val() );
+                               } else if (options.previewParser && typeof options.previewParser === 'function') {
                                        var data = options.previewParser( $$.val() );
-                                       writeInPreview( localize(data, 1) ); 
+                                       writeInPreview(localize(data, 1) ); 
                                } else if (options.previewParserPath !== '') {
                                        $.ajax({
-                                               type: 'POST',
+                                               type: options.previewParserAjaxType,
                                                dataType: 'text',
                                                global: false,
                                                url: options.previewParserPath,
                        }
                        
                        function writeInPreview(data) {
-                               if (previewWindow.document) {                   
+                               if (options.previewInElement) {
+                                       $(options.previewInElement).html(data);
+                               } else if (previewWindow && previewWindow.document) {                   
                                        try {
                                                sp = previewWindow.document.documentElement.scrollTop
                                        } catch(e) {
 
                                if (e.type === 'keydown') {
                                        if (ctrlKey === true) {
-                                               li = $('a[accesskey="'+String.fromCharCode(e.keyCode)+'"]', header).parent('li');
+                                               li = $('a[accesskey="'+((e.keyCode == 13) ? '\\n' : String.fromCharCode(e.keyCode))+'"]', header).parent('li');
                                                if (li.length !== 0) {
                                                        ctrlKey = false;
                                                        setTimeout(function() {
                                }
                        }
 
+                       function remove() {
+                               $$.unbind(".markItUp").removeClass('markItUpEditor');
+                               $$.parent('div').parent('div.markItUp').parent('div').replaceWith($$);
+
+                               var relativeRef = $$.parent('div').parent('div.markItUp').parent('div');
+                               if (relativeRef.length) {
+                                   relativeRef.replaceWith($$);
+                               }
+                               
+                               $$.data('markItUp', null);
+                       }
+
                        init();
                });
        };
 
        $.fn.markItUpRemove = function() {
                return this.each(function() {
-                               var $$ = $(this).unbind().removeClass('markItUpEditor');
-                               $$.parent('div').parent('div.markItUp').parent('div').replaceWith($$);
+                               $(this).markItUp('remove');
                        }
                );
        };