jquery.sceditor.js 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724
  1. /**
  2. * SCEditor
  3. * http://www.samclarke.com/2011/07/sceditor/
  4. *
  5. * Copyright (C) 2011-2012, Sam Clarke (samclarke.com)
  6. *
  7. * SCEditor is licensed under the MIT license:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * @fileoverview SCEditor - A lightweight WYSIWYG BBCode and HTML editor
  11. * @author Sam Clarke
  12. * @version 1.3.7
  13. * @requires jQuery
  14. */
  15. // ==ClosureCompiler==
  16. // @output_file_name jquery.sceditor.min.js
  17. // @compilation_level SIMPLE_OPTIMIZATIONS
  18. // ==/ClosureCompiler==
  19. /*jshint smarttabs: true, scripturl: true, jquery: true, devel:true, eqnull:true, curly: false */
  20. /*global XMLSerializer: true*/
  21. ;(function ($, window, document) {
  22. 'use strict';
  23. var _templates = {
  24. html: '<!DOCTYPE html>' +
  25. '<html>' +
  26. '<head>' +
  27. '<!--[if IE]><style>* {min-height: auto !important}</style><![endif]-->' +
  28. '<meta http-equiv="Content-Type" content="text/html;charset={charset}" />' +
  29. '<link rel="stylesheet" type="text/css" href="{style}" />' +
  30. '</head>' +
  31. '<body contenteditable="true"></body>' +
  32. '</html>',
  33. toolbarButton: '<a class="sceditor-button sceditor-button-{name}" data-sceditor-command="{name}" unselectable="on"><div unselectable="on">{dispName}</div></a>',
  34. emoticon: '<img src="{url}" data-sceditor-emoticon="{key}" alt="{key}" />',
  35. fontOpt: '<a class="sceditor-font-option" href="#" data-font="{font}"><font face="{font}">{font}</font></a>',
  36. sizeOpt: '<a class="sceditor-fontsize-option" data-size="{size}" style="line-height:{points}pt" href="#"><font size="{size}">{size}</font></a>',
  37. pastetext: '<div><label for="txt">{label}</label> ' +
  38. '<textarea cols="20" rows="7" id="txt"></textarea></div>' +
  39. '<div><input type="button" class="button" value="{insert}" /></div>',
  40. table: '<div><label for="rows">{rows}</label><input type="text" id="rows" value="2" /></div>' +
  41. '<div><label for="cols">{cols}</label><input type="text" id="cols" value="2" /></div>' +
  42. '<div><input type="button" class="button" value="{insert}" /></div>',
  43. image: '<div><label for="link">{url}</label> <input type="text" id="image" value="http://" /></div>' +
  44. '<div><label for="width">{width}</label> <input type="text" id="width" size="2" /></div>' +
  45. '<div><label for="height">{height}</label> <input type="text" id="height" size="2" /></div>' +
  46. '<div><input type="button" class="button" value="{insert}" /></div>',
  47. email: '<div><label for="email">{label}</label> <input type="text" id="email" /></div>' +
  48. '<div><input type="button" class="button" value="{insert}" /></div>',
  49. link: '<div><label for="link">{url}</label> <input type="text" id="link" value="http://" /></div>' +
  50. '<div><label for="des">{desc}</label> <input type="text" id="des" /></div>' +
  51. '<div><input type="button" class="button" value="{ins}" /></div>',
  52. youtubeMenu: '<div><label for="link">{label}</label> <input type="text" id="link" value="http://" /></div><div><input type="button" class="button" value="{insert}" /></div>',
  53. youtube: '<iframe width="560" height="315" src="http://www.youtube.com/embed/{id}?wmode=opaque" data-youtube-id="{id}" frameborder="0" allowfullscreen></iframe>'
  54. };
  55. /**
  56. * <p>Replaces any params in a template with the passed params.</p>
  57. *
  58. * <p>If createHTML is passed it will use jQuery to create the HTML. The
  59. * same as doing: $(editor.tmpl("html", {params...}));</p>
  60. *
  61. * @param {string} templateName
  62. * @param {Object} params
  63. * @param {Boolean} createHTML
  64. * @private
  65. */
  66. var _tmpl = function(name, params, createHTML) {
  67. var template = _templates[name];
  68. $.each(params, function(name, val) {
  69. template = template.replace(new RegExp('\\{' + name + '\\}', 'g'), val);
  70. });
  71. if(createHTML)
  72. template = $(template);
  73. return template;
  74. };
  75. /**
  76. * SCEditor - A lightweight WYSIWYG editor
  77. *
  78. * @param {Element} el The textarea to be converted
  79. * @return {Object} options
  80. * @class sceditor
  81. * @name jQuery.sceditor
  82. */
  83. $.sceditor = function (el, options) {
  84. /**
  85. * Alias of this
  86. * @private
  87. */
  88. var base = this;
  89. /**
  90. * The textarea element being replaced
  91. * @private
  92. */
  93. var $textarea = $(el);
  94. var textarea = el;
  95. /**
  96. * The div which contains the editor and toolbar
  97. * @private
  98. */
  99. var $editorContainer;
  100. /**
  101. * The editors toolbar
  102. * @private
  103. */
  104. var $toolbar;
  105. /**
  106. * The editors iframe which should be in design mode
  107. * @private
  108. */
  109. var $wysiwygEditor;
  110. var wysiwygEditor;
  111. /**
  112. * The editors textarea for viewing source
  113. * @private
  114. */
  115. var $textEditor;
  116. var textEditor;
  117. /**
  118. * The current dropdown
  119. * @private
  120. */
  121. var $dropdown;
  122. /**
  123. * Array of all the commands key press functions
  124. * @private
  125. */
  126. var keyPressFuncs = [];
  127. /**
  128. * Store the last cursor position. Needed for IE because it forgets
  129. * @private
  130. */
  131. var lastRange;
  132. /**
  133. * The editors locale
  134. * @private
  135. */
  136. var locale;
  137. /**
  138. * Stores a cache of preloaded images
  139. * @private
  140. */
  141. var preLoadCache = [];
  142. var rangeHelper;
  143. var $blurElm;
  144. var init,
  145. replaceEmoticons,
  146. handleCommand,
  147. saveRange,
  148. handlePasteEvt,
  149. handlePasteData,
  150. handleKeyPress,
  151. handleFormReset,
  152. handleMouseDown,
  153. initEditor,
  154. initToolBar,
  155. initKeyPressFuncs,
  156. initResize,
  157. documentClickHandler,
  158. formSubmitHandler,
  159. initEmoticons,
  160. getWysiwygDoc,
  161. handleWindowResize,
  162. initLocale,
  163. updateToolBar,
  164. textEditorSelectedText,
  165. autofocus;
  166. /**
  167. * All the commands supported by the editor
  168. */
  169. base.commands = $.extend({}, (options.commands || $.sceditor.commands));
  170. /**
  171. * Initializer. Creates the editor iframe and textarea
  172. * @private
  173. * @name sceditor.init
  174. */
  175. init = function () {
  176. $textarea.data("sceditor", base);
  177. base.options = $.extend({}, $.sceditor.defaultOptions, options);
  178. // Load locale
  179. if(base.options.locale && base.options.locale !== "en")
  180. initLocale();
  181. // if either width or height are % based, add the resize handler to update the editor
  182. // when the window is resized
  183. var h = base.options.height, w = base.options.width;
  184. if((h && (h + "").indexOf("%") > -1) || (w && (w + "").indexOf("%") > -1))
  185. $(window).resize(handleWindowResize);
  186. $editorContainer = $('<div class="sceditor-container" />').insertAfter($textarea);
  187. // create the editor
  188. initToolBar();
  189. initEditor();
  190. initKeyPressFuncs();
  191. if(base.options.resizeEnabled)
  192. initResize();
  193. if(base.options.id)
  194. $editorContainer.attr('id', base.options.id);
  195. $(document).click(documentClickHandler);
  196. $(textarea.form)
  197. .attr('novalidate','novalidate')
  198. .bind("reset", handleFormReset)
  199. .submit(formSubmitHandler);
  200. // load any textarea value into the editor
  201. base.val($textarea.hide().val());
  202. /*
  203. // Pass the value though the getTextHandler if it is set so that
  204. // BBCode, ect. can be converted
  205. if(base.options.getTextHandler && base.options.supportedWysiwyg)
  206. {
  207. val = base.options.getTextHandler(val);
  208. base.setWysiwygEditorValue(val);
  209. }
  210. else
  211. {
  212. base.toggleTextMode();
  213. base.setTextareaValue(val);
  214. }
  215. */
  216. if(base.options.autofocus)
  217. autofocus();
  218. // force into source mode if is a browser that can't handle
  219. // full editing
  220. if(!$.sceditor.isWysiwygSupported())
  221. base.toggleTextMode();
  222. if(base.options.toolbar.indexOf('emoticon') !== -1)
  223. initEmoticons();
  224. // Can't use load event as it gets fired before CSS is loaded
  225. // in some browsers
  226. if(base.options.autoExpand)
  227. var interval = setInterval(function() {
  228. if (!document.readyState || document.readyState === "complete") {
  229. base.expandToContent();
  230. clearInterval(interval);
  231. }
  232. }, 10);
  233. };
  234. /**
  235. * Creates the editor iframe and textarea
  236. * @private
  237. */
  238. initEditor = function () {
  239. var $doc, $body;
  240. $textEditor = $('<textarea></textarea>').hide();
  241. $wysiwygEditor = $('<iframe frameborder="0"></iframe>');
  242. if(window.location.protocol === "https:")
  243. $wysiwygEditor.attr("src", "javascript:false");
  244. // add the editor to the HTML and store the editors element
  245. $editorContainer.append($wysiwygEditor).append($textEditor);
  246. wysiwygEditor = $wysiwygEditor[0];
  247. textEditor = $textEditor[0];
  248. base.width(base.options.width || $textarea.width());
  249. base.height(base.options.height || $textarea.height());
  250. getWysiwygDoc().open();
  251. getWysiwygDoc().write(_tmpl("html", {
  252. charset: base.options.charset,
  253. style: base.options.style
  254. }));
  255. getWysiwygDoc().close();
  256. base.readOnly(!!base.options.readOnly);
  257. $doc = $(getWysiwygDoc());
  258. $body = $doc.find("body");
  259. // Add IE version class to the HTML element so can apply
  260. // conditional styling without CSS hacks
  261. if($.sceditor.ie)
  262. $doc.find("html").addClass('ie' + $.sceditor.ie);
  263. // iframe overflow fix
  264. if(/iPhone|iPod|iPad| wosbrowser\//i.test(navigator.userAgent))
  265. $body.height('100%');
  266. // set the key press event
  267. $body.keypress(handleKeyPress);
  268. $doc.keypress(handleKeyPress)
  269. .mousedown(handleMouseDown)
  270. .bind("beforedeactivate keyup", saveRange)
  271. .focus(function() {
  272. lastRange = null;
  273. });
  274. if(base.options.rtl)
  275. {
  276. $body.attr('dir', 'rtl');
  277. $textEditor.attr('dir', 'rtl');
  278. }
  279. if(base.options.enablePasteFiltering)
  280. $body.bind("paste", handlePasteEvt);
  281. if(base.options.autoExpand)
  282. $doc.bind("keyup", base.expandToContent);
  283. rangeHelper = new $.sceditor.rangeHelper(wysiwygEditor.contentWindow);
  284. };
  285. /**
  286. * Creates the toolbar and appends it to the container
  287. * @private
  288. */
  289. initToolBar = function () {
  290. var $group, $button, buttons,
  291. i, x, buttonClick,
  292. groups = base.options.toolbar.split("|");
  293. buttonClick = function () {
  294. var self = $(this);
  295. if(!self.hasClass('disabled'))
  296. handleCommand(self, base.commands[self.data("sceditor-command")]);
  297. return false;
  298. };
  299. $toolbar = $('<div class="sceditor-toolbar" />');
  300. var rows = base.options.toolbar.split("||");
  301. for (var r=0; r < rows.length; r++) {
  302. var row = $('<div class="sceditor-row" />');
  303. var groups = rows[r].split("|"),
  304. buttons, accessibilityName, button, i;
  305. for (i=0; i < groups.length; i++) {
  306. $group = $('<div class="sceditor-group" />');
  307. buttons = groups[i].split(",");
  308. for (x=0; x < buttons.length; x++) {
  309. // the button must be a valid command otherwise ignore it
  310. if(!base.commands[buttons[x]])
  311. continue;
  312. $button = _tmpl("toolbarButton", {
  313. name: buttons[x],
  314. dispName: base.commands[buttons[x]].tooltip || buttons[x]
  315. }, true).click(buttonClick);
  316. if(base.commands[buttons[x]].hasOwnProperty("tooltip"))
  317. $button.attr('title', base._(base.commands[buttons[x]].tooltip));
  318. if(base.commands[buttons[x]].exec)
  319. $button.data('sceditor-wysiwygmode', true);
  320. else
  321. $button.addClass('disabled');
  322. if(base.commands[buttons[x]].txtExec)
  323. $button.data('sceditor-txtmode', true);
  324. $group.append($button);
  325. }
  326. row.append($group);
  327. }
  328. $toolbar.append(row);
  329. }
  330. // append the toolbar to the toolbarContainer option if given
  331. if(base.options.toolbarContainer)
  332. $(base.options.toolbarContainer).append($toolbar);
  333. else
  334. $editorContainer.append($toolbar);
  335. };
  336. /**
  337. * Autofocus the editor
  338. * @private
  339. */
  340. autofocus = function() {
  341. var doc = wysiwygEditor.contentWindow.document,
  342. body = doc.body, rng;
  343. if(!doc.createRange)
  344. return base.focus();
  345. if(!body.firstChild)
  346. return;
  347. rng = doc.createRange();
  348. rng.setStart(body.firstChild, 0);
  349. rng.setEnd(body.firstChild, 0);
  350. rangeHelper.selectRange(rng);
  351. body.focus();
  352. };
  353. /**
  354. * Gets the readOnly property of the editor
  355. *
  356. * @since 1.3.5
  357. * @function
  358. * @memberOf jQuery.sceditor.prototype
  359. * @name readOnly
  360. * @return {boolean}
  361. */
  362. /**
  363. * Sets the readOnly property of the editor
  364. *
  365. * @param {boolean} readOnly
  366. * @since 1.3.5
  367. * @function
  368. * @memberOf jQuery.sceditor.prototype
  369. * @name readOnly^2
  370. * @return {this}
  371. */
  372. base.readOnly = function(readOnly) {
  373. if(typeof readOnly !== 'boolean')
  374. return $textEditor.attr('readonly') === 'readonly';
  375. getWysiwygDoc().body.contentEditable = !readOnly;
  376. if(!readOnly)
  377. $textEditor.removeAttr('readonly');
  378. else
  379. $textEditor.attr('readonly', 'readonly');
  380. updateToolBar(readOnly);
  381. return this;
  382. };
  383. /**
  384. * Updates the toolbar to disable/enable the appropriate buttons
  385. * @private
  386. */
  387. updateToolBar = function(disable) {
  388. $toolbar.find('.sceditor-button').removeClass('disabled');
  389. $toolbar.find('.sceditor-button').each(function () {
  390. var button = $(this);
  391. if(disable === true)
  392. button.addClass('disabled');
  393. else if(base.inSourceMode() && !button.data('sceditor-txtmode'))
  394. button.addClass('disabled');
  395. else if (!base.inSourceMode() && !button.data('sceditor-wysiwygmode'))
  396. button.addClass('disabled');
  397. });
  398. };
  399. /**
  400. * Creates an array of all the key press functions
  401. * like emoticons, ect.
  402. * @private
  403. */
  404. initKeyPressFuncs = function () {
  405. $.each(base.commands, function (command, values) {
  406. if(values.keyPress)
  407. keyPressFuncs.push(values.keyPress);
  408. });
  409. };
  410. /**
  411. * Gets the width of the editor in px
  412. *
  413. * @since 1.3.5
  414. * @function
  415. * @memberOf jQuery.sceditor.prototype
  416. * @name width
  417. * @return {int}
  418. */
  419. /**
  420. * Sets the width of the editor
  421. *
  422. * @param {int} width Width in px
  423. * @since 1.3.5
  424. * @function
  425. * @memberOf jQuery.sceditor.prototype
  426. * @name width^2
  427. * @return {this}
  428. */
  429. base.width = function (width) {
  430. if(!width)
  431. return $editorContainer.width();
  432. $editorContainer.width(width);
  433. // fix the height and width of the textarea/iframe
  434. $wysiwygEditor.width(width);
  435. $wysiwygEditor.width(width + (width - $wysiwygEditor.outerWidth(true)));
  436. $textEditor.width(width);
  437. $textEditor.width(width + (width - $textEditor.outerWidth(true)));
  438. return this;
  439. };
  440. /**
  441. * Gets the height of the editor in px
  442. *
  443. * @since 1.3.5
  444. * @function
  445. * @memberOf jQuery.sceditor.prototype
  446. * @name height
  447. * @return {int}
  448. */
  449. /**
  450. * Sets the height of the editor
  451. *
  452. * @param {int} height Height in px
  453. * @since 1.3.5
  454. * @function
  455. * @memberOf jQuery.sceditor.prototype
  456. * @name height^2
  457. * @return {this}
  458. */
  459. base.height = function (height) {
  460. if(!height)
  461. return $editorContainer.height();
  462. $editorContainer.height(height);
  463. height -= !base.options.toolbarContainer ? $toolbar.outerHeight(true) : 0;
  464. // fix the height and width of the textarea/iframe
  465. $wysiwygEditor.height(height);
  466. $wysiwygEditor.height(height + (height - $wysiwygEditor.outerHeight(true)));
  467. $textEditor.height(height);
  468. $textEditor.height(height + (height - $textEditor.outerHeight(true)));
  469. return this;
  470. };
  471. /**
  472. * Expands the editor to the size of it's content
  473. *
  474. * @since 1.3.5
  475. * @param {Boolean} [ignoreMaxHeight=false]
  476. * @function
  477. * @name expandToContent
  478. * @memberOf jQuery.sceditor.prototype
  479. * @see #resizeToContent
  480. */
  481. base.expandToContent = function(ignoreMaxHeight) {
  482. var doc = getWysiwygDoc(),
  483. currentHeight = $editorContainer.height(),
  484. height = doc.body.scrollHeight || doc.documentElement.scrollHeight,
  485. padding = (currentHeight - $wysiwygEditor.height()),
  486. maxHeight = base.options.resizeMaxHeight || ((base.options.height || $textarea.height()) * 2);
  487. height += padding;
  488. if(ignoreMaxHeight !== true && height > maxHeight)
  489. height = maxHeight;
  490. if(height > currentHeight)
  491. base.height(height);
  492. };
  493. /**
  494. * Creates the resizer.
  495. * @private
  496. */
  497. initResize = function () {
  498. var $grip = $('<div class="sceditor-grip" />'),
  499. // cover is used to cover the editor iframe so document still gets mouse move events
  500. $cover = $('<div class="sceditor-resize-cover" />'),
  501. startX = 0,
  502. startY = 0,
  503. startWidth = 0,
  504. startHeight = 0,
  505. origWidth = $editorContainer.width(),
  506. origHeight = $editorContainer.height(),
  507. dragging = false,
  508. minHeight, maxHeight, minWidth, maxWidth, mouseMoveFunc;
  509. minHeight = base.options.resizeMinHeight || origHeight / 1.5;
  510. maxHeight = base.options.resizeMaxHeight || origHeight * 2.5;
  511. minWidth = base.options.resizeMinWidth || origWidth / 1.25;
  512. maxWidth = base.options.resizeMaxWidth || origWidth * 1.25;
  513. mouseMoveFunc = function (e) {
  514. var newHeight = startHeight + (e.pageY - startY),
  515. newWidth = startWidth + (e.pageX - startX);
  516. if (newWidth >= minWidth && (maxWidth < 0 || newWidth <= maxWidth))
  517. base.width(newWidth);
  518. if (newHeight >= minHeight && (maxHeight < 0 || newHeight <= maxHeight))
  519. base.height(newHeight);
  520. e.preventDefault();
  521. };
  522. $editorContainer.append($grip);
  523. $editorContainer.append($cover.hide());
  524. $grip.mousedown(function (e) {
  525. startX = e.pageX;
  526. startY = e.pageY;
  527. startWidth = $editorContainer.width();
  528. startHeight = $editorContainer.height();
  529. dragging = true;
  530. $editorContainer.addClass('resizing');
  531. $cover.show();
  532. $(document).bind('mousemove', mouseMoveFunc);
  533. e.preventDefault();
  534. });
  535. $(document).mouseup(function (e) {
  536. if(!dragging)
  537. return;
  538. dragging = false;
  539. $cover.hide();
  540. $editorContainer.removeClass('resizing');
  541. $(document).unbind('mousemove', mouseMoveFunc);
  542. e.preventDefault();
  543. });
  544. };
  545. /**
  546. * Handles the forms submit event
  547. * @private
  548. */
  549. formSubmitHandler = function(e) {
  550. base.updateTextareaValue();
  551. $(this).removeAttr('novalidate');
  552. if(this.checkValidity && !this.checkValidity())
  553. e.preventDefault();
  554. $(this).attr('novalidate','novalidate');
  555. base.blur();
  556. };
  557. /**
  558. * Destroys the editor, removing all elements and
  559. * event handlers.
  560. *
  561. * @function
  562. * @name destory
  563. * @memberOf jQuery.sceditor.prototype
  564. */
  565. base.destory = function () {
  566. $(document).unbind('click', documentClickHandler);
  567. $(window).unbind('resize', handleWindowResize);
  568. $(textarea.form).removeAttr('novalidate')
  569. .unbind('submit', formSubmitHandler)
  570. .unbind("reset", handleFormReset);
  571. $(getWysiwygDoc()).find('*').remove();
  572. $(getWysiwygDoc()).unbind("keypress mousedown beforedeactivate keyup focus paste keypress");
  573. $editorContainer.find('*').remove();
  574. $editorContainer.remove();
  575. $textarea.removeData("sceditor").removeData("sceditorbbcode").show();
  576. };
  577. /**
  578. * Preloads the emoticon images
  579. * Idea from: http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
  580. * @private
  581. */
  582. initEmoticons = function () {
  583. // prefix emoticon root to emoticon urls
  584. if(base.options.emoticonsRoot && base.options.emoticons)
  585. {
  586. $.each(base.options.emoticons, function (idx, emoticons) {
  587. $.each(emoticons, function (key, url) {
  588. base.options.emoticons[idx][key] = base.options.emoticonsRoot + url;
  589. });
  590. });
  591. }
  592. var emoticons = $.extend({}, base.options.emoticons.more, base.options.emoticons.dropdown, base.options.emoticons.hidden),
  593. emoticon;
  594. $.each(emoticons, function (key, url) {
  595. // In SMF an empty entry means a new line
  596. if (key == '')
  597. emoticon = document.createElement('br');
  598. else
  599. {
  600. emoticon = document.createElement('img');
  601. emoticon.src = url;
  602. }
  603. preLoadCache.push(emoticon);
  604. });
  605. };
  606. /**
  607. * Creates a menu item drop down
  608. *
  609. * @param HTMLElement menuItem The button to align the drop down with
  610. * @param string dropDownName Used for styling the dropown, will be a class sceditor-dropDownName
  611. * @param string content The HTML content of the dropdown
  612. * @param bool ieUnselectable If to add the unselectable attribute to all the contents elements. Stops IE from deselecting the text in the editor
  613. * @function
  614. * @name createDropDown
  615. * @memberOf jQuery.sceditor.prototype
  616. */
  617. base.createDropDown = function (menuItem, dropDownName, content, ieUnselectable) {
  618. base.closeDropDown();
  619. // IE needs unselectable attr to stop it from unselecting the text in the editor.
  620. // The editor can cope if IE does unselect the text it's just not nice.
  621. if(ieUnselectable !== false) {
  622. $(content).find(':not(input,textarea)')
  623. .filter(function() {
  624. return this.nodeType===1;
  625. })
  626. .attr('unselectable', 'on');
  627. }
  628. var css = {
  629. top: menuItem.offset().top,
  630. left: menuItem.offset().left
  631. };
  632. $.extend(css, base.options.dropDownCss);
  633. $dropdown = $('<div class="sceditor-dropdown sceditor-' + dropDownName + '" />')
  634. .css(css)
  635. .append(content)
  636. .appendTo($('body'))
  637. .click(function (e) {
  638. // stop clicks within the dropdown from being handled
  639. e.stopPropagation();
  640. });
  641. };
  642. /**
  643. * Handles any document click and closes the dropdown if open
  644. * @private
  645. */
  646. documentClickHandler = function (e) {
  647. // ignore right clicks
  648. if(e.which !== 3)
  649. base.closeDropDown();
  650. };
  651. handlePasteEvt = function(e) {
  652. var elm = getWysiwygDoc().body,
  653. checkCount = 0,
  654. pastearea = elm.ownerDocument.createElement('div'),
  655. prePasteContent = elm.ownerDocument.createDocumentFragment();
  656. rangeHelper.saveRange();
  657. document.body.appendChild(pastearea);
  658. if (e && e.clipboardData && e.clipboardData.getData)
  659. {
  660. var html, handled=true;
  661. if ((html = e.clipboardData.getData('text/html')) || (html = e.clipboardData.getData('text/plain')))
  662. pastearea.innerHTML = html;
  663. else
  664. handled = false;
  665. if(handled)
  666. {
  667. handlePasteData(elm, pastearea);
  668. if (e.preventDefault)
  669. {
  670. e.stopPropagation();
  671. e.preventDefault();
  672. }
  673. return false;
  674. }
  675. }
  676. while(elm.firstChild)
  677. prePasteContent.appendChild(elm.firstChild);
  678. function handlePaste(elm, pastearea) {
  679. if (elm.childNodes.length > 0)
  680. {
  681. while(elm.firstChild)
  682. pastearea.appendChild(elm.firstChild);
  683. while(prePasteContent.firstChild)
  684. elm.appendChild(prePasteContent.firstChild);
  685. handlePasteData(elm, pastearea);
  686. }
  687. else
  688. {
  689. // Allow max 25 checks before giving up.
  690. // Needed inscase empty input is posted or
  691. // something gose wrong.
  692. if(checkCount > 25)
  693. {
  694. while(prePasteContent.firstChild)
  695. elm.appendChild(prePasteContent.firstChild);
  696. return;
  697. }
  698. ++checkCount;
  699. setTimeout(function () {
  700. handlePaste(elm, pastearea);
  701. }, 20);
  702. }
  703. }
  704. handlePaste(elm, pastearea);
  705. base.focus();
  706. return true;
  707. };
  708. /**
  709. * @param {Element} elm
  710. * @param {Element} pastearea
  711. * @private
  712. */
  713. handlePasteData = function(elm, pastearea) {
  714. // fix any invalid nesting
  715. $.sceditor.dom.fixNesting(pastearea);
  716. var pasteddata = pastearea.innerHTML;
  717. if(base.options.getHtmlHandler)
  718. pasteddata = base.options.getHtmlHandler(pasteddata, $(pastearea));
  719. pastearea.parentNode.removeChild(pastearea);
  720. if(base.options.getTextHandler)
  721. pasteddata = base.options.getTextHandler(pasteddata, true);
  722. rangeHelper.restoreRange();
  723. rangeHelper.insertHTML(pasteddata);
  724. };
  725. /**
  726. * Closes the current drop down
  727. *
  728. * @param bool focus If to focus the editor on close
  729. * @function
  730. * @name closeDropDown
  731. * @memberOf jQuery.sceditor.prototype
  732. */
  733. base.closeDropDown = function (focus) {
  734. if($dropdown) {
  735. $dropdown.remove();
  736. $dropdown = null;
  737. }
  738. if(focus === true)
  739. base.focus();
  740. };
  741. /**
  742. * Gets the WYSIWYG editors document
  743. * @private
  744. */
  745. getWysiwygDoc = function () {
  746. if (wysiwygEditor.contentDocument)
  747. return wysiwygEditor.contentDocument;
  748. if (wysiwygEditor.contentWindow && wysiwygEditor.contentWindow.document)
  749. return wysiwygEditor.contentWindow.document;
  750. if (wysiwygEditor.document)
  751. return wysiwygEditor.document;
  752. return null;
  753. };
  754. /**
  755. * <p>Inserts HTML into WYSIWYG editor.</p>
  756. *
  757. * <p>If endHtml is specified instead of the inserted HTML replacing the selected
  758. * text the selected text will be placed between html and endHtml. If there is
  759. * no selected text html and endHtml will be concated together.</p>
  760. *
  761. * @param {string} html
  762. * @param {string} [endHtml=null]
  763. * @param {boolean} [overrideCodeBlocking=false]
  764. * @function
  765. * @name wysiwygEditorInsertHtml
  766. * @memberOf jQuery.sceditor.prototype
  767. */
  768. base.wysiwygEditorInsertHtml = function (html, endHtml, overrideCodeBlocking) {
  769. base.focus();
  770. // don't apply to code elements
  771. if(!overrideCodeBlocking && ($(rangeHelper.parentNode()).is('code') ||
  772. $(rangeHelper.parentNode()).parents('code').length !== 0))
  773. return;
  774. rangeHelper.insertHTML(html, endHtml);
  775. };
  776. /**
  777. * Like wysiwygEditorInsertHtml except it will convert any HTML into text
  778. * before inserting it.
  779. *
  780. * @param {String} text
  781. * @param {String} [endText=null]
  782. * @function
  783. * @name wysiwygEditorInsertText
  784. * @memberOf jQuery.sceditor.prototype
  785. */
  786. base.wysiwygEditorInsertText = function (text, endText) {
  787. var escape = function(str) {
  788. if(!str)
  789. return str;
  790. return str.replace(/&/g, "&amp;")
  791. .replace(/</g, "&lt;")
  792. .replace(/>/g, "&gt;")
  793. .replace(/ /g, "&nbsp;")
  794. .replace(/\r\n|\r/g, "\n")
  795. .replace(/\n/g, "<br />");
  796. };
  797. base.wysiwygEditorInsertHtml(escape(text), escape(endText));
  798. };
  799. /**
  800. * <p>Inserts text into either WYSIWYG or textEditor depending on which
  801. * mode the editor is in.</p>
  802. *
  803. * <p>If endText is specified any selected text will be placed between
  804. * text and endText. If no text is selected text and endText will
  805. * just be concated together.</p>
  806. *
  807. * @param {String} text
  808. * @param {String} [endText=null]
  809. * @since 1.3.5
  810. * @function
  811. * @name insertText
  812. * @memberOf jQuery.sceditor.prototype
  813. */
  814. base.insertText = function (text, endText) {
  815. if(base.inSourceMode())
  816. base.textEditorInsertText(text, endText);
  817. else
  818. base.wysiwygEditorInsertText(text, endText);
  819. return this;
  820. };
  821. /**
  822. * Like wysiwygEditorInsertHtml but inserts text into the text
  823. * (source mode) editor instead
  824. *
  825. * @param {String} text
  826. * @param {String} [endText=null]
  827. * @function
  828. * @name textEditorInsertText
  829. * @memberOf jQuery.sceditor.prototype
  830. */
  831. base.textEditorInsertText = function (text, endText) {
  832. var range, start, end, txtLen;
  833. textEditor.focus();
  834. if(typeof textEditor.selectionStart !== "undefined")
  835. {
  836. start = textEditor.selectionStart;
  837. end = textEditor.selectionEnd;
  838. txtLen = text.length;
  839. if(endText)
  840. text += textEditor.value.substring(start, end) + endText;
  841. textEditor.value = textEditor.value.substring(0, start) + text + textEditor.value.substring(end, textEditor.value.length);
  842. if(endText)
  843. textEditor.selectionStart = (start + text.length) - endText.length;
  844. else
  845. textEditor.selectionStart = start + text.length;
  846. textEditor.selectionEnd = textEditor.selectionStart;
  847. }
  848. else if(typeof document.selection.createRange !== "undefined")
  849. {
  850. range = document.selection.createRange();
  851. if(endText)
  852. text += range.text + endText;
  853. range.text = text;
  854. if(endText)
  855. range.moveEnd('character', 0-endText.length);
  856. range.moveStart('character', range.End - range.Start);
  857. range.select();
  858. }
  859. else
  860. textEditor.value += text + endText;
  861. textEditor.focus();
  862. };
  863. /**
  864. * Gets the current rangeHelper instance
  865. *
  866. * @return jQuery.sceditor.rangeHelper
  867. * @function
  868. * @name getRangeHelper
  869. * @memberOf jQuery.sceditor.prototype
  870. */
  871. base.getRangeHelper = function () {
  872. return rangeHelper;
  873. };
  874. /**
  875. * Gets the value of the editor
  876. *
  877. * @since 1.3.5
  878. * @return {string}
  879. * @function
  880. * @name val
  881. * @memberOf jQuery.sceditor.prototype
  882. */
  883. /**
  884. * Sets the value of the editor
  885. *
  886. * @param {String} val
  887. * @param {Boolean} [filter]
  888. * @return {this}
  889. * @since 1.3.5
  890. * @function
  891. * @name val^2
  892. * @memberOf jQuery.sceditor.prototype
  893. */
  894. base.val = function (val, filter) {
  895. if(typeof val === "string")
  896. {
  897. if(base.inSourceMode())
  898. base.setTextareaValue(val);
  899. else
  900. {
  901. if(filter !== false && base.options.getTextHandler)
  902. val = base.options.getTextHandler(val);
  903. base.setWysiwygEditorValue(val);
  904. }
  905. return this;
  906. }
  907. return base.inSourceMode() ?
  908. base.getTextareaValue(false) :
  909. base.getWysiwygEditorValue();
  910. };
  911. /**
  912. * <p>Inserts HTML/BBCode into the editor</p>
  913. *
  914. * <p>If end is supplied any slected text will be placed between
  915. * start and end. If there is no selected text start and end
  916. * will be concated together.</p>
  917. *
  918. * @param {String} start
  919. * @param {String} [end=null]
  920. * @param {Boolean} [filter=true]
  921. * @param {Boolean} [convertEmoticons=true]
  922. * @return {this}
  923. * @since 1.3.5
  924. * @function
  925. * @name insert
  926. * @memberOf jQuery.sceditor.prototype
  927. */
  928. base.insert = function (start, end, filter, convertEmoticons) {
  929. if(base.inSourceMode())
  930. base.textEditorInsertText(start, end);
  931. else
  932. {
  933. if(end)
  934. {
  935. var html = base.getRangeHelper().selectedHtml(),
  936. frag = $('<div>').appendTo($('body')).hide().html(html);
  937. if(filter !== false && base.options.getHtmlHandler)
  938. {
  939. html = base.options.getHtmlHandler(html, frag);
  940. frag.remove();
  941. }
  942. start += html + end;
  943. }
  944. if(filter !== false && base.options.getTextHandler)
  945. start = base.options.getTextHandler(start, true);
  946. if(convertEmoticons !== false)
  947. start = replaceEmoticons(start);
  948. base.wysiwygEditorInsertHtml(start);
  949. }
  950. return this;
  951. };
  952. /**
  953. * Gets the WYSIWYG editors HTML which is between the body tags
  954. *
  955. * @param {bool} [filter=true]
  956. * @return {string}
  957. * @function
  958. * @name getWysiwygEditorValue
  959. * @memberOf jQuery.sceditor.prototype
  960. */
  961. base.getWysiwygEditorValue = function (filter) {
  962. // Possible replacement:
  963. // if(!$.sceditor.isWysiwygSupported())
  964. //if (!base.options.supportedWysiwyg)
  965. // return;
  966. var $body = $wysiwygEditor.contents().find("body"),
  967. html;
  968. // fix any invalid nesting
  969. $.sceditor.dom.fixNesting($body.get(0));
  970. html = $body.html();
  971. if(filter !== false && base.options.getHtmlHandler)
  972. html = base.options.getHtmlHandler(html, $body, filter);
  973. return html;
  974. };
  975. /**
  976. * Gets the text editor value
  977. *
  978. * @param {bool} [filter=true]
  979. * @return {string}
  980. * @function
  981. * @name getTextareaValue
  982. * @memberOf jQuery.sceditor.prototype
  983. */
  984. base.getTextareaValue = function (filter) {
  985. var val = $textEditor.val();
  986. if(filter !== false && base.options.getTextHandler)
  987. val = base.options.getTextHandler(val);
  988. return val;
  989. };
  990. /**
  991. * Sets the WYSIWYG HTML editor value. Should only be the HTML
  992. * contained within the body tags
  993. *
  994. * @param {string} value
  995. * @function
  996. * @name setWysiwygEditorValue
  997. * @memberOf jQuery.sceditor.prototype
  998. */
  999. base.setWysiwygEditorValue = function (value) {
  1000. if(!value)
  1001. value = '<p>' + ($.sceditor.ie ? '' : '<br />') + '</p>';
  1002. getWysiwygDoc().body.innerHTML = replaceEmoticons(value);
  1003. };
  1004. /**
  1005. * Sets the text editor value
  1006. *
  1007. * @param {string} value
  1008. * @function
  1009. * @name setTextareaValue
  1010. * @memberOf jQuery.sceditor.prototype
  1011. */
  1012. base.setTextareaValue = function (value) {
  1013. $textEditor.val(value);
  1014. };
  1015. /**
  1016. * Updates the textarea that the editor is replacing
  1017. * with the value currently inside the editor.
  1018. *
  1019. * @function
  1020. * @name updateTextareaValue
  1021. * @memberOf jQuery.sceditor.prototype
  1022. */
  1023. base.updateTextareaValue = function () {
  1024. if(base.inSourceMode())
  1025. $textarea.val(base.getTextareaValue(false));
  1026. else
  1027. $textarea.val(base.getWysiwygEditorValue());
  1028. };
  1029. /**
  1030. * Replaces any emoticon codes in the passed HTML with their emoticon images
  1031. * @private
  1032. */
  1033. replaceEmoticons = function (html) {
  1034. if(base.options.toolbar.indexOf('emoticon') === -1)
  1035. return html;
  1036. var emoticons = $.extend({}, base.options.emoticons.more, base.options.emoticons.dropdown, base.options.emoticons.hidden);
  1037. $.each(emoticons, function (key, url) {
  1038. // In SMF an empty entry means a new line
  1039. if (key == '')
  1040. return;
  1041. // escape the key before using it as a regex
  1042. // and append the regex to only find emoticons outside
  1043. // of HTML tags
  1044. var reg = $.sceditor.regexEscape(key) + "(?=([^\\<\\>]*?<(?!/code)|[^\\<\\>]*?$))",
  1045. group = '';
  1046. // Make sure the emoticon is surrounded by whitespace or is at the start/end of a string or html tag
  1047. if(base.options.emoticonsCompat)
  1048. {
  1049. reg = "((>|^|\\s|\xA0|\u2002|\u2003|\u2009|&nbsp;))" + reg + "(?=(\\s|$|<|\xA0|\u2002|\u2003|\u2009|&nbsp;))";
  1050. group = '$1';
  1051. }
  1052. html = html.replace(
  1053. new RegExp(reg, 'gm'),
  1054. group + _tmpl('emoticon', {key: key, url: url})
  1055. );
  1056. });
  1057. return html;
  1058. };
  1059. /**
  1060. * If the editor is in source code mode
  1061. *
  1062. * @return {bool}
  1063. * @function
  1064. * @name inSourceMode
  1065. * @memberOf jQuery.sceditor.prototype
  1066. */
  1067. base.inSourceMode = function () {
  1068. return $textEditor.is(':visible');
  1069. };
  1070. /**
  1071. * Gets if the editor is in sourceMode
  1072. *
  1073. * @return boolean
  1074. * @function
  1075. * @name sourceMode
  1076. * @memberOf jQuery.sceditor.prototype
  1077. */
  1078. /**
  1079. * Sets if the editor is in sourceMode
  1080. *
  1081. * @param {bool} enable
  1082. * @return {this}
  1083. * @function
  1084. * @name sourceMode^2
  1085. * @memberOf jQuery.sceditor.prototype
  1086. */
  1087. base.sourceMode = function (enable) {
  1088. if(typeof enable !== 'boolean')
  1089. return base.inSourceMode();
  1090. if((base.inSourceMode() && !enable) || (!base.inSourceMode() && enable))
  1091. base.toggleTextMode();
  1092. return this;
  1093. };
  1094. /**
  1095. * Switches between the WYSIWYG and plain text modes
  1096. *
  1097. * @function
  1098. * @name toggleTextMode
  1099. * @memberOf jQuery.sceditor.prototype
  1100. */
  1101. base.toggleTextMode = function () {
  1102. // don't allow switching to WYSIWYG if doesn't support it
  1103. if(!$.sceditor.isWysiwygSupported() && base.inSourceMode())
  1104. return;
  1105. if(base.inSourceMode())
  1106. base.setWysiwygEditorValue(base.getTextareaValue());
  1107. else
  1108. base.setTextareaValue(base.getWysiwygEditorValue());
  1109. lastRange = null;
  1110. $textEditor.toggle();
  1111. $wysiwygEditor.toggle();
  1112. $editorContainer.removeClass('sourceMode');
  1113. $editorContainer.removeClass('wysiwygMode');
  1114. if(base.inSourceMode())
  1115. $editorContainer.addClass('sourceMode');
  1116. else
  1117. $editorContainer.addClass('wysiwygMode');
  1118. updateToolBar();
  1119. };
  1120. textEditorSelectedText = function () {
  1121. textEditor.focus();
  1122. if(textEditor.selectionStart != null)
  1123. return textEditor.value.substring(textEditor.selectionStart, textEditor.selectionEnd);
  1124. else if(document.selection.createRange)
  1125. return document.selection.createRange().text;
  1126. };
  1127. /**
  1128. * Handles the passed command
  1129. * @private
  1130. */
  1131. handleCommand = function (caller, command) {
  1132. // check if in text mode and handle text commands
  1133. if(base.inSourceMode())
  1134. {
  1135. if(command.txtExec)
  1136. {
  1137. if($.isArray(command.txtExec))
  1138. base.textEditorInsertText.apply(base, command.txtExec);
  1139. else
  1140. command.txtExec.call(base, caller, textEditorSelectedText());
  1141. }
  1142. return;
  1143. }
  1144. if(!command.exec)
  1145. return;
  1146. if($.isFunction(command.exec))
  1147. command.exec.call(base, caller);
  1148. else
  1149. base.execCommand(command.exec, command.hasOwnProperty("execParam") ? command.execParam : null);
  1150. };
  1151. /**
  1152. * Fucuses the editors input area
  1153. *
  1154. * @return {this}
  1155. * @function
  1156. * @name focus
  1157. * @memberOf jQuery.sceditor.prototype
  1158. */
  1159. base.focus = function () {
  1160. if(!base.inSourceMode())
  1161. {
  1162. wysiwygEditor.contentWindow.focus();
  1163. // Needed for IE < 9
  1164. if(lastRange) {
  1165. rangeHelper.selectRange(lastRange);
  1166. // remove the stored range after being set.
  1167. // If the editor loses focus it should be
  1168. // saved again.
  1169. lastRange = null;
  1170. }
  1171. }
  1172. else
  1173. textEditor.focus();
  1174. return this;
  1175. };
  1176. /**
  1177. * Blurs the editors input area
  1178. *
  1179. * @return {this}
  1180. * @function
  1181. * @name blur
  1182. * @memberOf jQuery.sceditor.prototype
  1183. * @since 1.3.6
  1184. */
  1185. base.blur = function () {
  1186. // Must use an element that isn't display:hidden or visibility:hidden for iOS
  1187. // so create a special blur element to use
  1188. if(!$blurElm)
  1189. $blurElm = $('<input style="width:0; height:0; opacity:0; filter: alpha(opacity=0)" type="text" />').appendTo($editorContainer);
  1190. $blurElm.removeAttr("disabled")
  1191. .focus()
  1192. .blur()
  1193. .attr("disabled", "disabled");
  1194. return this;
  1195. };
  1196. /**
  1197. * Saves the current range. Needed for IE because it forgets
  1198. * where the cursor was and what was selected
  1199. * @private
  1200. */
  1201. saveRange = function () {
  1202. /* this is only needed for IE */
  1203. if(!$.sceditor.ie)
  1204. return;
  1205. lastRange = rangeHelper.selectedRange();
  1206. };
  1207. /**
  1208. * Executes a command on the WYSIWYG editor
  1209. *
  1210. * @param {String|Function} command
  1211. * @param {String|Boolean} [param]
  1212. * @function
  1213. * @name execCommand
  1214. * @memberOf jQuery.sceditor.prototype
  1215. */
  1216. base.execCommand = function (command, param) {
  1217. var executed = false,
  1218. $parentNode = $(rangeHelper.parentNode());
  1219. base.focus();
  1220. // don't apply any comannds to code elements
  1221. if($parentNode.is('code') || $parentNode.parents('code').length !== 0)
  1222. return;
  1223. if(getWysiwygDoc())
  1224. {
  1225. try
  1226. {
  1227. executed = getWysiwygDoc().execCommand(command, false, param);
  1228. }
  1229. catch (e) {}
  1230. }
  1231. // show error if execution failed and an error message exists
  1232. if(!executed && base.commands[command] && base.commands[command].errorMessage)
  1233. alert(base._(base.commands[command].errorMessage));
  1234. };
  1235. /**
  1236. * Handles any key press in the WYSIWYG editor
  1237. *
  1238. * @private
  1239. */
  1240. handleKeyPress = function(e) {
  1241. base.closeDropDown();
  1242. var $parentNode = $(rangeHelper.parentNode());
  1243. // "Fix" (ok it's a cludge) for blocklevel elements being duplicated in some browsers when
  1244. // enter is pressed instead of inserting a newline
  1245. if(e.which === 13)
  1246. {
  1247. if($parentNode.is('code,blockquote,pre') || $parentNode.parents('code,blockquote,pre').length !== 0)
  1248. {
  1249. lastRange = null;
  1250. base.wysiwygEditorInsertHtml('<br />', null, true);
  1251. return false;
  1252. }
  1253. }
  1254. // make sure there is always a newline after code or quote tags
  1255. var d = getWysiwygDoc();
  1256. $.sceditor.dom.rTraverse(d.body, function(node) {
  1257. if((node.nodeType === 3 && node.nodeValue !== "") ||
  1258. node.nodeName.toLowerCase() === 'br') {
  1259. // this is the last text or br node, if its in a code or quote tag
  1260. // then add a newline after it
  1261. if($(node).parents('code, blockquote').length > 0)
  1262. $(d.body).append(d.createElement('br'));
  1263. return false;
  1264. }
  1265. }, true);
  1266. // don't apply to code elements
  1267. if($parentNode.is('code') || $parentNode.parents('code').length !== 0)
  1268. return;
  1269. var i = keyPressFuncs.length;
  1270. while(i--)
  1271. keyPressFuncs[i].call(base, e, wysiwygEditor, $textEditor);
  1272. };
  1273. /**
  1274. * Handles any mousedown press in the WYSIWYG editor
  1275. * @private
  1276. */
  1277. handleFormReset = function() {
  1278. base.val($textarea.val());
  1279. };
  1280. /**
  1281. * Handles any mousedown press in the WYSIWYG editor
  1282. * @private
  1283. */
  1284. handleMouseDown = function() {
  1285. base.closeDropDown();
  1286. lastRange = null;
  1287. };
  1288. /**
  1289. * Handles the window resize event. Needed to resize then editor
  1290. * when the window size changes in fluid deisgns.
  1291. * @ignore
  1292. */
  1293. handleWindowResize = function() {
  1294. if(base.options.height && base.options.height.toString().indexOf("%") > -1)
  1295. base.height($editorContainer.parent().height() *
  1296. (parseFloat(base.options.height) / 100));
  1297. if(base.options.width && base.options.width.toString().indexOf("%") > -1)
  1298. base.width($editorContainer.parent().width() *
  1299. (parseFloat(base.options.width) / 100));
  1300. };
  1301. /**
  1302. * Translates the string into the locale language.
  1303. *
  1304. * Replaces any {0}, {1}, {2}, ect. with the params provided.
  1305. *
  1306. * @param {string} str
  1307. * @param {...String} args
  1308. * @return {string}
  1309. * @function
  1310. * @name _
  1311. * @memberOf jQuery.sceditor.prototype
  1312. */
  1313. base._ = function() {
  1314. var args = arguments;
  1315. if(locale && locale[args[0]])
  1316. args[0] = locale[args[0]];
  1317. return args[0].replace(/\{(\d+)\}/g, function(str, p1) {
  1318. return typeof args[p1-0+1] !== "undefined" ?
  1319. args[p1-0+1] :
  1320. '{' + p1 + '}';
  1321. });
  1322. };
  1323. /**
  1324. * Init the locale variable with the specified locale if possible
  1325. * @private
  1326. * @return void
  1327. */
  1328. initLocale = function() {
  1329. if($.sceditor.locale[base.options.locale])
  1330. locale = $.sceditor.locale[base.options.locale];
  1331. else
  1332. {
  1333. var lang = base.options.locale.split("-");
  1334. if($.sceditor.locale[lang[0]])
  1335. locale = $.sceditor.locale[lang[0]];
  1336. }
  1337. if(locale && locale.dateFormat)
  1338. base.options.dateFormat = locale.dateFormat;
  1339. };
  1340. // run the initializer
  1341. init();
  1342. };
  1343. /**
  1344. * Detects which version of IE is being used if any.
  1345. *
  1346. * Will be the IE version number or undefined if not IE.
  1347. *
  1348. * Source: https://gist.github.com/527683
  1349. * @type {int}
  1350. * @memberOf jQuery.sceditor
  1351. */
  1352. $.sceditor.ie = (function(){
  1353. var undef,
  1354. v = 3,
  1355. div = document.createElement('div'),
  1356. all = div.getElementsByTagName('i');
  1357. do {
  1358. div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->';
  1359. } while (all[0]);
  1360. return v > 4 ? v : undef;
  1361. }());
  1362. /**
  1363. * Detects if WYSIWYG is supported by the browser
  1364. *
  1365. * @return {bool}
  1366. * @memberOf jQuery.sceditor
  1367. */
  1368. $.sceditor.isWysiwygSupported = function() {
  1369. var contentEditable = $('<div contenteditable="true">')[0].contentEditable,
  1370. contentEditableSupported = typeof contentEditable !== 'undefined' && contentEditable !== 'inherit',
  1371. userAgent = navigator.userAgent,
  1372. match;
  1373. if(!contentEditableSupported)
  1374. return false;
  1375. // I think blackberry supports it or will at least
  1376. // give a valid value for the contentEditable detection above
  1377. // so it's' not included here.
  1378. // The latest WebOS dose support contentEditable.
  1379. // But I still till need to check if all supported
  1380. // versions of WebOS support contentEditable
  1381. // I hate having to use UA sniffing but as some mobile browsers say they support
  1382. // contentediable/design mode when it isn't usable (i.e. you can't eneter text, ect.).
  1383. // This is the only way I can think of to detect them. It's also how every other editor
  1384. // I've seen detects them
  1385. var isUnsupported = /Opera Mobi|Opera Mini/i.test(userAgent);
  1386. if(/Android/i.test(userAgent))
  1387. {
  1388. isUnsupported = true;
  1389. if(/Safari/.test(userAgent))
  1390. {
  1391. // android browser 534+ supports content editable
  1392. match = /Safari\/(\d+)/.exec(userAgent);
  1393. isUnsupported = (!match || !match[1] ? true : match[1] < 534);
  1394. }
  1395. }
  1396. // Amazon Silk doesn't but as it uses webkit like android
  1397. // it might in a later version if it uses version >= 534
  1398. if(/ Silk\//i.test(userAgent))
  1399. {
  1400. match = /AppleWebKit\/(\d+)/.exec(userAgent);
  1401. isUnsupported = (!match || !match[1] ? true : match[1] < 534);
  1402. }
  1403. // iOS 5+ supports content editable
  1404. if(/iPhone|iPod|iPad/i.test(userAgent))
  1405. isUnsupported = !/OS 5(_\d)+ like Mac OS X/i.test(userAgent);
  1406. // FireFox dose support WYSIWYG on mobiles so override
  1407. // any previous value if using FF
  1408. if(/fennec/i.test(userAgent))
  1409. isUnsupported = false;
  1410. return !isUnsupported;
  1411. };
  1412. /**
  1413. * Escapes a string so it's safe to use in regex
  1414. *
  1415. * @param {string} str
  1416. * @return {string}
  1417. * @memberOf jQuery.sceditor
  1418. */
  1419. $.sceditor.regexEscape = function (str) {
  1420. return str.replace(/[\$\?\[\]\.\*\(\)\|\\]/g, "\\$&")
  1421. .replace("<", "&lt;")
  1422. .replace(">", "&gt;");
  1423. };
  1424. $.sceditor.locale = {};
  1425. $.sceditor.commands = {
  1426. // START_COMMAND: Bold
  1427. bold: {
  1428. exec: "bold",
  1429. tooltip: "Bold"
  1430. },
  1431. // END_COMMAND
  1432. // START_COMMAND: Italic
  1433. italic: {
  1434. exec: "italic",
  1435. tooltip: "Italic"
  1436. },
  1437. // END_COMMAND
  1438. // START_COMMAND: Underline
  1439. underline: {
  1440. exec: "underline",
  1441. tooltip: "Underline"
  1442. },
  1443. // END_COMMAND
  1444. // START_COMMAND: Strikethrough
  1445. strike: {
  1446. exec: "strikethrough",
  1447. tooltip: "Strikethrough"
  1448. },
  1449. // END_COMMAND
  1450. // START_COMMAND: Subscript
  1451. subscript: {
  1452. exec: "subscript",
  1453. tooltip: "Subscript"
  1454. },
  1455. // END_COMMAND
  1456. // START_COMMAND: Superscript
  1457. superscript: {
  1458. exec: "superscript",
  1459. tooltip: "Superscript"
  1460. },
  1461. // END_COMMAND
  1462. // START_COMMAND: Left
  1463. left: {
  1464. exec: "justifyleft",
  1465. tooltip: "Align left"
  1466. },
  1467. // END_COMMAND
  1468. // START_COMMAND: Centre
  1469. center: {
  1470. exec: "justifycenter",
  1471. tooltip: "Center"
  1472. },
  1473. // END_COMMAND
  1474. // START_COMMAND: Right
  1475. right: {
  1476. exec: "justifyright",
  1477. tooltip: "Align right"
  1478. },
  1479. // END_COMMAND
  1480. // START_COMMAND: Justify
  1481. justify: {
  1482. exec: "justifyfull",
  1483. tooltip: "Justify"
  1484. },
  1485. // END_COMMAND
  1486. // START_COMMAND: Font
  1487. font: {
  1488. _dropDown: function(editor, caller, callback) {
  1489. var fonts = editor.options.fonts.split(","),
  1490. content = $("<div />"),
  1491. /** @private */
  1492. clickFunc = function () {
  1493. callback($(this).data('font'));
  1494. editor.closeDropDown(true);
  1495. return false;
  1496. };
  1497. for (var i=0; i < fonts.length; i++)
  1498. content.append(_tmpl('fontOpt', {font: fonts[i]}, true).click(clickFunc));
  1499. editor.createDropDown(caller, "font-picker", content);
  1500. },
  1501. exec: function (caller) {
  1502. var editor = this;
  1503. $.sceditor.command.get('font')._dropDown(
  1504. editor,
  1505. caller,
  1506. function(fontName) {
  1507. editor.execCommand("fontname", fontName);
  1508. }
  1509. );
  1510. },
  1511. tooltip: "Font Name"
  1512. },
  1513. // END_COMMAND
  1514. // START_COMMAND: Size
  1515. size: {
  1516. _dropDown: function(editor, caller, callback) {
  1517. var sizes = [0, 8, 10, 12, 14, 18, 24, 36];
  1518. var content = $("<div />"),
  1519. /** @private */
  1520. clickFunc = function (e) {
  1521. callback($(this).data('size'));
  1522. editor.closeDropDown(true);
  1523. e.preventDefault();
  1524. };
  1525. for (var i=1; i<= 7; i++)
  1526. content.append(_tmpl('sizeOpt', {size: i, points: sizes[i]}, true).click(clickFunc));
  1527. editor.createDropDown(caller, "fontsize-picker", content);
  1528. },
  1529. exec: function (caller) {
  1530. var editor = this;
  1531. $.sceditor.command.get('size')._dropDown(
  1532. editor,
  1533. caller,
  1534. function(fontSize) {
  1535. editor.execCommand("fontsize", fontSize);
  1536. }
  1537. );
  1538. },
  1539. tooltip: "Font Size"
  1540. },
  1541. // END_COMMAND
  1542. // START_COMMAND: Colour
  1543. color: {
  1544. _dropDown: function(editor, caller, callback) {
  1545. var genColor = {r: 255, g: 255, b: 255},
  1546. content = $("<div />"),
  1547. colorColumns = editor.options.colors?editor.options.colors.split("|"):new Array(21),
  1548. // IE is slow at string concation so use an array
  1549. html = [],
  1550. htmlIndex = 0;
  1551. for (var i=0; i < colorColumns.length; ++i) {
  1552. var colors = colorColumns[i]?colorColumns[i].split(","):new Array(21);
  1553. html[htmlIndex++] = '<div class="sceditor-color-column">';
  1554. for (var x=0; x < colors.length; ++x) {
  1555. // use pre defined colour if can otherwise use the generated color
  1556. var color = colors[x]?colors[x]:"#" + genColor.r.toString(16) + genColor.g.toString(16) + genColor.b.toString(16);
  1557. html[htmlIndex++] = '<a href="#" class="sceditor-color-option" style="background-color: '+color+'" data-color="'+color+'"></a>';
  1558. // calculate the next generated color
  1559. if(x%5===0)
  1560. genColor = {r: genColor.r, g: genColor.g-51, b: 255};
  1561. else
  1562. genColor = {r: genColor.r, g: genColor.g, b: genColor.b-51};
  1563. }
  1564. html[htmlIndex++] = '</div>';
  1565. // calculate the next generated color
  1566. if(i%5===0)
  1567. genColor = {r: genColor.r-51, g: 255, b: 255};
  1568. else
  1569. genColor = {r: genColor.r, g: 255, b: 255};
  1570. }
  1571. content.append(html.join(''))
  1572. .find('a')
  1573. .click(function (e) {
  1574. callback($(this).attr('data-color'));
  1575. editor.closeDropDown(true);
  1576. e.preventDefault();
  1577. });
  1578. editor.createDropDown(caller, "color-picker", content);
  1579. },
  1580. exec: function (caller) {
  1581. var editor = this;
  1582. $.sceditor.command.get('color')._dropDown(
  1583. editor,
  1584. caller,
  1585. function(color) {
  1586. editor.execCommand("forecolor", color);
  1587. }
  1588. );
  1589. },
  1590. tooltip: "Font Color"
  1591. },
  1592. // END_COMMAND
  1593. // START_COMMAND: Remove Format
  1594. removeformat: {
  1595. exec: "removeformat",
  1596. tooltip: "Remove Formatting"
  1597. },
  1598. // END_COMMAND
  1599. // START_COMMAND: Cut
  1600. cut: {
  1601. exec: "cut",
  1602. tooltip: "Cut",
  1603. errorMessage: "Your browser does not allow the cut command. Please use the keyboard shortcut Ctrl/Cmd-X"
  1604. },
  1605. // END_COMMAND
  1606. // START_COMMAND: Copy
  1607. copy: {
  1608. exec: "copy",
  1609. tooltip: "Copy",
  1610. errorMessage: "Your browser does not allow the copy command. Please use the keyboard shortcut Ctrl/Cmd-C"
  1611. },
  1612. // END_COMMAND
  1613. // START_COMMAND: Paste
  1614. paste: {
  1615. exec: "paste",
  1616. tooltip: "Paste",
  1617. errorMessage: "Your browser does not allow the paste command. Please use the keyboard shortcut Ctrl/Cmd-V"
  1618. },
  1619. // END_COMMAND
  1620. // START_COMMAND: Paste Text
  1621. pastetext: {
  1622. exec: function (caller) {
  1623. var val,
  1624. editor = this,
  1625. content = _tmpl("pastetext", {
  1626. label: editor._("Paste your text inside the following box:"),
  1627. insert: editor._("Insert")
  1628. }, true);
  1629. content.find('.button').click(function (e) {
  1630. val = content.find("#txt").val();
  1631. if(val)
  1632. editor.wysiwygEditorInsertText(val);
  1633. editor.closeDropDown(true);
  1634. e.preventDefault();
  1635. });
  1636. editor.createDropDown(caller, "pastetext", content);
  1637. },
  1638. tooltip: "Paste Text"
  1639. },
  1640. // END_COMMAND
  1641. // START_COMMAND: Bullet List
  1642. bulletlist: {
  1643. exec: "insertunorderedlist",
  1644. tooltip: "Bullet list"
  1645. },
  1646. // END_COMMAND
  1647. // START_COMMAND: Ordered List
  1648. orderedlist: {
  1649. exec: "insertorderedlist",
  1650. tooltip: "Numbered list"
  1651. },
  1652. // END_COMMAND
  1653. // START_COMMAND: Table
  1654. table: {
  1655. exec: function (caller) {
  1656. var editor = this,
  1657. content = _tmpl("table", {
  1658. rows: editor._("Rows:"),
  1659. cols: editor._("Cols:"),
  1660. insert: editor._("Insert")
  1661. }, true);
  1662. content.find('.button').click(function (e) {
  1663. var rows = content.find("#rows").val() - 0,
  1664. cols = content.find("#cols").val() - 0,
  1665. html = '<table>';
  1666. if(rows < 1 || cols < 1)
  1667. return;
  1668. for (var row=0; row < rows; row++) {
  1669. html += '<tr>';
  1670. for (var col=0; col < cols; col++)
  1671. html += '<td>' + ($.sceditor.ie ? '' : '<br class="sceditor-ignore" />') + '</td>';
  1672. html += '</tr>';
  1673. }
  1674. html += '</table>';
  1675. editor.wysiwygEditorInsertHtml(html);
  1676. editor.closeDropDown(true);
  1677. e.preventDefault();
  1678. });
  1679. editor.createDropDown(caller, "inserttable", content);
  1680. },
  1681. tooltip: "Insert a table"
  1682. },
  1683. // END_COMMAND
  1684. // START_COMMAND: Horizontal Rule
  1685. horizontalrule: {
  1686. exec: "inserthorizontalrule",
  1687. tooltip: "Insert a horizontal rule"
  1688. },
  1689. // END_COMMAND
  1690. // START_COMMAND: Code
  1691. code: {
  1692. exec: function () {
  1693. this.wysiwygEditorInsertHtml('<code>', '<br /></code>');
  1694. },
  1695. tooltip: "Code"
  1696. },
  1697. // END_COMMAND
  1698. // START_COMMAND: Image
  1699. image: {
  1700. exec: function (caller) {
  1701. var editor = this,
  1702. content = _tmpl("image", {
  1703. url: editor._("URL:"),
  1704. width: editor._("Width (optional):"),
  1705. height: editor._("Height (optional):"),
  1706. insert: editor._("Insert")
  1707. }, true);
  1708. content.find('.button').click(function (e) {
  1709. var val = content.find("#image").val(),
  1710. attrs = '',
  1711. width, height;
  1712. if((width = content.find("#width").val()))
  1713. attrs += ' width="' + width + '"';
  1714. if((height = content.find("#height").val()))
  1715. attrs += ' height="' + height + '"';
  1716. if(val && val !== "http://")
  1717. editor.wysiwygEditorInsertHtml('<img' + attrs + ' src="' + val + '" />');
  1718. editor.closeDropDown(true);
  1719. e.preventDefault();
  1720. });
  1721. editor.createDropDown(caller, "insertimage", content);
  1722. },
  1723. tooltip: "Insert an image"
  1724. },
  1725. // END_COMMAND
  1726. // START_COMMAND: E-mail
  1727. email: {
  1728. exec: function (caller) {
  1729. var editor = this,
  1730. content = _tmpl("email", {
  1731. label: editor._("E-mail:"),
  1732. insert: editor._("Insert")
  1733. }, true);
  1734. content.find('.button').click(function (e) {
  1735. var val = content.find("#email").val();
  1736. if(val)
  1737. {
  1738. // needed for IE to reset the last range
  1739. editor.focus();
  1740. if(!editor.getRangeHelper().selectedHtml())
  1741. editor.wysiwygEditorInsertHtml('<a href="' + 'mailto:' + val + '">' + val + '</a>');
  1742. else
  1743. editor.execCommand("createlink", 'mailto:' + val);
  1744. }
  1745. editor.closeDropDown(true);
  1746. e.preventDefault();
  1747. });
  1748. editor.createDropDown(caller, "insertemail", content);
  1749. },
  1750. tooltip: "Insert an email"
  1751. },
  1752. // END_COMMAND
  1753. // START_COMMAND: Link
  1754. link: {
  1755. exec: function (caller) {
  1756. var editor = this,
  1757. content = _tmpl("link", {
  1758. url: editor._("URL:"),
  1759. desc: editor._("Description (optional):"),
  1760. ins: editor._("Insert")
  1761. }, true);
  1762. content.find('.button').click(function (e) {
  1763. var val = content.find("#link").val(),
  1764. description = content.find("#des").val();
  1765. if(val !== "" && val !== "http://") {
  1766. // needed for IE to reset the last range
  1767. editor.focus();
  1768. if(!editor.getRangeHelper().selectedHtml() || description)
  1769. {
  1770. if(!description)
  1771. description = val;
  1772. editor.wysiwygEditorInsertHtml('<a href="' + val + '">' + description + '</a>');
  1773. }
  1774. else
  1775. editor.execCommand("createlink", val);
  1776. }
  1777. editor.closeDropDown(true);
  1778. e.preventDefault();
  1779. });
  1780. editor.createDropDown(caller, "insertlink", content);
  1781. },
  1782. tooltip: "Insert a link"
  1783. },
  1784. // END_COMMAND
  1785. // START_COMMAND: Unlink
  1786. unlink: {
  1787. exec: "unlink",
  1788. tooltip: "Unlink"
  1789. },
  1790. // END_COMMAND
  1791. // START_COMMAND: Quote
  1792. quote: {
  1793. exec: function (caller, html, author) {
  1794. var before = '<blockquote>',
  1795. end = '</blockquote>';
  1796. // if there is HTML passed set end to null so any selected
  1797. // text is replaced
  1798. if(html)
  1799. {
  1800. author = (author ? '<cite>' + author + '</cite>' : '');
  1801. before = before + author + html + end + '<br />';
  1802. end = null;
  1803. }
  1804. // if not add a newline to the end of the inserted quote
  1805. else if(this.getRangeHelper().selectedHtml() === "")
  1806. end = '<br />' + end;
  1807. this.wysiwygEditorInsertHtml(before, end);
  1808. },
  1809. tooltip: "Insert a Quote"
  1810. },
  1811. // END_COMMAND
  1812. // START_COMMAND: Emoticons
  1813. emoticon: {
  1814. exec: function (caller) {
  1815. var appendEmoticon,
  1816. editor = this,
  1817. end = (editor.options.emoticonsCompat ? ' ' : ''),
  1818. content = $('<div />'),
  1819. line = $('<div />');
  1820. appendEmoticon = function (code, emoticon) {
  1821. line.append($('<img />')
  1822. .attr({
  1823. src: emoticon,
  1824. alt: code
  1825. })
  1826. .click(function (e) {
  1827. editor.insert($(this).attr('alt') + end);
  1828. editor.closeDropDown(true);
  1829. e.preventDefault();
  1830. })
  1831. );
  1832. if(line.children().length > 3) {
  1833. content.append(line);
  1834. line = $('<div />');
  1835. }
  1836. };
  1837. $.each(editor.options.emoticons.dropdown, appendEmoticon);
  1838. if(line.children().length > 0)
  1839. content.append(line);
  1840. if(editor.options.emoticons.more) {
  1841. var more = $(
  1842. this._('<a class="sceditor-more">{0}</a>', this._("More"))
  1843. ).click(function () {
  1844. var emoticons = $.extend({}, editor.options.emoticons.dropdown, editor.options.emoticons.more);
  1845. content = $('<div />');
  1846. $.each(emoticons, appendEmoticon);
  1847. if(line.children().length > 0)
  1848. content.append(line);
  1849. editor.createDropDown(caller, "insertemoticon", content);
  1850. return false;
  1851. });
  1852. content.append(more);
  1853. }
  1854. editor.createDropDown(caller, "insertemoticon", content);
  1855. },
  1856. txtExec: function(caller) {
  1857. $.sceditor.command.get('emoticon').exec.call(this, caller);
  1858. },
  1859. keyPress: function (e)
  1860. {
  1861. // make sure emoticons command is in the toolbar before running
  1862. if(this.options.toolbar.indexOf('emoticon') === -1)
  1863. return;
  1864. var editor = this,
  1865. pos = 0,
  1866. curChar = String.fromCharCode(e.which);
  1867. if(!editor.EmoticonsCache) {
  1868. editor.EmoticonsCache = [];
  1869. $.each($.extend({}, editor.options.emoticons.more, editor.options.emoticons.dropdown, editor.options.emoticons.hidden), function(key, url) {
  1870. editor.EmoticonsCache[pos++] = [
  1871. key,
  1872. _tmpl("emoticon", {key: key, url: url})
  1873. ];
  1874. });
  1875. editor.EmoticonsCache.sort(function(a, b){
  1876. return a[0].length - b[0].length;
  1877. });
  1878. }
  1879. if(!editor.longestEmoticonCode)
  1880. editor.longestEmoticonCode = editor.EmoticonsCache[editor.EmoticonsCache.length - 1][0].length;
  1881. if(editor.getRangeHelper().raplaceKeyword(editor.EmoticonsCache, true, true, editor.longestEmoticonCode, editor.options.emoticonsCompat, curChar))
  1882. {
  1883. if(/^\s$/.test(curChar) && editor.options.emoticonsCompat)
  1884. return true;
  1885. e.preventDefault();
  1886. e.stopPropagation();
  1887. return false;
  1888. }
  1889. },
  1890. tooltip: "Insert an emoticon"
  1891. },
  1892. // END_COMMAND
  1893. // START_COMMAND: YouTube
  1894. youtube: {
  1895. _dropDown: function (editor, caller, handleIdFunc) {
  1896. var matches,
  1897. content = _tmpl("youtubeMenu", {
  1898. label: editor._("Video URL:"),
  1899. insert: editor._("Insert")
  1900. }, true);
  1901. content.find('.button').click(function (e) {
  1902. var val = content.find("#link").val().replace("http://", "");
  1903. if (val !== "") {
  1904. matches = val.match(/(?:v=|v\/|embed\/|youtu.be\/)(.{11})/);
  1905. if (matches) val = matches[1];
  1906. if (/^[a-zA-Z0-9_\-]{11}$/.test(val))
  1907. handleIdFunc(val);
  1908. else
  1909. alert('Invalid YouTube video');
  1910. }
  1911. editor.closeDropDown(true);
  1912. e.preventDefault();
  1913. });
  1914. editor.createDropDown(caller, "insertlink", content);
  1915. },
  1916. exec: function (caller) {
  1917. var editor = this;
  1918. $.sceditor.command.get('youtube')._dropDown(
  1919. editor,
  1920. caller,
  1921. function(id) {
  1922. editor.wysiwygEditorInsertHtml(_tmpl("youtube", { id: id }));
  1923. }
  1924. );
  1925. },
  1926. tooltip: "Insert a YouTube video"
  1927. },
  1928. // END_COMMAND
  1929. // START_COMMAND: Date
  1930. date: {
  1931. _date: function (editor) {
  1932. var now = new Date(),
  1933. year = now.getYear(),
  1934. month = now.getMonth()+1,
  1935. day = now.getDate();
  1936. if(year < 2000)
  1937. year = 1900 + year;
  1938. if(month < 10)
  1939. month = "0" + month;
  1940. if(day < 10)
  1941. day = "0" + day;
  1942. return editor.options.dateFormat.replace(/year/i, year).replace(/month/i, month).replace(/day/i, day);
  1943. },
  1944. exec: function () {
  1945. this.insertText($.sceditor.command.get('date')._date(this));
  1946. },
  1947. txtExec: function () {
  1948. this.insertText($.sceditor.command.get('date')._date(this));
  1949. },
  1950. tooltip: "Insert current date"
  1951. },
  1952. // END_COMMAND
  1953. // START_COMMAND: Time
  1954. time: {
  1955. _time: function () {
  1956. var now = new Date(),
  1957. hours = now.getHours(),
  1958. mins = now.getMinutes(),
  1959. secs = now.getSeconds();
  1960. if(hours < 10)
  1961. hours = "0" + hours;
  1962. if(mins < 10)
  1963. mins = "0" + mins;
  1964. if(secs < 10)
  1965. secs = "0" + secs;
  1966. return hours + ':' + mins + ':' + secs;
  1967. },
  1968. exec: function () {
  1969. this.insertText($.sceditor.command.get('time')._time());
  1970. },
  1971. txtExec: function () {
  1972. this.insertText($.sceditor.command.get('time')._time());
  1973. },
  1974. tooltip: "Insert current time"
  1975. },
  1976. // END_COMMAND
  1977. // START_COMMAND: Ltr
  1978. ltr: {
  1979. exec: function() {
  1980. var editor = this,
  1981. elm = editor.getRangeHelper().getFirstBlockParent(),
  1982. $elm = $(elm);
  1983. editor.focus();
  1984. if(!elm || $elm.is('body'))
  1985. {
  1986. editor.execCommand("formatBlock", "p");
  1987. elm = editor.getRangeHelper().getFirstBlockParent();
  1988. $elm = $(elm);
  1989. if(!elm || $elm.is('body'))
  1990. return;
  1991. }
  1992. if($elm.css('direction') === 'ltr')
  1993. $(elm).css('direction', '');
  1994. else
  1995. $(elm).attr('direction', 'ltr');
  1996. },
  1997. tooltip: "Left-to-Right"
  1998. },
  1999. // END_COMMAND
  2000. // START_COMMAND: Rtl
  2001. rtl: {
  2002. exec: function() {
  2003. var editor = this,
  2004. elm = editor.getRangeHelper().getFirstBlockParent(),
  2005. $elm = $(elm);
  2006. editor.focus();
  2007. if(!elm || $elm.is('body'))
  2008. {
  2009. editor.execCommand("formatBlock", "p");
  2010. elm = editor.getRangeHelper().getFirstBlockParent();
  2011. $elm = $(elm);
  2012. if(!elm || $elm.is('body'))
  2013. return;
  2014. }
  2015. if($elm.css('direction') === 'rtl')
  2016. $(elm).css('direction', '');
  2017. else
  2018. $(elm).css('direction', 'rtl');
  2019. },
  2020. tooltip: "Right-to-Left"
  2021. },
  2022. // END_COMMAND
  2023. // START_COMMAND: Print
  2024. print: {
  2025. exec: "print",
  2026. tooltip: "Print"
  2027. },
  2028. // END_COMMAND
  2029. // START_COMMAND: Source
  2030. source: {
  2031. exec: function () {
  2032. this.toggleTextMode();
  2033. },
  2034. txtExec: function () {
  2035. this.toggleTextMode();
  2036. },
  2037. tooltip: "View source"
  2038. },
  2039. // END_COMMAND
  2040. // this is here so that commands above can be removed
  2041. // without having to remove the , after the last one.
  2042. // Needed for IE.
  2043. ignore: {}
  2044. };
  2045. /**
  2046. * Range helper class
  2047. * @class rangeHelper
  2048. * @name jQuery.sceditor.rangeHelper
  2049. */
  2050. $.sceditor.rangeHelper = function(w, d) {
  2051. var win, doc,
  2052. isW3C = true,
  2053. startMarker = "sceditor-start-marker",
  2054. endMarker = "sceditor-end-marker",
  2055. base = this,
  2056. init, _createMarker, _getOuterText, _selectOuterText;
  2057. /**
  2058. * @constructor
  2059. * @param Window window
  2060. * @param Document document
  2061. * @private
  2062. */
  2063. init = function (window, document) {
  2064. doc = document || window.contentDocument || window.document;
  2065. win = window;
  2066. isW3C = !!window.getSelection;
  2067. }(w, d);
  2068. /**
  2069. * <p>Inserts HTML into the current range replacing any selected
  2070. * text.</p>
  2071. *
  2072. * <p>If endHTML is specified the selected contents will be put between
  2073. * html and endHTML. If there is nothing selected html and endHTML are
  2074. * just concated together.</p>
  2075. *
  2076. * @param {string} html
  2077. * @param {string} endHTML
  2078. * @function
  2079. * @name insertHTML
  2080. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2081. */
  2082. base.insertHTML = function(html, endHTML) {
  2083. var node, div;
  2084. if(endHTML)
  2085. html += base.selectedHtml() + endHTML;
  2086. if(isW3C)
  2087. {
  2088. div = doc.createElement('div');
  2089. node = doc.createDocumentFragment();
  2090. div.innerHTML = html;
  2091. while(div.firstChild)
  2092. node.appendChild(div.firstChild);
  2093. base.insertNode(node);
  2094. }
  2095. else
  2096. base.selectedRange().pasteHTML(html);
  2097. };
  2098. /**
  2099. * <p>The same as insertHTML except with DOM nodes instead</p>
  2100. *
  2101. * <p><strong>Warning:</strong> the nodes must belong to the
  2102. * document they are being inserted into. Some browsers
  2103. * will throw exceptions if they don't.</p>
  2104. *
  2105. * @param {Node} node
  2106. * @param {Node} endNode
  2107. *
  2108. * @function
  2109. * @name insertNode
  2110. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2111. */
  2112. base.insertNode = function(node, endNode) {
  2113. if(isW3C)
  2114. {
  2115. var toInsert = doc.createDocumentFragment(),
  2116. range = base.selectedRange(),
  2117. selection, selectAfter;
  2118. toInsert.appendChild(node);
  2119. if(endNode)
  2120. {
  2121. toInsert.appendChild(range.extractContents());
  2122. toInsert.appendChild(endNode);
  2123. }
  2124. selectAfter = toInsert.lastChild;
  2125. range.deleteContents();
  2126. range.insertNode(toInsert);
  2127. selection = doc.createRange();
  2128. selection.setStartAfter(selectAfter);
  2129. base.selectRange(selection);
  2130. }
  2131. else
  2132. base.insertHTML(node.outerHTML, endNode?endNode.outerHTML:null);
  2133. };
  2134. /**
  2135. * <p>Clones the selected Range</p>
  2136. *
  2137. * <p>IE <= 8 will return a TextRange, all other browsers
  2138. * will return a Range object.</p>
  2139. *
  2140. * @return {Range|TextRange}
  2141. * @function
  2142. * @name cloneSelected
  2143. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2144. */
  2145. base.cloneSelected = function() {
  2146. if(!isW3C)
  2147. return base.selectedRange().duplicate();
  2148. return base.selectedRange().cloneRange();
  2149. };
  2150. /**
  2151. * <p>Gets the selected Range</p>
  2152. *
  2153. * <p>IE <= 8 will return a TextRange, all other browsers
  2154. * will return a Range object.</p>
  2155. *
  2156. * @return {Range|TextRange}
  2157. * @function
  2158. * @name selectedRange
  2159. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2160. */
  2161. base.selectedRange = function() {
  2162. var sel;
  2163. if(win.getSelection)
  2164. sel = win.getSelection();
  2165. else
  2166. sel = doc.selection;
  2167. if(sel.getRangeAt && sel.rangeCount <= 0)
  2168. sel.addRange(doc.createRange());
  2169. if(!isW3C)
  2170. return sel.createRange();
  2171. return sel.getRangeAt(0);
  2172. };
  2173. /**
  2174. * Gets the currently selected HTML
  2175. *
  2176. * @return {string}
  2177. * @function
  2178. * @name selectedHtml
  2179. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2180. */
  2181. base.selectedHtml = function() {
  2182. var range = base.selectedRange();
  2183. if(!range)
  2184. return '';
  2185. // IE9+ and all other browsers
  2186. if (window.XMLSerializer)
  2187. return new XMLSerializer().serializeToString(range.cloneContents());
  2188. // IE < 9
  2189. if(!isW3C)
  2190. {
  2191. if(range.text !== '' && range.htmlText)
  2192. return range.htmlText;
  2193. }
  2194. return '';
  2195. };
  2196. /**
  2197. * Gets the parent node of the selected contents in the range
  2198. *
  2199. * @return {HTMLElement}
  2200. * @function
  2201. * @name parentNode
  2202. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2203. */
  2204. base.parentNode = function() {
  2205. var range = base.selectedRange();
  2206. if(isW3C)
  2207. return range.commonAncestorContainer;
  2208. else
  2209. return range.parentElement();
  2210. };
  2211. /**
  2212. * Gets the first block level parent of the selected
  2213. * contents of the range.
  2214. *
  2215. * @return {HTMLElement}
  2216. * @function
  2217. * @name getFirstBlockParent
  2218. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2219. */
  2220. base.getFirstBlockParent = function() {
  2221. var func = function(node) {
  2222. if(!$.sceditor.dom.isInline(node))
  2223. return node;
  2224. var p = node.parentNode;
  2225. if(p)
  2226. return func(p);
  2227. return null;
  2228. };
  2229. return func(base.parentNode());
  2230. };
  2231. /**
  2232. * Inserts a node at either the start or end of the current selection
  2233. *
  2234. * @param {Bool} start
  2235. * @param {Node} node
  2236. * @function
  2237. * @name insertNodeAt
  2238. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2239. */
  2240. base.insertNodeAt = function(start, node) {
  2241. var range = base.cloneSelected();
  2242. range.collapse(start);
  2243. if(range.insertNode)
  2244. range.insertNode(node);
  2245. else
  2246. range.pasteHTML(node.outerHTML);
  2247. };
  2248. /**
  2249. * Creates a marker node
  2250. *
  2251. * @param {String} id
  2252. * @return {Node}
  2253. * @private
  2254. */
  2255. _createMarker = function(id) {
  2256. base.removeMarker(id);
  2257. var marker = doc.createElement("span");
  2258. marker.id = id;
  2259. marker.style.lineHeight = "0";
  2260. marker.style.display = "none";
  2261. marker.className = "sceditor-selection";
  2262. return marker;
  2263. };
  2264. /**
  2265. * Inserts start/end markers for the current selection
  2266. * which can be used by restoreRange to re-select the
  2267. * range.
  2268. *
  2269. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2270. * @function
  2271. * @name insertMarkers
  2272. */
  2273. base.insertMarkers = function() {
  2274. base.insertNodeAt(true, _createMarker(startMarker));
  2275. base.insertNodeAt(false, _createMarker(endMarker));
  2276. };
  2277. /**
  2278. * Gets the marker with the specified ID
  2279. *
  2280. * @param {String} id
  2281. * @return {Node}
  2282. * @function
  2283. * @name getMarker
  2284. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2285. */
  2286. base.getMarker = function(id) {
  2287. return doc.getElementById(id);
  2288. };
  2289. /**
  2290. * Removes the marker with the specified ID
  2291. *
  2292. * @param {String} id
  2293. * @function
  2294. * @name removeMarker
  2295. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2296. */
  2297. base.removeMarker = function(id) {
  2298. var marker = base.getMarker(id);
  2299. if(marker)
  2300. marker.parentNode.removeChild(marker);
  2301. };
  2302. /**
  2303. * Removes the start/end markers
  2304. *
  2305. * @function
  2306. * @name removeMarkers
  2307. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2308. */
  2309. base.removeMarkers = function() {
  2310. base.removeMarker(startMarker);
  2311. base.removeMarker(endMarker);
  2312. };
  2313. /**
  2314. * Saves the current range location. Alias of insertMarkers()
  2315. *
  2316. * @function
  2317. * @name saveRage
  2318. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2319. */
  2320. base.saveRange = function() {
  2321. base.insertMarkers();
  2322. };
  2323. /**
  2324. * Select the specified range
  2325. *
  2326. * @param {Range|TextRange} range
  2327. * @function
  2328. * @name selectRange
  2329. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2330. */
  2331. base.selectRange = function(range) {
  2332. if(!isW3C)
  2333. range.select();
  2334. else
  2335. {
  2336. win.getSelection().removeAllRanges();
  2337. win.getSelection().addRange(range);
  2338. }
  2339. };
  2340. /**
  2341. * Restores the last range saved by saveRange() or insertMarkers()
  2342. *
  2343. * @function
  2344. * @name restoreRange
  2345. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2346. */
  2347. base.restoreRange = function() {
  2348. var range = base.selectedRange(),
  2349. start = base.getMarker(startMarker),
  2350. end = base.getMarker(endMarker);
  2351. if(!start || !end)
  2352. return false;
  2353. if(!isW3C)
  2354. {
  2355. range = doc.body.createTextRange();
  2356. var marker = doc.body.createTextRange();
  2357. marker.moveToElementText(start);
  2358. range.setEndPoint('StartToStart', marker);
  2359. range.moveStart('character', 0);
  2360. marker.moveToElementText(end);
  2361. range.setEndPoint('EndToStart', marker);
  2362. range.moveEnd('character', 0);
  2363. base.selectRange(range);
  2364. }
  2365. else
  2366. {
  2367. range = doc.createRange();
  2368. range.setStartBefore(start);
  2369. range.setEndAfter(end);
  2370. base.selectRange(range);
  2371. }
  2372. base.removeMarkers();
  2373. };
  2374. /**
  2375. * Selects the text left and right of the current selection
  2376. * @param int left
  2377. * @param int right
  2378. * @private
  2379. */
  2380. _selectOuterText = function(left, right) {
  2381. var range = base.cloneSelected();
  2382. range.collapse(false);
  2383. if(!isW3C)
  2384. {
  2385. range.moveStart('character', 0-left);
  2386. range.moveEnd('character', right);
  2387. }
  2388. else
  2389. {
  2390. range.setStart(range.startContainer, range.startOffset-left);
  2391. range.setEnd(range.endContainer, range.endOffset+right);
  2392. //range.deleteContents();
  2393. }
  2394. base.selectRange(range);
  2395. };
  2396. /**
  2397. * Gets the text left or right of the current selection
  2398. * @param bool before
  2399. * @param int length
  2400. * @private
  2401. */
  2402. _getOuterText = function(before, length) {
  2403. var ret = "",
  2404. range = base.cloneSelected();
  2405. range.collapse(false);
  2406. if(before)
  2407. {
  2408. if(!isW3C)
  2409. {
  2410. range.moveStart('character', 0-length);
  2411. ret = range.text;
  2412. }
  2413. else
  2414. {
  2415. ret = range.startContainer.textContent.substr(0, range.startOffset);
  2416. ret = ret.substr(Math.max(0, ret.length - length));
  2417. }
  2418. }
  2419. else
  2420. {
  2421. if(!isW3C)
  2422. {
  2423. range.moveEnd('character', length);
  2424. ret = range.text;
  2425. }
  2426. else
  2427. ret = range.startContainer.textContent.substr(range.startOffset, length);
  2428. }
  2429. return ret;
  2430. };
  2431. /**
  2432. * Replaces keys with values based on the current range
  2433. *
  2434. * @param {Array} rep
  2435. * @param {Bool} includePrev If to include text before or just text after
  2436. * @param {Bool} repSorted If the keys array is pre sorted
  2437. * @param {Int} longestKey Length of the longest key
  2438. * @param {Bool} requireWhiteSpace If the key must be surrounded by whitespace
  2439. * @function
  2440. * @name raplaceKeyword
  2441. * @memberOf jQuery.sceditor.rangeHelper.prototype
  2442. */
  2443. base.raplaceKeyword = function(rep, includeAfter, repSorted, longestKey, requireWhiteSpace, curChar) {
  2444. if(!repSorted)
  2445. rep.sort(function(a, b){
  2446. return a.length - b.length;
  2447. });
  2448. var maxKeyLen = longestKey || rep[rep.length-1][0].length,
  2449. before, after, str, i, start, left, pat, lookStart;
  2450. before = after = str = "";
  2451. if(requireWhiteSpace)
  2452. {
  2453. // forcing spaces around doesn't work with textRanges as they will select text
  2454. // on the other side of an image causing space-img-key to be returned as
  2455. // space-key which would be valid when it's not.
  2456. if(!isW3C)
  2457. return false;
  2458. ++maxKeyLen;
  2459. }
  2460. before = _getOuterText(true, maxKeyLen);
  2461. if(includeAfter)
  2462. after = _getOuterText(false, maxKeyLen);
  2463. str = before + (curChar!=null?curChar:"") + after;
  2464. i = rep.length;
  2465. while(i--)
  2466. {
  2467. pat = new RegExp("(?:[\\s\xA0\u2002\u2003\u2009])" + $.sceditor.regexEscape(rep[i][0]) + "(?=[\\s\xA0\u2002\u2003\u2009])");
  2468. lookStart = before.length - 1 - rep[i][0].length;
  2469. if(requireWhiteSpace)
  2470. --lookStart;
  2471. lookStart = Math.max(0, lookStart);
  2472. if((!requireWhiteSpace && (start = str.indexOf(rep[i][0], lookStart)) > -1) ||
  2473. (requireWhiteSpace && (start = str.substr(lookStart).search(pat)) > -1))
  2474. {
  2475. if(requireWhiteSpace)
  2476. start += lookStart + 1;
  2477. // make sure the substr is between before and after not entierly in one
  2478. // or the other
  2479. if(start > before.length || start+rep[i][0].length + (requireWhiteSpace?1:0) < before.length)
  2480. continue;
  2481. left = before.length - start;
  2482. _selectOuterText(left, rep[i][0].length-left-(curChar!=null&&/^\S/.test(curChar)?1:0));
  2483. base.insertHTML(rep[i][1]);
  2484. return true;
  2485. }
  2486. }
  2487. return false;
  2488. };
  2489. };
  2490. /**
  2491. * Static DOM helper class
  2492. * @class dom
  2493. * @name jQuery.sceditor.dom
  2494. */
  2495. $.sceditor.dom =
  2496. /** @lends jQuery.sceditor.dom */
  2497. {
  2498. /**
  2499. * Loop all child nodes of the passed node
  2500. *
  2501. * The function should accept 1 parameter being the node.
  2502. * If the function returns false the loop will be exited.
  2503. *
  2504. * @param {HTMLElement} node
  2505. * @param {function} func Function that is called for every node, should accept 1 param for the node
  2506. * @param {bool} innermostFirst If the innermost node should be passed to the function before it's parents
  2507. * @param {bool} siblingsOnly If to only traverse the nodes siblings
  2508. * @param {bool} reverse If to traverse the nodes in reverse
  2509. */
  2510. traverse: function(node, func, innermostFirst, siblingsOnly, reverse) {
  2511. if(node)
  2512. {
  2513. node = reverse ? node.lastChild : node.firstChild;
  2514. while(node != null)
  2515. {
  2516. var next = reverse ? node.previousSibling : node.nextSibling;
  2517. if(!innermostFirst && func(node) === false)
  2518. return false;
  2519. // traverse all children
  2520. if(!siblingsOnly && this.traverse(node, func, innermostFirst, siblingsOnly, reverse) === false)
  2521. return false;
  2522. if(innermostFirst && func(node) === false)
  2523. return false;
  2524. // move to next child
  2525. node = next;
  2526. }
  2527. }
  2528. },
  2529. /**
  2530. * Like traverse but loops in reverse
  2531. * @see traverse
  2532. */
  2533. rTraverse: function(node, func, innermostFirst, siblingsOnly) {
  2534. this.traverse(node, func, innermostFirst, siblingsOnly, true);
  2535. },
  2536. /**
  2537. * List of block level elements seperated by bars (|)
  2538. * @type {string}
  2539. */
  2540. blockLevelList: "|body|hr|p|div|h1|h2|h3|h4|h5|h6|address|pre|form|table|tbody|thead|tfoot|th|tr|td|li|ol|ul|blockquote|center|",
  2541. /**
  2542. * Checks if an element is inline
  2543. *
  2544. * @return {bool}
  2545. */
  2546. isInline: function(elm, includeCodeAsBlock) {
  2547. if(!elm || elm.nodeType !== 1)
  2548. return true;
  2549. if(includeCodeAsBlock && elm.tagName.toLowerCase() === 'code')
  2550. return false;
  2551. return $.sceditor.dom.blockLevelList.indexOf("|" + elm.tagName.toLowerCase() + "|") < 0;
  2552. },
  2553. /**
  2554. * Copys the CSS from 1 node to another
  2555. *
  2556. * @param {HTMLElement} from
  2557. * @param {HTMLElement} to
  2558. */
  2559. copyCSS: function(from, to) {
  2560. to.style.cssText = from.style.cssText;
  2561. },
  2562. /**
  2563. * Fixes block level elements inside in inline elements.
  2564. *
  2565. * @param {HTMLElement} node
  2566. */
  2567. fixNesting: function(node) {
  2568. var base = this,
  2569. getLastInlineParent = function(node) {
  2570. while(base.isInline(node.parentNode, true))
  2571. node = node.parentNode;
  2572. return node;
  2573. };
  2574. base.traverse(node, function(node) {
  2575. // if node is an element, and it is blocklevel and the parent isn't block level
  2576. // then it needs fixing
  2577. if(node.nodeType === 1 && !base.isInline(node, true) && base.isInline(node.parentNode, true))
  2578. {
  2579. var parent = getLastInlineParent(node),
  2580. rParent = parent.parentNode,
  2581. before = base.extractContents(parent, node),
  2582. middle = node;
  2583. // copy current styling so when moved out of the parent
  2584. // it still has the same styling
  2585. base.copyCSS(middle, middle);
  2586. rParent.insertBefore(before, parent);
  2587. rParent.insertBefore(middle, parent);
  2588. }
  2589. });
  2590. },
  2591. /**
  2592. * Finds the common parent of two nodes
  2593. *
  2594. * @param {HTMLElement} node1
  2595. * @param {HTMLElement} node2
  2596. * @return {HTMLElement}
  2597. */
  2598. findCommonAncestor: function(node1, node2) {
  2599. // not as fast as making two arrays of parents and comparing
  2600. // but is a lot smaller and as it's currently only used with
  2601. // fixing invalid nesting it doesn't need to be very fast
  2602. return $(node1).parents().has($(node2)).first();
  2603. },
  2604. /**
  2605. * Removes unused whitespace from the root and all it's children
  2606. *
  2607. * @param HTMLElement root
  2608. * @return void
  2609. */
  2610. removeWhiteSpace: function(root) {
  2611. // 00A0 is non-breaking space which should not be striped
  2612. var regex = /[^\S|\u00A0]+/g;
  2613. this.traverse(root, function(node) {
  2614. if(node.nodeType === 3 && $(node).parents('code, pre').length === 0 && node.nodeValue)
  2615. {
  2616. // new lines in text nodes are always ignored in normal handling
  2617. node.nodeValue = node.nodeValue.replace(/[\r\n]/, "");
  2618. //remove empty nodes
  2619. if(!node.nodeValue.length)
  2620. {
  2621. node.parentNode.removeChild(node);
  2622. return;
  2623. }
  2624. if(!/\S|\u00A0/.test(node.nodeValue))
  2625. node.nodeValue = " ";
  2626. else if(regex.test(node.nodeValue))
  2627. node.nodeValue = node.nodeValue.replace(regex, " ");
  2628. }
  2629. });
  2630. },
  2631. /**
  2632. * Extracts all the nodes between the start and end nodes
  2633. *
  2634. * @param {HTMLElement} startNode The node to start extracting at
  2635. * @param {HTMLElement} endNode The node to stop extracting at
  2636. * @return {DocumentFragment}
  2637. */
  2638. extractContents: function(startNode, endNode) {
  2639. var base = this,
  2640. $commonAncestor = base.findCommonAncestor(startNode, endNode),
  2641. commonAncestor = !$commonAncestor?null:$commonAncestor.get(0),
  2642. startReached = false,
  2643. endReached = false;
  2644. return (function extract(root) {
  2645. var df = startNode.ownerDocument.createDocumentFragment();
  2646. base.traverse(root, function(node) {
  2647. // if end has been reached exit loop
  2648. if(endReached || (node === endNode && startReached))
  2649. {
  2650. endReached = true;
  2651. return false;
  2652. }
  2653. if(node === startNode)
  2654. startReached = true;
  2655. var c, n;
  2656. if(startReached)
  2657. {
  2658. // if the start has been reached and this elm contains
  2659. // the end node then clone it
  2660. if(jQuery.contains(node, endNode) && node.nodeType === 1)
  2661. {
  2662. c = extract(node);
  2663. n = node.cloneNode(false);
  2664. n.appendChild(c);
  2665. df.appendChild(n);
  2666. }
  2667. // otherwise just move it
  2668. else
  2669. df.appendChild(node);
  2670. }
  2671. // if this node contains the start node then add it
  2672. else if(jQuery.contains(node, startNode) && node.nodeType === 1)
  2673. {
  2674. c = extract(node);
  2675. n = node.cloneNode(false);
  2676. n.appendChild(c);
  2677. df.appendChild(n);
  2678. }
  2679. }, false);
  2680. return df;
  2681. }(commonAncestor));
  2682. }
  2683. };
  2684. /**
  2685. * Static command helper class
  2686. * @class command
  2687. * @name jQuery.sceditor.command
  2688. */
  2689. $.sceditor.command =
  2690. /** @lends jQuery.sceditor.command */
  2691. {
  2692. /**
  2693. * Gets a command
  2694. *
  2695. * @param {String} name
  2696. * @return {Object|null}
  2697. * @since v1.3.5
  2698. */
  2699. get: function(name) {
  2700. return $.sceditor.commands[name] || null;
  2701. },
  2702. /**
  2703. * <p>Adds a command to the editor or updates an exisiting
  2704. * command if a command with the specified name already exists.</p>
  2705. *
  2706. * <p>Once a command is add it can be included in the toolbar by
  2707. * adding it's name to the toolbar option in the constructor. It
  2708. * can also be executed manually by calling {@link jQuery.sceditor.execCommand}</p>
  2709. *
  2710. * @example
  2711. * $.sceditor.command.set("hello",
  2712. * {
  2713. * exec: function() {
  2714. * alert("Hello World!");
  2715. * }
  2716. * });
  2717. *
  2718. * @param {String} name
  2719. * @param {Object} cmd
  2720. * @return {this|false} Returns false if name or cmd is false
  2721. * @since v1.3.5
  2722. */
  2723. set: function(name, cmd) {
  2724. if(!name || !cmd)
  2725. return false;
  2726. // merge any existing command properties
  2727. cmd = $.extend($.sceditor.commands[name] || {}, cmd);
  2728. cmd.remove = function() { $.sceditor.command.remove(name); };
  2729. $.sceditor.commands[name] = cmd;
  2730. return this;
  2731. },
  2732. /**
  2733. * Removes a command
  2734. *
  2735. * @param {String} name
  2736. * @return {this}
  2737. * @since v1.3.5
  2738. */
  2739. remove: function(name) {
  2740. if($.sceditor.commands[name])
  2741. delete $.sceditor.commands[name];
  2742. return this;
  2743. }
  2744. };
  2745. /**
  2746. * Checks if a command with the specified name exists
  2747. *
  2748. * @param {String} name
  2749. * @return {Bool}
  2750. * @deprecated Since v1.3.5
  2751. * @memberOf jQuery.sceditor
  2752. */
  2753. $.sceditor.commandExists = function(name) {
  2754. return !!$.sceditor.command.get(name);
  2755. };
  2756. /**
  2757. * Adds/updates a command.
  2758. *
  2759. * Only name and exec are required. Exec is only required if
  2760. * the command dose not already exist.
  2761. *
  2762. * @param {String} name The commands name
  2763. * @param {String|Function} exec The commands exec function or string for the native execCommand
  2764. * @param {String} tooltip The commands tooltip text
  2765. * @param {Function} keypress Function that gets called every time a key is pressed
  2766. * @param {Function|Array} txtExec Called when the command is executed in source mode or array containing prepend and optional append
  2767. * @return {Bool}
  2768. * @deprecated Since v1.3.5
  2769. * @memberOf jQuery.sceditor
  2770. */
  2771. $.sceditor.setCommand = function(name, exec, tooltip, keypress, txtExec) {
  2772. return !!$.sceditor.command.set(name, {
  2773. exec: exec,
  2774. tooltip: tooltip,
  2775. keypress: keypress,
  2776. txtExec: txtExec
  2777. });
  2778. };
  2779. $.sceditor.defaultOptions = {
  2780. // Toolbar buttons order and groups. Should be comma seperated and have a bar | to seperate groups
  2781. toolbar: "bold,italic,underline,strike,subscript,superscript|left,center,right,justify|" +
  2782. "font,size,color,removeformat|cut,copy,paste,pastetext|bulletlist,orderedlist|" +
  2783. "table|code,quote|horizontalrule,image,email,link,unlink|emoticon,youtube,date,time|" +
  2784. "ltr,rtl|print,source",
  2785. // Stylesheet to include in the WYSIWYG editor. Will style the WYSIWYG elements
  2786. style: "jquery.sceditor.default.css",
  2787. // Comma seperated list of fonts for the font selector
  2788. fonts: "Arial,Arial Black,Comic Sans MS,Courier New,Georgia,Impact,Sans-serif,Serif,Times New Roman,Trebuchet MS,Verdana",
  2789. // Colors should be comma seperated and have a bar | to signal a new column. If null the colors will be auto generated.
  2790. colors: null,
  2791. locale: "en",
  2792. charset: "utf-8",
  2793. // compatibility mode for if you have emoticons such as :/ This mode requires
  2794. // emoticons to be surrounded by whitespace or end of line chars. This mode
  2795. // has limited As You Type emoticon converstion support (end of line chars)
  2796. // are not accepted as whitespace so only emoticons surrounded by whitespace
  2797. // will work
  2798. emoticonsCompat: false,
  2799. emoticonsRoot: '',
  2800. emoticons: {
  2801. dropdown: {
  2802. ":)": "emoticons/smile.png",
  2803. ":angel:": "emoticons/angel.png",
  2804. ":angry:": "emoticons/angry.png",
  2805. "8-)": "emoticons/cool.png",
  2806. ":'(": "emoticons/cwy.png",
  2807. ":ermm:": "emoticons/ermm.png",
  2808. ":D": "emoticons/grin.png",
  2809. "<3": "emoticons/heart.png",
  2810. ":(": "emoticons/sad.png",
  2811. ":O": "emoticons/shocked.png",
  2812. ":P": "emoticons/tongue.png",
  2813. ";)": "emoticons/wink.png"
  2814. },
  2815. more: {
  2816. ":alien:": "emoticons/alien.png",
  2817. ":blink:": "emoticons/blink.png",
  2818. ":blush:": "emoticons/blush.png",
  2819. ":cheerful:": "emoticons/cheerful.png",
  2820. ":devil:": "emoticons/devil.png",
  2821. ":dizzy:": "emoticons/dizzy.png",
  2822. ":getlost:": "emoticons/getlost.png",
  2823. ":happy:": "emoticons/happy.png",
  2824. ":kissing:": "emoticons/kissing.png",
  2825. ":ninja:": "emoticons/ninja.png",
  2826. ":pinch:": "emoticons/pinch.png",
  2827. ":pouty:": "emoticons/pouty.png",
  2828. ":sick:": "emoticons/sick.png",
  2829. ":sideways:": "emoticons/sideways.png",
  2830. ":silly:": "emoticons/silly.png",
  2831. ":sleeping:": "emoticons/sleeping.png",
  2832. ":unsure:": "emoticons/unsure.png",
  2833. ":woot:": "emoticons/w00t.png",
  2834. ":wassat:": "emoticons/wassat.png"
  2835. },
  2836. hidden: {
  2837. ":whistling:": "emoticons/whistling.png",
  2838. ":love:": "emoticons/wub.png"
  2839. }
  2840. },
  2841. // Width of the editor. Set to null for automatic with
  2842. width: null,
  2843. // Height of the editor including toolbat. Set to null for automatic height
  2844. height: null,
  2845. // If to allow the editor to be resized
  2846. resizeEnabled: true,
  2847. // Min resize to width, set to null for half textarea width or -1 for unlimited
  2848. resizeMinWidth: null,
  2849. // Min resize to height, set to null for half textarea height or -1 for unlimited
  2850. resizeMinHeight: null,
  2851. // Max resize to height, set to null for double textarea height or -1 for unlimited
  2852. resizeMaxHeight: null,
  2853. // Max resize to width, set to null for double textarea width or -1 for unlimited
  2854. resizeMaxWidth: null,
  2855. getHtmlHandler: null,
  2856. getTextHandler: null,
  2857. // date format. year, month and day will be replaced with the users current year, month and day.
  2858. dateFormat: "year-month-day",
  2859. toolbarContainer: null,
  2860. // Curently experimental
  2861. enablePasteFiltering: false,
  2862. readOnly: false,
  2863. rtl: false,
  2864. autofocus: false,
  2865. autoExpand: false,
  2866. // If to run the editor without WYSIWYG support
  2867. runWithoutWysiwygSupport: false,
  2868. id: null,
  2869. //add css to dropdown menu (eg. z-index)
  2870. dropDownCss: { }
  2871. };
  2872. $.fn.sceditor = function (options) {
  2873. if((!options || !options.runWithoutWysiwygSupport) && !$.sceditor.isWysiwygSupported())
  2874. return;
  2875. return this.each(function () {
  2876. (new $.sceditor(this, options));
  2877. });
  2878. };
  2879. })(jQuery, window, document);
  2880. (function($) {
  2881. var extensionMethods = {
  2882. InsertText: function(text, bClear) {
  2883. var bIsSource = this.inSourceMode();
  2884. // @TODO make it put the quote close to the current selection
  2885. if (!bIsSource)
  2886. this.toggleTextMode();
  2887. var current_value = bClear ? text + "\n" : this.getTextareaValue(false) + "\n" + text + "\n";
  2888. this.setTextareaValue(current_value);
  2889. if (!bIsSource)
  2890. this.toggleTextMode();
  2891. },
  2892. getText: function(filter) {
  2893. var current_value = '';
  2894. if(this.inSourceMode())
  2895. current_value = this.getTextareaValue(false);
  2896. else
  2897. current_value = this.getWysiwygEditorValue(filter);
  2898. return current_value;
  2899. },
  2900. appendEmoticon: function (code, emoticon) {
  2901. if (code == '')
  2902. line.append($('<br />'));
  2903. else
  2904. line.append($('<img />')
  2905. .attr({
  2906. src: emoticon,
  2907. alt: code,
  2908. })
  2909. .click(function (e) {
  2910. var start = '', end = '';
  2911. if (base.options.emoticonsCompat)
  2912. {
  2913. start = '<span> ';
  2914. end = ' </span>';
  2915. }
  2916. if (base.inSourceMode())
  2917. base.textEditorInsertText(' ' + $(this).attr('alt') + ' ');
  2918. else
  2919. base.wysiwygEditorInsertHtml(start + '<img src="' + $(this).attr("src") +
  2920. '" data-sceditor-emoticon="' + $(this).attr('alt') + '" />' + end);
  2921. e.preventDefault();
  2922. })
  2923. );
  2924. if (line.children().length > 0)
  2925. content.append(line);
  2926. $(".sceditor-toolbar").append(content);
  2927. },
  2928. storeLastState: function (){
  2929. this.wasSource = this.inSourceMode();
  2930. },
  2931. setTextMode: function () {
  2932. if (!this.inSourceMode())
  2933. this.toggleTextMode();
  2934. },
  2935. createPermanentDropDown: function() {
  2936. var emoticons = $.extend({}, this.options.emoticons.dropdown);
  2937. var popup_exists = false;
  2938. content = $('<div class="sceditor-insertemoticon" />');
  2939. line = $('<div />');
  2940. base = this;
  2941. for (smiley_popup in this.options.emoticons.popup)
  2942. {
  2943. popup_exists = true;
  2944. break;
  2945. }
  2946. if (popup_exists)
  2947. {
  2948. this.options.emoticons.more = this.options.emoticons.popup;
  2949. moreButton = $('<div class="sceditor-more-button" />').attr({'class': "sceditor-more"}).text('[' + this._('More') + ']').click(function () {
  2950. if ($(".sceditor-smileyPopup").length > 0)
  2951. {
  2952. $(".sceditor-smileyPopup").fadeIn('fast');
  2953. }
  2954. else
  2955. {
  2956. var emoticons = $.extend({}, base.options.emoticons.popup);
  2957. var popup_position;
  2958. var titlebar = $('<div class="catbg sceditor-popup-grip"/>');
  2959. popupContent = $('<div id="sceditor-popup"/>');
  2960. allowHide = true;
  2961. popupContent.append(titlebar);
  2962. line = $('<div />');
  2963. closeButton = $('<span />').text('[' + base._('Close') + ']').click(function () {
  2964. $(".sceditor-smileyPopup").fadeOut('fast');
  2965. });
  2966. $.each(emoticons, base.appendEmoticon);
  2967. if (line.children().length > 0)
  2968. popupContent.append(line);
  2969. if (typeof closeButton !== "undefined")
  2970. popupContent.append(closeButton);
  2971. // IE needs unselectable attr to stop it from unselecting the text in the editor.
  2972. // The editor can cope if IE does unselect the text it's just not nice.
  2973. if(base.ieUnselectable !== false) {
  2974. content = $(content);
  2975. content.find(':not(input,textarea)').filter(function() { return this.nodeType===1; }).attr('unselectable', 'on');
  2976. }
  2977. $dropdown = $('<div class="sceditor-dropdown sceditor-smileyPopup" />').append(popupContent);
  2978. $dropdown.appendTo($('body'));
  2979. dropdownIgnoreLastClick = true;
  2980. $dropdown.css({
  2981. position: "fixed",
  2982. top: $(window).height() * 0.2,
  2983. left: $(window).width() * 0.5 - ($dropdown.width() / 2),
  2984. "max-width": "50%"
  2985. });
  2986. $('.sceditor-smileyPopup').animaDrag({
  2987. speed: 150,
  2988. interval: 120,
  2989. grip: '.sceditor-popup-grip'
  2990. });
  2991. // stop clicks within the dropdown from being handled
  2992. $dropdown.click(function (e) {
  2993. e.stopPropagation();
  2994. });
  2995. }
  2996. });
  2997. }
  2998. $.each(emoticons, base.appendEmoticon);
  2999. if (typeof moreButton !== "undefined")
  3000. content.append(moreButton);
  3001. }
  3002. };
  3003. $.extend(true, $['sceditor'].prototype, extensionMethods);
  3004. })(jQuery);
  3005. $.sceditor.setCommand(
  3006. 'ftp',
  3007. function (caller) {
  3008. var editor = this,
  3009. content = $(this._('<form><div><label for="link">{0}</label> <input type="text" id="link" value="ftp://" /></div>' +
  3010. '<div><label for="des">{1}</label> <input type="text" id="des" value="" /></div></form>',
  3011. this._("URL:"),
  3012. this._("Description (optional):")
  3013. ))
  3014. .submit(function () {return false;});
  3015. content.append($(
  3016. this._('<div><input type="button" class="button" value="{0}" /></div>',
  3017. this._("Insert")
  3018. )).click(function (e) {
  3019. var val = $(this).parent("form").find("#link").val(),
  3020. description = $(this).parent("form").find("#des").val();
  3021. if(val !== "" && val !== "ftp://") {
  3022. // needed for IE to reset the last range
  3023. editor.focus();
  3024. if(!editor.getRangeHelper().selectedHtml() || description)
  3025. {
  3026. if(!description)
  3027. description = val;
  3028. editor.wysiwygEditorInsertHtml('<a href="' + val + '">' + description + '</a>');
  3029. }
  3030. else
  3031. editor.execCommand("createlink", val);
  3032. }
  3033. editor.closeDropDown(true);
  3034. e.preventDefault();
  3035. }));
  3036. editor.createDropDown(caller, "insertlink", content);
  3037. },
  3038. 'Insert FTP Link'
  3039. );
  3040. $.sceditor.setCommand(
  3041. 'glow',
  3042. function () {
  3043. this.wysiwygEditorInsertHtml('[glow=red,2,300]', '[/glow]');
  3044. },
  3045. 'Glow'
  3046. );
  3047. $.sceditor.setCommand(
  3048. 'shadow',
  3049. function () {
  3050. this.wysiwygEditorInsertHtml('[shadow=red,left]', '[/shadow]');
  3051. },
  3052. 'Shadow'
  3053. );
  3054. $.sceditor.setCommand(
  3055. 'tt',
  3056. function () {
  3057. this.wysiwygEditorInsertHtml('<tt>', '</tt>');
  3058. },
  3059. 'Teletype'
  3060. );
  3061. $.sceditor.setCommand(
  3062. 'pre',
  3063. function () {
  3064. this.wysiwygEditorInsertHtml('<pre>', '</pre>');
  3065. },
  3066. 'Pre'
  3067. );
  3068. /**
  3069. * AnimaDrag
  3070. * Animated jQuery Drag and Drop Plugin
  3071. * Version 0.5.1 beta
  3072. * Author Abel Mohler
  3073. * Released with the MIT License: http://www.opensource.org/licenses/mit-license.php
  3074. */
  3075. (function($){
  3076. $.fn.animaDrag = function(o, callback) {
  3077. var defaults = {
  3078. speed: 400,
  3079. interval: 300,
  3080. easing: null,
  3081. cursor: 'move',
  3082. boundary: document.body,
  3083. grip: null,
  3084. overlay: true,
  3085. after: function(e) {},
  3086. during: function(e) {},
  3087. before: function(e) {},
  3088. afterEachAnimation: function(e) {}
  3089. }
  3090. if(typeof callback == 'function') {
  3091. defaults.after = callback;
  3092. }
  3093. o = $.extend(defaults, o || {});
  3094. return this.each(function() {
  3095. var id, startX, startY, draggableStartX, draggableStartY, dragging = false, Ev, draggable = this,
  3096. grip = ($(this).find(o.grip).length > 0) ? $(this).find(o.grip) : $(this);
  3097. if(o.boundary) {
  3098. var limitTop = $(o.boundary).offset().top, limitLeft = $(o.boundary).offset().left,
  3099. limitBottom = limitTop + $(o.boundary).innerHeight(), limitRight = limitLeft + $(o.boundary).innerWidth();
  3100. }
  3101. grip.mousedown(function(e) {
  3102. o.before.call(draggable, e);
  3103. var lastX, lastY;
  3104. dragging = true;
  3105. Ev = e;
  3106. startX = lastX = e.pageX;
  3107. startY = lastY = e.pageY;
  3108. draggableStartX = $(draggable).offset().left;
  3109. draggableStartY = $(draggable).offset().top;
  3110. $(draggable).css({
  3111. position: 'absolute',
  3112. left: draggableStartX + 'px',
  3113. top: draggableStartY + 'px',
  3114. cursor: o.cursor,
  3115. zIndex: '1010'
  3116. }).addClass('anima-drag').appendTo(document.body);
  3117. if(o.overlay && $('#anima-drag-overlay').length == 0) {
  3118. $('<div id="anima-drag-overlay"></div>').css({
  3119. position: 'absolute',
  3120. top: '0',
  3121. left: '0',
  3122. zIndex: '1000',
  3123. width: $(document.body).outerWidth() + 'px',
  3124. height: $(document.body).outerHeight() + 'px'
  3125. }).appendTo(document.body);
  3126. }
  3127. else if(o.overlay) {
  3128. $('#anima-drag-overlay').show();
  3129. }
  3130. id = setInterval(function() {
  3131. if(lastX != Ev.pageX || lastY != Ev.pageY) {
  3132. var positionX = draggableStartX - (startX - Ev.pageX), positionY = draggableStartY - (startY - Ev.pageY);
  3133. if(positionX < limitLeft && o.boundary) {
  3134. positionX = limitLeft;
  3135. }
  3136. else if(positionX + $(draggable).innerWidth() > limitRight && o.boundary) {
  3137. positionX = limitRight - $(draggable).outerWidth();
  3138. }
  3139. if(positionY < limitTop && o.boundary) {
  3140. positionY = limitTop;
  3141. }
  3142. else if(positionY + $(draggable).innerHeight() > limitBottom && o.boundary) {
  3143. positionY = limitBottom - $(draggable).outerHeight();
  3144. }
  3145. $(draggable).stop().animate({
  3146. left: positionX + 'px',
  3147. top: positionY + 'px'
  3148. }, o.speed, o.easing, function(){o.afterEachAnimation.call(draggable, Ev)});
  3149. }
  3150. lastX = Ev.pageX;
  3151. lastY = Ev.pageY;
  3152. }, o.interval);
  3153. ($.browser.safari || e.preventDefault());
  3154. });
  3155. $(document).mousemove(function(e) {
  3156. if(dragging) {
  3157. Ev = e;
  3158. o.during.call(draggable, e);
  3159. }
  3160. });
  3161. $(document).mouseup(function(e) {
  3162. if(dragging) {
  3163. $(draggable).css({
  3164. cursor: '',
  3165. zIndex: '990'
  3166. }).removeClass('anima-drag');
  3167. $('#anima-drag-overlay').hide().appendTo(document.body);
  3168. clearInterval(id);
  3169. o.after.call(draggable, e);
  3170. dragging = false;
  3171. }
  3172. });
  3173. });
  3174. }
  3175. })(jQuery);