bootstrap-wysihtml5.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. !function($, wysi) {
  2. "use strict";
  3. var tpl = {
  4. "font-styles": function(locale, options) {
  5. var size = (options && options.size) ? ' btn-'+options.size : '';
  6. return "<li class='dropdown'>" +
  7. "<a class='btn dropdown-toggle" + size + "' data-toggle='dropdown' href='#'>" +
  8. "<i class='icon-font'></i>&nbsp;<span class='current-font'>" + locale.font_styles.normal + "</span>&nbsp;<b class='caret'></b>" +
  9. "</a>" +
  10. "<ul class='dropdown-menu'>" +
  11. "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='div' tabindex='-1'>" + locale.font_styles.normal + "</a></li>" +
  12. "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h1' tabindex='-1'>" + locale.font_styles.h1 + "</a></li>" +
  13. "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h2' tabindex='-1'>" + locale.font_styles.h2 + "</a></li>" +
  14. "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h3' tabindex='-1'>" + locale.font_styles.h3 + "</a></li>" +
  15. "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h4'>" + locale.font_styles.h4 + "</a></li>" +
  16. "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h5'>" + locale.font_styles.h5 + "</a></li>" +
  17. "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h6'>" + locale.font_styles.h6 + "</a></li>" +
  18. "</ul>" +
  19. "</li>";
  20. },
  21. "emphasis": function(locale, options) {
  22. var size = (options && options.size) ? ' btn-'+options.size : '';
  23. return "<li>" +
  24. "<div class='btn-group'>" +
  25. "<a class='btn" + size + "' data-wysihtml5-command='bold' title='CTRL+B' tabindex='-1'>" + locale.emphasis.bold + "</a>" +
  26. "<a class='btn" + size + "' data-wysihtml5-command='italic' title='CTRL+I' tabindex='-1'>" + locale.emphasis.italic + "</a>" +
  27. "<a class='btn" + size + "' data-wysihtml5-command='underline' title='CTRL+U' tabindex='-1'>" + locale.emphasis.underline + "</a>" +
  28. "</div>" +
  29. "</li>";
  30. },
  31. "lists": function(locale, options) {
  32. var size = (options && options.size) ? ' btn-'+options.size : '';
  33. return "<li>" +
  34. "<div class='btn-group'>" +
  35. "<a class='btn" + size + "' data-wysihtml5-command='insertUnorderedList' title='" + locale.lists.unordered + "' tabindex='-1'><i class='icon-list'></i></a>" +
  36. "<a class='btn" + size + "' data-wysihtml5-command='insertOrderedList' title='" + locale.lists.ordered + "' tabindex='-1'><i class='icon-th-list'></i></a>" +
  37. "<a class='btn" + size + "' data-wysihtml5-command='Outdent' title='" + locale.lists.outdent + "' tabindex='-1'><i class='icon-indent-right'></i></a>" +
  38. "<a class='btn" + size + "' data-wysihtml5-command='Indent' title='" + locale.lists.indent + "' tabindex='-1'><i class='icon-indent-left'></i></a>" +
  39. "</div>" +
  40. "</li>";
  41. },
  42. "link": function(locale, options) {
  43. var size = (options && options.size) ? ' btn-'+options.size : '';
  44. return "<li>" +
  45. "<div class='bootstrap-wysihtml5-insert-link-modal modal hide fade'>" +
  46. "<div class='modal-header'>" +
  47. "<a class='close' data-dismiss='modal'></a>" +
  48. "<h3>" + locale.link.insert + "</h3>" +
  49. "</div>" +
  50. "<div class='modal-body'>" +
  51. "<input type='text' value='http://' class='bootstrap-wysihtml5-insert-link-url1 m-wrap large'>" +
  52. "<label class='checkbox'> <input type='checkbox' class='bootstrap-wysihtml5-insert-link-target' checked>" + locale.link.target + "</label>" +
  53. "</div>" +
  54. "<div class='modal-footer'>" +
  55. "<a href='#' class='btn' data-dismiss='modal'>" + locale.link.cancel + "</a>" +
  56. "<a href='#' class='btn green btn-primary' data-dismiss='modal'>" + locale.link.insert + "</a>" +
  57. "</div>" +
  58. "</div>" +
  59. "<a class='btn" + size + "' data-wysihtml5-command='createLink' title='" + locale.link.insert + "' tabindex='-1'><i class='icon-share'></i></a>" +
  60. "</li>";
  61. },
  62. "image": function(locale, options) {
  63. var size = (options && options.size) ? ' btn-'+options.size : '';
  64. return "<li>" +
  65. "<div class='bootstrap-wysihtml5-insert-image-modal modal hide fade'>" +
  66. "<div class='modal-header'>" +
  67. "<a class='close' data-dismiss='modal'></a>" +
  68. "<h3>" + locale.image.insert + "</h3>" +
  69. "</div>" +
  70. "<div class='modal-body'>" +
  71. "<input type='text' value='http://' class='bootstrap-wysihtml5-insert-image-url m-wrap large'>" +
  72. "</div>" +
  73. "<div class='modal-footer'>" +
  74. "<a href='#' class='btn' data-dismiss='modal'>" + locale.image.cancel + "</a>" +
  75. "<a href='#' class='btn green btn-primary' data-dismiss='modal'>" + locale.image.insert + "</a>" +
  76. "</div>" +
  77. "</div>" +
  78. "<a class='btn" + size + "' data-wysihtml5-command='insertImage' title='" + locale.image.insert + "' tabindex='-1'><i class='icon-picture'></i></a>" +
  79. "</li>";
  80. },
  81. "html": function(locale, options) {
  82. var size = (options && options.size) ? ' btn-'+options.size : '';
  83. return "<li>" +
  84. "<div class='btn-group'>" +
  85. "<a class='btn" + size + "' data-wysihtml5-action='change_view' title='" + locale.html.edit + "' tabindex='-1'><i class='icon-pencil'></i></a>" +
  86. "</div>" +
  87. "</li>";
  88. },
  89. "color": function(locale, options) {
  90. var size = (options && options.size) ? ' btn-'+options.size : '';
  91. return "<li class='dropdown'>" +
  92. "<a class='btn dropdown-toggle" + size + "' data-toggle='dropdown' href='#' tabindex='-1'>" +
  93. "<span class='current-color'>" + locale.colours.black + "</span>&nbsp;<b class='caret'></b>" +
  94. "</a>" +
  95. "<ul class='dropdown-menu'>" +
  96. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='black'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='black'>" + locale.colours.black + "</a></li>" +
  97. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='silver'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='silver'>" + locale.colours.silver + "</a></li>" +
  98. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='gray'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='gray'>" + locale.colours.gray + "</a></li>" +
  99. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='maroon'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='maroon'>" + locale.colours.maroon + "</a></li>" +
  100. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='red'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='red'>" + locale.colours.red + "</a></li>" +
  101. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='purple'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='purple'>" + locale.colours.purple + "</a></li>" +
  102. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='green'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='green'>" + locale.colours.green + "</a></li>" +
  103. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='olive'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='olive'>" + locale.colours.olive + "</a></li>" +
  104. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='navy'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='navy'>" + locale.colours.navy + "</a></li>" +
  105. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='blue'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='blue'>" + locale.colours.blue + "</a></li>" +
  106. "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='orange'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='orange'>" + locale.colours.orange + "</a></li>" +
  107. "</ul>" +
  108. "</li>";
  109. }
  110. };
  111. var templates = function(key, locale, options) {
  112. return tpl[key](locale, options);
  113. };
  114. var Wysihtml5 = function(el, options) {
  115. this.el = el;
  116. var toolbarOpts = options || defaultOptions;
  117. for(var t in toolbarOpts.customTemplates) {
  118. tpl[t] = toolbarOpts.customTemplates[t];
  119. }
  120. this.toolbar = this.createToolbar(el, toolbarOpts);
  121. this.editor = this.createEditor(options);
  122. window.editor = this.editor;
  123. $('iframe.wysihtml5-sandbox').each(function(i, el){
  124. $(el.contentWindow).off('focus.wysihtml5').on({
  125. 'focus.wysihtml5' : function(){
  126. $('li.dropdown').removeClass('open');
  127. }
  128. });
  129. });
  130. };
  131. Wysihtml5.prototype = {
  132. constructor: Wysihtml5,
  133. createEditor: function(options) {
  134. options = options || {};
  135. // Add the toolbar to a clone of the options object so multiple instances
  136. // of the WYISYWG don't break because "toolbar" is already defined
  137. options = $.extend(true, {}, options);
  138. options.toolbar = this.toolbar[0];
  139. var editor = new wysi.Editor(this.el[0], options);
  140. if(options && options.events) {
  141. for(var eventName in options.events) {
  142. editor.on(eventName, options.events[eventName]);
  143. }
  144. }
  145. return editor;
  146. },
  147. createToolbar: function(el, options) {
  148. var self = this;
  149. var toolbar = $("<ul/>", {
  150. 'class' : "wysihtml5-toolbar",
  151. 'style': "display:none"
  152. });
  153. var culture = options.locale || defaultOptions.locale || "en";
  154. for(var key in defaultOptions) {
  155. var value = false;
  156. if(options[key] !== undefined) {
  157. if(options[key] === true) {
  158. value = true;
  159. }
  160. } else {
  161. value = defaultOptions[key];
  162. }
  163. if(value === true) {
  164. toolbar.append(templates(key, locale[culture], options));
  165. if(key === "html") {
  166. this.initHtml(toolbar);
  167. }
  168. if(key === "link") {
  169. this.initInsertLink(toolbar);
  170. }
  171. if(key === "image") {
  172. this.initInsertImage(toolbar);
  173. }
  174. }
  175. }
  176. if(options.toolbar) {
  177. for(key in options.toolbar) {
  178. toolbar.append(options.toolbar[key]);
  179. }
  180. }
  181. toolbar.find("a[data-wysihtml5-command='formatBlock']").click(function(e) {
  182. var target = e.target || e.srcElement;
  183. var el = $(target);
  184. self.toolbar.find('.current-font').text(el.html());
  185. });
  186. toolbar.find("a[data-wysihtml5-command='foreColor']").click(function(e) {
  187. var target = e.target || e.srcElement;
  188. var el = $(target);
  189. self.toolbar.find('.current-color').text(el.html());
  190. });
  191. this.el.before(toolbar);
  192. return toolbar;
  193. },
  194. initHtml: function(toolbar) {
  195. var changeViewSelector = "a[data-wysihtml5-action='change_view']";
  196. toolbar.find(changeViewSelector).click(function(e) {
  197. toolbar.find('a.btn').not(changeViewSelector).toggleClass('disabled');
  198. });
  199. },
  200. initInsertImage: function(toolbar) {
  201. var self = this;
  202. var insertImageModal = toolbar.find('.bootstrap-wysihtml5-insert-image-modal');
  203. var urlInput = insertImageModal.find('.bootstrap-wysihtml5-insert-image-url');
  204. var insertButton = insertImageModal.find('a.btn-primary');
  205. var initialValue = urlInput.val();
  206. var caretBookmark;
  207. var insertImage = function() {
  208. var url = urlInput.val();
  209. urlInput.val(initialValue);
  210. self.editor.currentView.element.focus();
  211. if (caretBookmark) {
  212. self.editor.composer.selection.setBookmark(caretBookmark);
  213. caretBookmark = null;
  214. }
  215. self.editor.composer.commands.exec("insertImage", url);
  216. };
  217. urlInput.keypress(function(e) {
  218. if(e.which == 13) {
  219. insertImage();
  220. insertImageModal.modal('hide');
  221. }
  222. });
  223. insertButton.click(insertImage);
  224. insertImageModal.on('shown', function() {
  225. urlInput.focus();
  226. });
  227. insertImageModal.on('hide', function() {
  228. self.editor.currentView.element.focus();
  229. });
  230. toolbar.find('a[data-wysihtml5-command=insertImage]').click(function() {
  231. var activeButton = $(this).hasClass("wysihtml5-command-active");
  232. if (!activeButton) {
  233. self.editor.currentView.element.focus(false);
  234. caretBookmark = self.editor.composer.selection.getBookmark();
  235. insertImageModal.appendTo('body').modal('show');
  236. insertImageModal.on('click.dismiss.modal', '[data-dismiss="modal"]', function(e) {
  237. e.stopPropagation();
  238. });
  239. return false;
  240. }
  241. else {
  242. return true;
  243. }
  244. });
  245. },
  246. initInsertLink: function(toolbar) {
  247. var self = this;
  248. var insertLinkModal = toolbar.find('.bootstrap-wysihtml5-insert-link-modal');
  249. var urlInput = insertLinkModal.find('.bootstrap-wysihtml5-insert-link-url');
  250. var targetInput = insertLinkModal.find('.bootstrap-wysihtml5-insert-link-target');
  251. var insertButton = insertLinkModal.find('a.btn-primary');
  252. var initialValue = urlInput.val();
  253. var caretBookmark;
  254. var insertLink = function() {
  255. var url = urlInput.val();
  256. urlInput.val(initialValue);
  257. self.editor.currentView.element.focus();
  258. if (caretBookmark) {
  259. self.editor.composer.selection.setBookmark(caretBookmark);
  260. caretBookmark = null;
  261. }
  262. var newWindow = targetInput.prop("checked");
  263. self.editor.composer.commands.exec("createLink", {
  264. 'href' : url,
  265. 'target' : (newWindow ? '_blank' : '_self'),
  266. 'rel' : (newWindow ? 'nofollow' : '')
  267. });
  268. };
  269. var pressedEnter = false;
  270. urlInput.keypress(function(e) {
  271. if(e.which == 13) {
  272. insertLink();
  273. insertLinkModal.modal('hide');
  274. }
  275. });
  276. insertButton.click(insertLink);
  277. insertLinkModal.on('shown', function() {
  278. urlInput.focus();
  279. });
  280. insertLinkModal.on('hide', function() {
  281. self.editor.currentView.element.focus();
  282. });
  283. toolbar.find('a[data-wysihtml5-command=createLink]').click(function() {
  284. var activeButton = $(this).hasClass("wysihtml5-command-active");
  285. if (!activeButton) {
  286. self.editor.currentView.element.focus(false);
  287. caretBookmark = self.editor.composer.selection.getBookmark();
  288. insertLinkModal.appendTo('body').modal('show');
  289. App.initUniform(); //initialize uniform checkboxes
  290. insertLinkModal.on('click.dismiss.modal', '[data-dismiss="modal"]', function(e) {
  291. e.stopPropagation();
  292. });
  293. return false;
  294. }
  295. else {
  296. return true;
  297. }
  298. });
  299. }
  300. };
  301. // these define our public api
  302. var methods = {
  303. resetDefaults: function() {
  304. $.fn.wysihtml5.defaultOptions = $.extend(true, {}, $.fn.wysihtml5.defaultOptionsCache);
  305. },
  306. bypassDefaults: function(options) {
  307. return this.each(function () {
  308. var $this = $(this);
  309. $this.data('wysihtml5', new Wysihtml5($this, options));
  310. });
  311. },
  312. shallowExtend: function (options) {
  313. var settings = $.extend({}, $.fn.wysihtml5.defaultOptions, options || {}, $(this).data());
  314. var that = this;
  315. return methods.bypassDefaults.apply(that, [settings]);
  316. },
  317. deepExtend: function(options) {
  318. var settings = $.extend(true, {}, $.fn.wysihtml5.defaultOptions, options || {});
  319. var that = this;
  320. return methods.bypassDefaults.apply(that, [settings]);
  321. },
  322. init: function(options) {
  323. var that = this;
  324. return methods.shallowExtend.apply(that, [options]);
  325. }
  326. };
  327. $.fn.wysihtml5 = function ( method ) {
  328. if ( methods[method] ) {
  329. return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
  330. } else if ( typeof method === 'object' || ! method ) {
  331. return methods.init.apply( this, arguments );
  332. } else {
  333. $.error( 'Method ' + method + ' does not exist on jQuery.wysihtml5' );
  334. }
  335. };
  336. $.fn.wysihtml5.Constructor = Wysihtml5;
  337. var defaultOptions = $.fn.wysihtml5.defaultOptions = {
  338. "font-styles": true,
  339. "color": false,
  340. "emphasis": true,
  341. "lists": true,
  342. "html": false,
  343. "link": true,
  344. "image": true,
  345. events: {},
  346. parserRules: {
  347. classes: {
  348. // (path_to_project/lib/css/wysiwyg-color.css)
  349. "wysiwyg-color-silver" : 1,
  350. "wysiwyg-color-gray" : 1,
  351. "wysiwyg-color-white" : 1,
  352. "wysiwyg-color-maroon" : 1,
  353. "wysiwyg-color-red" : 1,
  354. "wysiwyg-color-purple" : 1,
  355. "wysiwyg-color-fuchsia" : 1,
  356. "wysiwyg-color-green" : 1,
  357. "wysiwyg-color-lime" : 1,
  358. "wysiwyg-color-olive" : 1,
  359. "wysiwyg-color-yellow" : 1,
  360. "wysiwyg-color-navy" : 1,
  361. "wysiwyg-color-blue" : 1,
  362. "wysiwyg-color-teal" : 1,
  363. "wysiwyg-color-aqua" : 1,
  364. "wysiwyg-color-orange" : 1
  365. },
  366. tags: {
  367. "b": {},
  368. "i": {},
  369. "br": {},
  370. "ol": {},
  371. "ul": {},
  372. "li": {},
  373. "h1": {},
  374. "h2": {},
  375. "h3": {},
  376. "h4": {},
  377. "h5": {},
  378. "h6": {},
  379. "blockquote": {},
  380. "u": 1,
  381. "img": {
  382. "check_attributes": {
  383. "width": "numbers",
  384. "alt": "alt",
  385. "src": "url",
  386. "height": "numbers"
  387. }
  388. },
  389. "a": {
  390. check_attributes: {
  391. 'href': "url", // important to avoid XSS
  392. 'target': 'alt',
  393. 'rel': 'alt'
  394. }
  395. },
  396. "span": 1,
  397. "div": 1,
  398. // to allow save and edit files with code tag hacks
  399. "code": 1,
  400. "pre": 1
  401. }
  402. },
  403. stylesheets: ["./lib/css/wysiwyg-color.css"], // (path_to_project/lib/css/wysiwyg-color.css)
  404. locale: "en"
  405. };
  406. if (typeof $.fn.wysihtml5.defaultOptionsCache === 'undefined') {
  407. $.fn.wysihtml5.defaultOptionsCache = $.extend(true, {}, $.fn.wysihtml5.defaultOptions);
  408. }
  409. var locale = $.fn.wysihtml5.locale = {
  410. en: {
  411. font_styles: {
  412. normal: "Normal text",
  413. h1: "Heading 1",
  414. h2: "Heading 2",
  415. h3: "Heading 3",
  416. h4: "Heading 4",
  417. h5: "Heading 5",
  418. h6: "Heading 6"
  419. },
  420. emphasis: {
  421. bold: "Bold",
  422. italic: "Italic",
  423. underline: "Underline"
  424. },
  425. lists: {
  426. unordered: "Unordered list",
  427. ordered: "Ordered list",
  428. outdent: "Outdent",
  429. indent: "Indent"
  430. },
  431. link: {
  432. insert: "Insert link",
  433. cancel: "Cancel",
  434. target: "Open link in new window"
  435. },
  436. image: {
  437. insert: "Insert image",
  438. cancel: "Cancel"
  439. },
  440. html: {
  441. edit: "Edit HTML"
  442. },
  443. colours: {
  444. black: "Black",
  445. silver: "Silver",
  446. gray: "Grey",
  447. maroon: "Maroon",
  448. red: "Red",
  449. purple: "Purple",
  450. green: "Green",
  451. olive: "Olive",
  452. navy: "Navy",
  453. blue: "Blue",
  454. orange: "Orange"
  455. }
  456. }
  457. };
  458. }(window.jQuery, window.wysihtml5);