gremmie@1: // ---------------------------------------------------------------------------- gremmie@1: // markItUp! Universal MarkUp Engine, JQuery plugin bgneal@185: // v 1.1.6.1 gremmie@1: // Dual licensed under the MIT and GPL licenses. gremmie@1: // ---------------------------------------------------------------------------- bgneal@185: // Copyright (C) 2007-2010 Jay Salvat gremmie@1: // http://markitup.jaysalvat.com/ gremmie@1: // ---------------------------------------------------------------------------- bgneal@185: (function($){$.fn.markItUp=function(settings,extraSettings){var options,ctrlKey,shiftKey,altKey;ctrlKey=shiftKey=altKey=false;options={id:'',nameSpace:'',root:'',previewInWindow:'',previewAutoRefresh:true,previewPosition:'after',previewTemplatePath:'~/templates/preview.html',previewParserPath:'',previewParserVar:'data',resizeHandle:true,beforeInsert:'',afterInsert:'',onEnter:{},onShiftEnter:{},onCtrlEnter:{},onTab:{},markupSet:[{}]};$.extend(options,settings,extraSettings);if(!options.root){$('script').each(function(a,tag){miuScript=$(tag).get(0).src.match(/(.*)jquery\.markitup(\.pack)?\.js$/);if(miuScript!==null){options.root=miuScript[1]}})}return this.each(function(){var $$,textarea,levels,scrollPosition,caretPosition,caretOffset,clicked,hash,header,footer,previewWindow,template,iFrame,abort;$$=$(this);textarea=this;levels=[];abort=false;scrollPosition=caretPosition=0;caretOffset=-1;options.previewParserPath=localize(options.previewParserPath);options.previewTemplatePath=localize(options.previewTemplatePath);function localize(data,inText){if(inText){return data.replace(/("|')~\//g,"$1"+options.root)}return data.replace(/^~\//,options.root)}function init(){id='';nameSpace='';if(options.id){id='id="'+options.id+'"'}else if($$.attr("id")){id='id="markItUp'+($$.attr("id").substr(0,1).toUpperCase())+($$.attr("id").substr(1))+'"'}if(options.nameSpace){nameSpace='class="'+options.nameSpace+'"'}$$.wrap('
');$$.wrap('
');$$.wrap('
');$$.addClass("markItUpEditor");header=$('
').insertBefore($$);$(dropMenus(options.markupSet)).appendTo(header);footer=$('
').insertAfter($$);if(options.resizeHandle===true&&$.browser.safari!==true){resizeHandle=$('
').insertAfter($$).bind("mousedown",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);return false};$("html").bind("mousemove",mouseMove).bind("mouseup",mouseUp)});footer.append(resizeHandle)}$$.keydown(keyPressed).keyup(keyPressed);$$.bind("insertion",function(e,settings){if(settings.target!==false){get()}if(textarea===$.markItUp.focused){markup(settings)}});$$.focus(function(){$.markItUp.focused=this})}function dropMenus(markupSet){var ul=$(''),i=0;$('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=$('
  • '+(button.separator||'')+'
  • ').appendTo(ul)}else{i++;for(j=levels.length-1;j>=0;j--){t+=levels[j]+"-"}li=$('
  • '+(button.name||'')+'
  • ').bind("contextmenu",function(){return false}).click(function(){return false}).mouseup(function(){if(button.call){eval(button.call)()}markup(button);return false}).hover(function(){$('> ul',this).show();$(document).one('click',function(){$('ul ul',header).hide()})},function(){$('> ul',this).hide()}).appendTo(ul);if(button.dropMenu){levels.push(i);$(li).addClass('markItUpDropMenu').append(dropMenus(button.dropMenu))}}});levels.pop();return ul}function magicMarkups(string){if(string){string=string.toString();string=string.replace(/\(\!\(([\s\S]*?)\)\!\)/g,function(x,a){var b=a.split('|!|');if(altKey===true){return(b[1]!==undefined)?b[1]:b[0]}else{return(b[1]===undefined)?"":b[0]}});string=string.replace(/\[\!\[([\s\S]*?)\]\!\]/g,function(x,a){var b=a.split(':!:');if(abort===true){return false}value=prompt(b[0],(b[1])?b[1]:'');if(value===null){abort=true}return value});return string}return""}function prepare(action){if($.isFunction(action)){action=action(hash)}return magicMarkups(action)}function build(string){openWith=prepare(clicked.openWith);placeHolder=prepare(clicked.placeHolder);replaceWith=prepare(clicked.replaceWith);closeWith=prepare(clicked.closeWith);if(replaceWith!==""){block=openWith+replaceWith+closeWith}else if(selection===''&&placeHolder!==''){block=openWith+placeHolder+closeWith}else{block=openWith+(string||selection)+closeWith}return{block:block,openWith:openWith,replaceWith:replaceWith,placeHolder:placeHolder,closeWith:closeWith}}function markup(button){var len,j,n,i;hash=clicked=button;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);if(ctrlKey===true&&shiftKey===true){prepare(clicked.beforeMultiInsert)}$.extend(hash,{line:1});if(ctrlKey===true&&shiftKey===true){lines=selection.split(/\r?\n/);for(j=0,n=lines.length,i=0;i=9.5&&len==0){return false}range=textarea.createTextRange();range.collapse(true);range.moveStart('character',start);range.moveEnd('character',len);range.select()}else if(textarea.setSelectionRange){textarea.setSelectionRange(start,start+len)}textarea.scrollTop=scrollPosition;textarea.focus()}function get(){textarea.focus();scrollPosition=textarea.scrollTop;if(document.selection){selection=document.selection.createRange().text;if($.browser.msie){var range=document.selection.createRange(),rangeCopy=range.duplicate();rangeCopy.moveToElementText(textarea);caretPosition=-1;while(rangeCopy.inRange(range)){rangeCopy.moveStart('character');caretPosition++}}else{caretPosition=textarea.selectionStart}}else{caretPosition=textarea.selectionStart;selection=$$.val().substring(caretPosition,textarea.selectionEnd)}return selection}function preview(){if(!previewWindow||previewWindow.closed){if(options.previewInWindow){previewWindow=window.open('','preview',options.previewInWindow)}else{iFrame=$('');if(options.previewPosition=='after'){iFrame.insertAfter(footer)}else{iFrame.insertBefore(header)}previewWindow=iFrame[iFrame.length-1].contentWindow||frame[iFrame.length-1]}}else if(altKey===true){if(iFrame){iFrame.remove()}else{previewWindow.close()}previewWindow=iFrame=false}if(!options.previewAutoRefresh){refreshPreview()}}function refreshPreview(){renderPreview()}function renderPreview(){var phtml;if(options.previewParserPath!==''){$.ajax({type:'POST',url:options.previewParserPath,data:options.previewParserVar+'='+encodeURIComponent($$.val()),success:function(data){writeInPreview(localize(data,1))}})}else{if(!template){$.ajax({url:options.previewTemplatePath,success:function(data){writeInPreview(localize(data,1).replace(//g,$$.val()))}})}}return false}function writeInPreview(data){if(previewWindow.document){try{sp=previewWindow.document.documentElement.scrollTop}catch(e){sp=0}var h="test";previewWindow.document.open();previewWindow.document.write(data);previewWindow.document.close();previewWindow.document.documentElement.scrollTop=sp}if(options.previewInWindow){previewWindow.focus()}}function keyPressed(e){shiftKey=e.shiftKey;altKey=e.altKey;ctrlKey=(!(e.altKey&&e.ctrlKey))?e.ctrlKey:false;if(e.type==='keydown'){if(ctrlKey===true){li=$("a[accesskey="+String.fromCharCode(e.keyCode)+"]",header).parent('li');if(li.length!==0){ctrlKey=false;li.triggerHandler('mouseup');return false}}if(e.keyCode===13||e.keyCode===10){if(ctrlKey===true){ctrlKey=false;markup(options.onCtrlEnter);return options.onCtrlEnter.keepDefault}else if(shiftKey===true){shiftKey=false;markup(options.onShiftEnter);return options.onShiftEnter.keepDefault}else{markup(options.onEnter);return options.onEnter.keepDefault}}if(e.keyCode===9){if(shiftKey==true||ctrlKey==true||altKey==true){return false}if(caretOffset!==-1){get();caretOffset=$$.val().length-caretOffset;set(caretOffset,0);caretOffset=-1;return false}else{markup(options.onTab);return options.onTab.keepDefault}}}}init()})};$.fn.markItUpRemove=function(){return this.each(function(){$$=$(this).unbind().removeClass('markItUpEditor');$$.parent('div').parent('div.markItUp').parent('div').replaceWith($$)})};$.markItUp=function(settings){var options={target:false};$.extend(options,settings);if(options.target){return $(options.target).each(function(){$(this).focus();$(this).trigger('insertion',[options])})}else{$('textarea').trigger('insertion',[options])}}})(jQuery);