Mercurial > public > sg101
view static/js/markitup/jquery.markitup1.1.14.min.js @ 887:9a15f7c27526
Actually save model object upon change.
This commit was tested on the comments model.
Additional logging added.
Added check for Markdown image references.
Added TODOs after observing behavior on comments.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 03 Feb 2015 21:09:44 -0600 |
parents | ad69236e8501 |
children |
line wrap: on
line source
// ---------------------------------------------------------------------------- // markItUp! Universal MarkUp Engine, JQuery plugin // v 1.1.x // Dual licensed under the MIT and GPL licenses. // ---------------------------------------------------------------------------- // Copyright (C) 2007-2012 Jay Salvat // http://markitup.jaysalvat.com/ // ---------------------------------------------------------------------------- // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // ---------------------------------------------------------------------------- (function($){$.fn.markItUp=function(settings,extraSettings){var method,params,options,ctrlKey,shiftKey,altKey;ctrlKey=shiftKey=altKey=!1,typeof settings=="string"&&(method=settings,params=extraSettings),options={id:"",nameSpace:"",root:"",previewHandler:!1,previewInWindow:"",previewInElement:"",previewAutoRefresh:!0,previewPosition:"after",previewTemplatePath:"~/templates/preview.html",previewParser:!1,previewParserPath:"",previewParserVar:"data",resizeHandle:!0,beforeInsert:"",afterInsert:"",onEnter:{},onShiftEnter:{},onCtrlEnter:{},onTab:{},markupSet:[{}]},$.extend(options,settings,extraSettings),options.root||$("script").each(function(a,b){miuScript=$(b).get(0).src.match(/(.*)jquery\.markitup(\.pack)?\.js$/),miuScript!==null&&(options.root=miuScript[1])});var uaMatch=function(a){a=a.toLowerCase();var b=/(chrome)[ \/]([\w.]+)/.exec(a)||/(webkit)[ \/]([\w.]+)/.exec(a)||/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(a)||/(msie) ([\w.]+)/.exec(a)||a.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(a)||[];return{browser:b[1]||"",version:b[2]||"0"}},matched=uaMatch(navigator.userAgent),browser={};return matched.browser&&(browser[matched.browser]=!0,browser.version=matched.version),browser.chrome?browser.webkit=!0:browser.webkit&&(browser.safari=!0),this.each(function(){function localize(a,b){return b?a.replace(/("|')~\//g,"$1"+options.root):a.replace(/^~\//,options.root)}function init(){id="",nameSpace="",options.id?id='id="'+options.id+'"':$$.attr("id")&&(id='id="markItUp'+$$.attr("id").substr(0,1).toUpperCase()+$$.attr("id").substr(1)+'"'),options.nameSpace&&(nameSpace='class="'+options.nameSpace+'"'),$$.wrap("<div "+nameSpace+"></div>"),$$.wrap("<div "+id+' class="markItUp"></div>'),$$.wrap('<div class="markItUpContainer"></div>'),$$.addClass("markItUpEditor"),header=$('<div class="markItUpHeader"></div>').insertBefore($$),$(dropMenus(options.markupSet)).appendTo(header),footer=$('<div class="markItUpFooter"></div>').insertAfter($$),options.resizeHandle===!0&&browser.safari!==!0&&(resizeHandle=$('<div class="markItUpResizeHandle"></div>').insertAfter($$).bind("mousedown.markItUp",function(a){var b=$$.height(),c=a.clientY,d,e;d=function(a){return $$.css("height",Math.max(20,a.clientY+b-c)+"px"),!1},e=function(a){return $("html").unbind("mousemove.markItUp",d).unbind("mouseup.markItUp",e),!1},$("html").bind("mousemove.markItUp",d).bind("mouseup.markItUp",e)}),footer.append(resizeHandle)),$$.bind("keydown.markItUp",keyPressed).bind("keyup",keyPressed),$$.bind("insertion.markItUp",function(a,b){b.target!==!1&&get(),textarea===$.markItUp.focused&&markup(b)}),$$.bind("focus.markItUp",function(){$.markItUp.focused=this}),options.previewInElement&&refreshPreview()}function dropMenus(markupSet){var ul=$("<ul></ul>"),i=0;return $("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||"",key=button.key?'accesskey="'+button.key+'"':"";if(button.separator)li=$('<li class="markItUpSeparator">'+(button.separator||"")+"</li>").appendTo(ul);else{i++;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.markItUp",function(){return!1}).bind("click.markItUp",function(a){a.preventDefault()}).bind("focusin.markItUp",function(){$$.focus()}).bind("mouseup",function(){return button.call&&eval(button.call)(),setTimeout(function(){markup(button)},1),!1}).bind("mouseenter.markItUp",function(){$("> ul",this).show(),$(document).one("click",function(){$("ul ul",header).hide()})}).bind("mouseleave.markItUp",function(){$("> ul",this).hide()}).appendTo(ul),button.dropMenu&&(levels.push(i),$(li).addClass("markItUpDropMenu").append(dropMenus(button.dropMenu)))}}),levels.pop(),ul}function magicMarkups(a){return a?(a=a.toString(),a=a.replace(/\(\!\(([\s\S]*?)\)\!\)/g,function(a,b){var c=b.split("|!|");return altKey===!0?c[1]!==undefined?c[1]:c[0]:c[1]===undefined?"":c[0]}),a=a.replace(/\[\!\[([\s\S]*?)\]\!\]/g,function(a,b){var c=b.split(":!:");return abort===!0?!1:(value=prompt(c[0],c[1]?c[1]:""),value===null&&(abort=!0),value)}),a):""}function prepare(a){return $.isFunction(a)&&(a=a(hash)),magicMarkups(a)}function build(a){var b=prepare(clicked.openWith),c=prepare(clicked.placeHolder),d=prepare(clicked.replaceWith),e=prepare(clicked.closeWith),f=prepare(clicked.openBlockWith),g=prepare(clicked.closeBlockWith),h=clicked.multiline;if(d!=="")block=b+d+e;else if(selection===""&&c!=="")block=b+c+e;else{a=a||selection;var i=[a],j=[];h===!0&&(i=a.split(/\r?\n/));for(var k=0;k<i.length;k++){line=i[k];var l;(l=line.match(/ *$/))?j.push(b+line.replace(/ *$/g,"")+e+l):j.push(b+line+e)}block=j.join("\n")}return block=f+block+g,{block:block,openBlockWith:f,openWith:b,replaceWith:d,placeHolder:c,closeWith:e,closeBlockWith:g}}function markup(a){var b,c,d,e;hash=clicked=a,get(),$.extend(hash,{line:"",root:options.root,textarea:textarea,selection:selection||"",caretPosition:caretPosition,ctrlKey:ctrlKey,shiftKey:shiftKey,altKey:altKey}),prepare(options.beforeInsert),prepare(clicked.beforeInsert),(ctrlKey===!0&&shiftKey===!0||a.multiline===!0)&&prepare(clicked.beforeMultiInsert),$.extend(hash,{line:1});if(ctrlKey===!0&&shiftKey===!0){lines=selection.split(/\r?\n/);for(c=0,d=lines.length,e=0;e<d;e++)$.trim(lines[e])!==""?($.extend(hash,{line:++c,selection:lines[e]}),lines[e]=build(lines[e]).block):lines[e]="";string={block:lines.join("\n")},start=caretPosition,b=string.block.length+(browser.opera?d-1:0)}else ctrlKey===!0?(string=build(selection),start=caretPosition+string.openWith.length,b=string.block.length-string.openWith.length-string.closeWith.length,b-=string.block.match(/ $/)?1:0,b-=fixIeBug(string.block)):shiftKey===!0?(string=build(selection),start=caretPosition,b=string.block.length,b-=fixIeBug(string.block)):(string=build(selection),start=caretPosition+string.block.length,b=0,start-=fixIeBug(string.block));selection===""&&string.replaceWith===""&&(caretOffset+=fixOperaBug(string.block),start=caretPosition+string.openBlockWith.length+string.openWith.length,b=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))),$.extend(hash,{caretPosition:caretPosition,scrollPosition:scrollPosition}),string.block!==selection&&abort===!1?(insert(string.block),set(start,b)):caretOffset=-1,get(),$.extend(hash,{line:"",selection:selection}),(ctrlKey===!0&&shiftKey===!0||a.multiline===!0)&&prepare(clicked.afterMultiInsert),prepare(clicked.afterInsert),prepare(options.afterInsert),previewWindow&&options.previewAutoRefresh&&refreshPreview(),shiftKey=altKey=ctrlKey=abort=!1}function fixOperaBug(a){return browser.opera?a.length-a.replace(/\n*/g,"").length:0}function fixIeBug(a){return browser.msie?a.length-a.replace(/\r*/g,"").length:0}function insert(a){if(document.selection){var b=document.selection.createRange();b.text=a}else textarea.value=textarea.value.substring(0,caretPosition)+a+textarea.value.substring(caretPosition+selection.length,textarea.value.length)}function set(a,b){if(textarea.createTextRange){if(browser.opera&&browser.version>=9.5&&b==0)return!1;range=textarea.createTextRange(),range.collapse(!0),range.moveStart("character",a),range.moveEnd("character",b),range.select()}else textarea.setSelectionRange&&textarea.setSelectionRange(a,a+b);textarea.scrollTop=scrollPosition,textarea.focus()}function get(){textarea.focus(),scrollPosition=textarea.scrollTop;if(document.selection){selection=document.selection.createRange().text;if(browser.msie){var a=document.selection.createRange(),b=a.duplicate();b.moveToElementText(textarea),caretPosition=-1;while(b.inRange(a))b.moveStart("character"),caretPosition++}else caretPosition=textarea.selectionStart}else caretPosition=textarea.selectionStart,selection=textarea.value.substring(caretPosition,textarea.selectionEnd);return selection}function preview(){typeof options.previewHandler=="function"?previewWindow=!0:options.previewInElement?previewWindow=$(options.previewInElement):!previewWindow||previewWindow.closed?options.previewInWindow?(previewWindow=window.open("","preview",options.previewInWindow),$(window).unload(function(){previewWindow.close()})):(iFrame=$('<iframe class="markItUpPreviewFrame"></iframe>'),options.previewPosition=="after"?iFrame.insertAfter(footer):iFrame.insertBefore(header),previewWindow=iFrame[iFrame.length-1].contentWindow||frame[iFrame.length-1]):altKey===!0&&(iFrame?iFrame.remove():previewWindow.close(),previewWindow=iFrame=!1),options.previewAutoRefresh||refreshPreview(),options.previewInWindow&&previewWindow.focus()}function refreshPreview(){renderPreview()}function renderPreview(){var a;if(options.previewHandler&&typeof options.previewHandler=="function")options.previewHandler($$.val());else if(options.previewParser&&typeof options.previewParser=="function"){var b=options.previewParser($$.val());writeInPreview(localize(b,1))}else options.previewParserPath!==""?$.ajax({type:"POST",dataType:"text",global:!1,url:options.previewParserPath,data:options.previewParserVar+"="+encodeURIComponent($$.val()),success:function(a){writeInPreview(localize(a,1))}}):template||$.ajax({url:options.previewTemplatePath,dataType:"text",global:!1,success:function(a){writeInPreview(localize(a,1).replace(/<!-- content -->/g,$$.val()))}});return!1}function writeInPreview(a){if(options.previewInElement)$(options.previewInElement).html(a);else if(previewWindow&&previewWindow.document){try{sp=previewWindow.document.documentElement.scrollTop}catch(b){sp=0}previewWindow.document.open(),previewWindow.document.write(a),previewWindow.document.close(),previewWindow.document.documentElement.scrollTop=sp}}function keyPressed(a){shiftKey=a.shiftKey,altKey=a.altKey,ctrlKey=!a.altKey||!a.ctrlKey?a.ctrlKey||a.metaKey:!1;if(a.type==="keydown"){if(ctrlKey===!0){li=$('a[accesskey="'+(a.keyCode==13?"\\n":String.fromCharCode(a.keyCode))+'"]',header).parent("li");if(li.length!==0)return ctrlKey=!1,setTimeout(function(){li.triggerHandler("mouseup")},1),!1}if(a.keyCode===13||a.keyCode===10)return ctrlKey===!0?(ctrlKey=!1,markup(options.onCtrlEnter),options.onCtrlEnter.keepDefault):shiftKey===!0?(shiftKey=!1,markup(options.onShiftEnter),options.onShiftEnter.keepDefault):(markup(options.onEnter),options.onEnter.keepDefault);if(a.keyCode===9)return shiftKey==1||ctrlKey==1||altKey==1?!1:caretOffset!==-1?(get(),caretOffset=$$.val().length-caretOffset,set(caretOffset,0),caretOffset=-1,!1):(markup(options.onTab),options.onTab.keepDefault)}}function remove(){$$.unbind(".markItUp").removeClass("markItUpEditor"),$$.parent("div").parent("div.markItUp").parent("div").replaceWith($$),$$.data("markItUp",null)}var $$,textarea,levels,scrollPosition,caretPosition,caretOffset,clicked,hash,header,footer,previewWindow,template,iFrame,abort;$$=$(this),textarea=this,levels=[],abort=!1,scrollPosition=caretPosition=0,caretOffset=-1,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}init()})},$.fn.markItUpRemove=function(){return this.each(function(){$(this).markItUp("remove")})},$.markItUp=function(a){var b={target:!1};$.extend(b,a);if(b.target)return $(b.target).each(function(){$(this).focus(),$(this).trigger("insertion",[b])});$("textarea").trigger("insertion",[b])}})(jQuery);