| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- /**
- * plugin.js
- *
- * Copyright, Moxiecode Systems AB
- * Released under LGPL License.
- *
- * License: http://www.tinymce.com/license
- * Contributing: http://www.tinymce.com/contributing
- */
- /*global tinymce:true */
- tinymce.PluginManager.add('link', function(editor) {
- function createLinkList(callback) {
- return function() {
- var linkList = editor.settings.link_list;
- if (typeof(linkList) == "string") {
- tinymce.util.XHR.send({
- url: linkList,
- success: function(text) {
- callback(tinymce.util.JSON.parse(text));
- }
- });
- } else {
- callback(linkList);
- }
- };
- }
- function showDialog(linkList) {
- var data = {}, selection = editor.selection, dom = editor.dom, selectedElm, anchorElm, initialText;
- var win, onlyText, textListCtrl, linkListCtrl, relListCtrl, targetListCtrl, classListCtrl, linkTitleCtrl;
- function linkListChangeHandler(e) {
- var textCtrl = win.find('#text');
- if (!textCtrl.value() || (e.lastControl && textCtrl.value() == e.lastControl.text())) {
- textCtrl.value(e.control.text());
- }
- win.find('#href').value(e.control.value());
- }
- function buildLinkList() {
- var linkListItems = [{text: 'None', value: ''}];
- tinymce.each(linkList, function(link) {
- linkListItems.push({
- text: link.text || link.title,
- value: editor.convertURL(link.value || link.url, 'href'),
- menu: link.menu
- });
- });
- return linkListItems;
- }
- function applyPreview(items) {
- tinymce.each(items, function(item) {
- item.textStyle = function() {
- return editor.formatter.getCssText({inline: 'a', classes: [item.value]});
- };
- });
- return items;
- }
- function buildValues(listSettingName, dataItemName, defaultItems) {
- var selectedItem, items = [];
- tinymce.each(editor.settings[listSettingName] || defaultItems, function(target) {
- var item = {
- text: target.text || target.title,
- value: target.value
- };
- items.push(item);
- if (data[dataItemName] === target.value || (!selectedItem && target.selected)) {
- selectedItem = item;
- }
- });
- if (selectedItem && !data[dataItemName]) {
- data[dataItemName] = selectedItem.value;
- selectedItem.selected = true;
- }
- return items;
- }
- function buildAnchorListControl(url) {
- var anchorList = [];
- tinymce.each(editor.dom.select('a:not([href])'), function(anchor) {
- var id = anchor.name || anchor.id;
- if (id) {
- anchorList.push({
- text: id,
- value: '#' + id,
- selected: url.indexOf('#' + id) != -1
- });
- }
- });
- if (anchorList.length) {
- anchorList.unshift({text: 'None', value: ''});
- return {
- name: 'anchor',
- type: 'listbox',
- label: 'Anchors',
- values: anchorList,
- onselect: linkListChangeHandler
- };
- }
- }
- function urlChange() {
- if (linkListCtrl) {
- linkListCtrl.value(editor.convertURL(this.value(), 'href'));
- }
- if (!initialText && data.text.length === 0 && onlyText) {
- this.parent().parent().find('#text')[0].value(this.value());
- }
- }
- function isOnlyTextSelected(anchorElm) {
- var html = selection.getContent();
- // Partial html and not a fully selected anchor element
- if (/</.test(html) && (!/^<a [^>]+>[^<]+<\/a>$/.test(html) || html.indexOf('href=') == -1)) {
- return false;
- }
- if (anchorElm) {
- var nodes = anchorElm.childNodes, i;
- if (nodes.length === 0) {
- return false;
- }
- for (i = nodes.length - 1; i >= 0; i--) {
- if (nodes[i].nodeType != 3) {
- return false;
- }
- }
- }
- return true;
- }
- selectedElm = selection.getNode();
- anchorElm = dom.getParent(selectedElm, 'a[href]');
- onlyText = isOnlyTextSelected();
- data.text = initialText = anchorElm ? (anchorElm.innerText || anchorElm.textContent) : selection.getContent({format: 'text'});
- data.href = anchorElm ? dom.getAttrib(anchorElm, 'href') : '';
- data.target = anchorElm ? dom.getAttrib(anchorElm, 'target') : (editor.settings.default_link_target || null);
- data.rel = anchorElm ? dom.getAttrib(anchorElm, 'rel') : null;
- data['class'] = anchorElm ? dom.getAttrib(anchorElm, 'class') : null;
- data.title = anchorElm ? dom.getAttrib(anchorElm, 'title') : '';
- if (onlyText) {
- textListCtrl = {
- name: 'text',
- type: 'textbox',
- size: 40,
- label: 'Text to display',
- onchange: function() {
- data.text = this.value();
- }
- };
- }
- if (linkList) {
- linkListCtrl = {
- type: 'listbox',
- label: 'Link list',
- values: buildLinkList(),
- onselect: linkListChangeHandler,
- value: editor.convertURL(data.href, 'href'),
- onPostRender: function() {
- linkListCtrl = this;
- }
- };
- }
- if (editor.settings.target_list !== false) {
- targetListCtrl = {
- name: 'target',
- type: 'listbox',
- label: 'Target',
- values: buildValues('target_list', 'target', [{text: 'None', value: ''}, {text: 'New window', value: '_blank'}])
- };
- }
- if (editor.settings.rel_list) {
- relListCtrl = {
- name: 'rel',
- type: 'listbox',
- label: 'Rel',
- values: buildValues('rel_list', 'rel', [{text: 'None', value: ''}])
- };
- }
- if (editor.settings.link_class_list) {
- classListCtrl = {
- name: 'class',
- type: 'listbox',
- label: 'Class',
- values: applyPreview(buildValues('link_class_list', 'class'))
- };
- }
- if (editor.settings.link_title !== false) {
- linkTitleCtrl = {
- name: 'title',
- type: 'textbox',
- label: 'Title',
- value: data.title
- };
- }
- win = editor.windowManager.open({
- title: 'Insert link',
- data: data,
- body: [
- {
- name: 'href',
- type: 'filepicker',
- filetype: 'file',
- size: 40,
- autofocus: true,
- label: 'Url',
- onchange: urlChange,
- onkeyup: urlChange
- },
- textListCtrl,
- linkTitleCtrl,
- buildAnchorListControl(data.href),
- linkListCtrl,
- relListCtrl,
- targetListCtrl,
- classListCtrl
- ],
- onSubmit: function(e) {
- var href;
- data = tinymce.extend(data, e.data);
- href = data.href;
- // Delay confirm since onSubmit will move focus
- function delayedConfirm(message, callback) {
- var rng = editor.selection.getRng();
- window.setTimeout(function() {
- editor.windowManager.confirm(message, function(state) {
- editor.selection.setRng(rng);
- callback(state);
- });
- }, 0);
- }
- function insertLink() {
- var linkAttrs = {
- href: href,
- target: data.target ? data.target : null,
- rel: data.rel ? data.rel : null,
- "class": data["class"] ? data["class"] : null,
- title: data.title ? data.title : null
- };
- if (anchorElm) {
- editor.focus();
- if (onlyText && data.text != initialText) {
- if ("innerText" in anchorElm) {
- anchorElm.innerText = data.text;
- } else {
- anchorElm.textContent = data.text;
- }
- }
- dom.setAttribs(anchorElm, linkAttrs);
- selection.select(anchorElm);
- editor.undoManager.add();
- } else {
- if (onlyText) {
- editor.insertContent(dom.createHTML('a', linkAttrs, dom.encode(data.text)));
- } else {
- editor.execCommand('mceInsertLink', false, linkAttrs);
- }
- }
- }
- if (!href) {
- editor.execCommand('unlink');
- return;
- }
- // Is email and not //user@domain.com
- if (href.indexOf('@') > 0 && href.indexOf('//') == -1 && href.indexOf('mailto:') == -1) {
- delayedConfirm(
- 'The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?',
- function(state) {
- if (state) {
- href = 'mailto:' + href;
- }
- insertLink();
- }
- );
- return;
- }
- // Is www. prefixed
- if (/^\s*www\./i.test(href)) {
- delayedConfirm(
- 'The URL you entered seems to be an external link. Do you want to add the required http:// prefix?',
- function(state) {
- if (state) {
- href = 'http://' + href;
- }
- insertLink();
- }
- );
- return;
- }
- insertLink();
- }
- });
- }
- editor.addButton('link', {
- icon: 'link',
- tooltip: 'Insert/edit link',
- shortcut: 'Ctrl+K',
- onclick: createLinkList(showDialog),
- stateSelector: 'a[href]'
- });
- editor.addButton('unlink', {
- icon: 'unlink',
- tooltip: 'Remove link',
- cmd: 'unlink',
- stateSelector: 'a[href]'
- });
- editor.addShortcut('Ctrl+K', '', createLinkList(showDialog));
- this.showDialog = showDialog;
- editor.addMenuItem('link', {
- icon: 'link',
- text: 'Insert link',
- shortcut: 'Ctrl+K',
- onclick: createLinkList(showDialog),
- stateSelector: 'a[href]',
- context: 'insert',
- prependToContext: true
- });
- });
|