bootstrap-wysihtml5.js 23KB

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