1 define(function(require) {
7 var $ = require('libs/jquery'),
8 _ = require('libs/underscore'),
9 genericElement = require('modules/documentCanvas/canvas/genericElement'),
10 Dialog = require('views/dialog/dialog'),
11 boxTemplate = require('libs/text!./box.html'),
12 attachments = require('views/attachments/attachments'),
13 linkElement = Object.create(genericElement);
16 _.extend(linkElement, {
18 genericElement.init.call(this);
19 _.bindAll(this, 'changeLink', 'deleteLink');
21 var linkText = this.wlxmlNode.getAttr('href') || '',
22 linkUrl = this.getUrl(linkText);
24 this.box = $(_.template(boxTemplate)({text: linkText, url: linkUrl}));
25 this.box.find('.change').on('click', this.changeLink);
26 this.box.find('.delete').on('click', this.deleteLink);
28 this.addWidget(this.box);
30 onStateChange: function(changes) {
31 genericElement.onStateChange.call(this, changes);
32 if(_.isBoolean(changes.active)) {
33 this.box.toggle(changes.active);
36 onNodeAttrChange: function(event) {
37 if(event.meta.attr === 'href') {
38 var link = this.box.find('[link]');
39 link.text(event.meta.newVal);
40 link.attr('href', this.getUrl(event.meta.newVal));
44 changeLink: function(e) {
46 doc = this.wlxmlNode.document,
47 offset = el.canvas.getSelection().toDocumentFragment().offset,
48 dialog = Dialog.create({
49 title: gettext('Edit link'),
50 executeButtonText: gettext('Apply'),
51 cancelButtonText: gettext('Cancel'),
53 {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'),
54 prePasteHandler: function(text) {
55 return this.wlxmlNode.document.getLinkForUrl(text);
57 description: '<a href="#-" class="attachment-library">' + gettext('attachment library') + '</a>'
64 dialog.on('execute', function(event) {
65 el.wlxmlNode.document.transaction(function() {
66 el.wlxmlNode.setAttr('href', event.formData.href);
70 description: gettext('Edit link'),
71 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
74 el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
79 $(".attachment-library", dialog.$el).on('click', function() {
80 attachments.select(function(v) {$("input", dialog.$el).val(v);});
85 deleteLink: function() {
87 doc = this.wlxmlNode.document;
89 el.wlxmlNode.document.transaction(function() {
90 var f = el.canvas.getSelection().toDocumentFragment(),
94 if(el.wlxmlNode.isPrecededByTextNode()) {
95 prefLen = el.wlxmlNode.prev().getText().length;
98 ret = el.wlxmlNode.unwrapContent();
99 return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
102 description: gettext('Remove link'),
103 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
105 success: function(ret) {
106 el.canvas.select(ret);
111 getUrl: function(link) {
112 var pattern = /^[a-z]*:\/\//g;
113 if(!pattern.test(link)) {
114 link = 'http://' + link;
116 return this.wlxmlNode.document.getUrlForLink(link);