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 add_attachments = require('views/attachments/add_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),
25 if (linkText.substr(0,7) === 'file://') {
26 var filename = decodeURIComponent(linkText.substr(7));
28 badLink = (window.materials.indexOf(filename) < 0);
31 this.box = $(_.template(boxTemplate)({text: linkText, url: linkUrl, bad: badLink}));
32 this.box.find('.change').on('click', this.changeLink);
33 this.box.find('.delete').on('click', this.deleteLink);
35 this.addWidget(this.box);
37 onStateChange: function(changes) {
38 genericElement.onStateChange.call(this, changes);
39 if(_.isBoolean(changes.active)) {
40 this.box.toggle(changes.active);
43 onNodeAttrChange: function(event) {
44 if(event.meta.attr === 'href') {
45 var link = this.box.find('[link]');
46 link.text(event.meta.newVal);
47 link.attr('href', this.getUrl(event.meta.newVal));
51 changeLink: function(e) {
53 doc = this.wlxmlNode.document,
54 offset = el.canvas.getSelection().toDocumentFragment().offset,
55 dialog = Dialog.create({
56 title: gettext('Edit link'),
57 executeButtonText: gettext('Apply'),
58 cancelButtonText: gettext('Cancel'),
60 {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'),
61 prePasteHandler: function(text) {
62 return this.wlxmlNode.document.getLinkForUrl(text);
70 dialog.on('execute', function(event) {
71 el.wlxmlNode.document.transaction(function() {
72 el.wlxmlNode.setAttr('href', event.formData.href);
76 description: gettext('Edit link'),
77 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
80 el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
85 add_attachments(dialog);
88 deleteLink: function() {
90 doc = this.wlxmlNode.document;
92 el.wlxmlNode.document.transaction(function() {
93 var f = el.canvas.getSelection().toDocumentFragment(),
97 if(el.wlxmlNode.isPrecededByTextNode()) {
98 prefLen = el.wlxmlNode.prev().getText().length;
101 ret = el.wlxmlNode.unwrapContent();
102 return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
105 description: gettext('Remove link'),
106 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
108 success: function(ret) {
109 el.canvas.select(ret);
114 getUrl: function(link) {
115 var pattern = /^[a-z]*:\/\//g;
116 if(!pattern.test(link)) {
117 link = 'http://' + link;
119 return this.wlxmlNode.document.getUrlForLink(link);