plugin.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. * plugin.js
  3. *
  4. * Released under LGPL License.
  5. * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /*global tinymce:true */
  11. tinymce.PluginManager.add('link', function(editor) {
  12. function createLinkList(callback) {
  13. return function() {
  14. var linkList = editor.settings.link_list;
  15. if (typeof linkList == "string") {
  16. tinymce.util.XHR.send({
  17. url: linkList,
  18. success: function(text) {
  19. callback(tinymce.util.JSON.parse(text));
  20. }
  21. });
  22. } else if (typeof linkList == "function") {
  23. linkList(callback);
  24. } else {
  25. callback(linkList);
  26. }
  27. };
  28. }
  29. function buildListItems(inputList, itemCallback, startItems) {
  30. function appendItems(values, output) {
  31. output = output || [];
  32. tinymce.each(values, function(item) {
  33. var menuItem = {text: item.text || item.title};
  34. if (item.menu) {
  35. menuItem.menu = appendItems(item.menu);
  36. } else {
  37. menuItem.value = item.value;
  38. if (itemCallback) {
  39. itemCallback(menuItem);
  40. }
  41. }
  42. output.push(menuItem);
  43. });
  44. return output;
  45. }
  46. return appendItems(inputList, startItems || []);
  47. }
  48. function showDialog(linkList) {
  49. var data = {}, selection = editor.selection, dom = editor.dom, selectedElm, anchorElm, initialText;
  50. var win, onlyText, textListCtrl, linkListCtrl, relListCtrl, targetListCtrl, classListCtrl, linkTitleCtrl, value;
  51. function linkListChangeHandler(e) {
  52. var textCtrl = win.find('#text');
  53. if (!textCtrl.value() || (e.lastControl && textCtrl.value() == e.lastControl.text())) {
  54. textCtrl.value(e.control.text());
  55. }
  56. win.find('#href').value(e.control.value());
  57. }
  58. function buildAnchorListControl(url) {
  59. var anchorList = [];
  60. tinymce.each(editor.dom.select('a:not([href])'), function(anchor) {
  61. var id = anchor.name || anchor.id;
  62. if (id) {
  63. anchorList.push({
  64. text: id,
  65. value: '#' + id,
  66. selected: url.indexOf('#' + id) != -1
  67. });
  68. }
  69. });
  70. if (anchorList.length) {
  71. anchorList.unshift({text: 'None', value: ''});
  72. return {
  73. name: 'anchor',
  74. type: 'listbox',
  75. label: 'Anchors',
  76. values: anchorList,
  77. onselect: linkListChangeHandler
  78. };
  79. }
  80. }
  81. function updateText() {
  82. if (!initialText && data.text.length === 0 && onlyText) {
  83. this.parent().parent().find('#text')[0].value(this.value());
  84. }
  85. }
  86. function urlChange(e) {
  87. var meta = e.meta || {};
  88. if (linkListCtrl) {
  89. linkListCtrl.value(editor.convertURL(this.value(), 'href'));
  90. }
  91. tinymce.each(e.meta, function(value, key) {
  92. win.find('#' + key).value(value);
  93. });
  94. if (!meta.text) {
  95. updateText.call(this);
  96. }
  97. }
  98. function isOnlyTextSelected(anchorElm) {
  99. var html = selection.getContent();
  100. // Partial html and not a fully selected anchor element
  101. if (/</.test(html) && (!/^<a [^>]+>[^<]+<\/a>$/.test(html) || html.indexOf('href=') == -1)) {
  102. return false;
  103. }
  104. if (anchorElm) {
  105. var nodes = anchorElm.childNodes, i;
  106. if (nodes.length === 0) {
  107. return false;
  108. }
  109. for (i = nodes.length - 1; i >= 0; i--) {
  110. if (nodes[i].nodeType != 3) {
  111. return false;
  112. }
  113. }
  114. }
  115. return true;
  116. }
  117. selectedElm = selection.getNode();
  118. anchorElm = dom.getParent(selectedElm, 'a[href]');
  119. onlyText = isOnlyTextSelected();
  120. data.text = initialText = anchorElm ? (anchorElm.innerText || anchorElm.textContent) : selection.getContent({format: 'text'});
  121. data.href = anchorElm ? dom.getAttrib(anchorElm, 'href') : '';
  122. if (anchorElm) {
  123. data.target = dom.getAttrib(anchorElm, 'target');
  124. } else if (editor.settings.default_link_target) {
  125. data.target = editor.settings.default_link_target;
  126. }
  127. if ((value = dom.getAttrib(anchorElm, 'rel'))) {
  128. data.rel = value;
  129. }
  130. if ((value = dom.getAttrib(anchorElm, 'class'))) {
  131. data['class'] = value;
  132. }
  133. if ((value = dom.getAttrib(anchorElm, 'title'))) {
  134. data.title = value;
  135. }
  136. if (onlyText) {
  137. textListCtrl = {
  138. name: 'text',
  139. type: 'textbox',
  140. size: 40,
  141. label: 'Text to display',
  142. onchange: function() {
  143. data.text = this.value();
  144. }
  145. };
  146. }
  147. if (linkList) {
  148. linkListCtrl = {
  149. type: 'listbox',
  150. label: 'Link list',
  151. values: buildListItems(
  152. linkList,
  153. function(item) {
  154. item.value = editor.convertURL(item.value || item.url, 'href');
  155. },
  156. [{text: 'None', value: ''}]
  157. ),
  158. onselect: linkListChangeHandler,
  159. value: editor.convertURL(data.href, 'href'),
  160. onPostRender: function() {
  161. /*eslint consistent-this:0*/
  162. linkListCtrl = this;
  163. }
  164. };
  165. }
  166. if (editor.settings.target_list !== false) {
  167. if (!editor.settings.target_list) {
  168. editor.settings.target_list = [
  169. {text: 'None', value: ''},
  170. {text: 'New window', value: '_blank'}
  171. ];
  172. }
  173. targetListCtrl = {
  174. name: 'target',
  175. type: 'listbox',
  176. label: 'Target',
  177. values: buildListItems(editor.settings.target_list)
  178. };
  179. }
  180. if (editor.settings.rel_list) {
  181. relListCtrl = {
  182. name: 'rel',
  183. type: 'listbox',
  184. label: 'Rel',
  185. values: buildListItems(editor.settings.rel_list)
  186. };
  187. }
  188. if (editor.settings.link_class_list) {
  189. classListCtrl = {
  190. name: 'class',
  191. type: 'listbox',
  192. label: 'Class',
  193. values: buildListItems(
  194. editor.settings.link_class_list,
  195. function(item) {
  196. if (item.value) {
  197. item.textStyle = function() {
  198. return editor.formatter.getCssText({inline: 'a', classes: [item.value]});
  199. };
  200. }
  201. }
  202. )
  203. };
  204. }
  205. if (editor.settings.link_title !== false) {
  206. linkTitleCtrl = {
  207. name: 'title',
  208. type: 'textbox',
  209. label: 'Title',
  210. value: data.title
  211. };
  212. }
  213. win = editor.windowManager.open({
  214. title: 'Insert link',
  215. data: data,
  216. body: [
  217. {
  218. name: 'href',
  219. type: 'filepicker',
  220. filetype: 'file',
  221. size: 40,
  222. autofocus: true,
  223. label: 'Url',
  224. onchange: urlChange,
  225. onkeyup: updateText
  226. },
  227. textListCtrl,
  228. linkTitleCtrl,
  229. buildAnchorListControl(data.href),
  230. linkListCtrl,
  231. relListCtrl,
  232. targetListCtrl,
  233. classListCtrl
  234. ],
  235. onSubmit: function(e) {
  236. /*eslint dot-notation: 0*/
  237. var href;
  238. data = tinymce.extend(data, e.data);
  239. href = data.href;
  240. // Delay confirm since onSubmit will move focus
  241. function delayedConfirm(message, callback) {
  242. var rng = editor.selection.getRng();
  243. tinymce.util.Delay.setEditorTimeout(editor, function() {
  244. editor.windowManager.confirm(message, function(state) {
  245. editor.selection.setRng(rng);
  246. callback(state);
  247. });
  248. });
  249. }
  250. function insertLink() {
  251. var linkAttrs = {
  252. href: href,
  253. target: data.target ? data.target : null,
  254. rel: data.rel ? data.rel : null,
  255. "class": data["class"] ? data["class"] : null,
  256. title: data.title ? data.title : null
  257. };
  258. if (anchorElm) {
  259. editor.focus();
  260. if (onlyText && data.text != initialText) {
  261. if ("innerText" in anchorElm) {
  262. anchorElm.innerText = data.text;
  263. } else {
  264. anchorElm.textContent = data.text;
  265. }
  266. }
  267. dom.setAttribs(anchorElm, linkAttrs);
  268. selection.select(anchorElm);
  269. editor.undoManager.add();
  270. } else {
  271. if (onlyText) {
  272. editor.insertContent(dom.createHTML('a', linkAttrs, dom.encode(data.text)));
  273. } else {
  274. editor.execCommand('mceInsertLink', false, linkAttrs);
  275. }
  276. }
  277. }
  278. if (!href) {
  279. editor.execCommand('unlink');
  280. return;
  281. }
  282. // Is email and not //user@domain.com
  283. if (href.indexOf('@') > 0 && href.indexOf('//') == -1 && href.indexOf('mailto:') == -1) {
  284. delayedConfirm(
  285. 'The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?',
  286. function(state) {
  287. if (state) {
  288. href = 'mailto:' + href;
  289. }
  290. insertLink();
  291. }
  292. );
  293. return;
  294. }
  295. // Is not protocol prefixed
  296. if ((editor.settings.link_assume_external_targets && !/^\w+:/i.test(href)) ||
  297. (!editor.settings.link_assume_external_targets && /^\s*www[\.|\d\.]/i.test(href))) {
  298. delayedConfirm(
  299. 'The URL you entered seems to be an external link. Do you want to add the required http:// prefix?',
  300. function(state) {
  301. if (state) {
  302. href = 'http://' + href;
  303. }
  304. insertLink();
  305. }
  306. );
  307. return;
  308. }
  309. insertLink();
  310. }
  311. });
  312. }
  313. editor.addButton('link', {
  314. icon: 'link',
  315. tooltip: 'Insert/edit link',
  316. shortcut: 'Meta+K',
  317. onclick: createLinkList(showDialog),
  318. stateSelector: 'a[href]'
  319. });
  320. editor.addButton('unlink', {
  321. icon: 'unlink',
  322. tooltip: 'Remove link',
  323. cmd: 'unlink',
  324. stateSelector: 'a[href]'
  325. });
  326. editor.addShortcut('Meta+K', '', createLinkList(showDialog));
  327. editor.addCommand('mceLink', createLinkList(showDialog));
  328. this.showDialog = showDialog;
  329. editor.addMenuItem('link', {
  330. icon: 'link',
  331. text: 'Insert/edit link',
  332. shortcut: 'Meta+K',
  333. onclick: createLinkList(showDialog),
  334. stateSelector: 'a[href]',
  335. context: 'insert',
  336. prependToContext: true
  337. });
  338. });