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('src') || '',
22 linkUrl = this.getUrl(linkText);
24 this.refreshLink(linkUrl);
26 this.box = $(_.template(boxTemplate)({text: linkText, url: linkUrl}));
27 this.box.find('.change').on('click', this.changeLink);
28 this.box.find('.delete').on('click', this.deleteLink);
30 this.addWidget(this.box);
32 onStateChange: function(changes) {
33 genericElement.onStateChange.call(this, changes);
34 if(_.isBoolean(changes.active)) {
35 this.box.toggle(changes.active);
38 onNodeAttrChange: function(event) {
39 if(event.meta.attr === 'src') {
40 var link = this.box.find('[link]');
41 link.text(event.meta.newVal);
42 var linkUrl = this.getUrl(event.meta.newVal);
43 link.attr('href', linkUrl);
44 this.refreshLink(linkUrl);
48 refreshLink: function(linkUrl) {
49 this._container().text('');
50 this._container().attr('style', 'background-image: url(\'' + linkUrl + '\');');
53 changeLink: function(e) {
55 //doc = this.wlxmlNode.document,
56 //offset = el.canvas.getSelection().toDocumentFragment().offset,
57 dialog = Dialog.create({
58 title: gettext('Edit image'),
59 executeButtonText: gettext('Apply'),
60 cancelButtonText: gettext('Cancel'),
62 {label: gettext('Image'), name: 'src', type: 'input', initialValue: el.wlxmlNode.getAttr('src'),
63 prePasteHandler: function(text) {
64 return this.wlxmlNode.document.getLinkForUrl(text);
66 description: '<a href="#-" class="attachment-library">attachment library</a>'
73 dialog.on('execute', function(event) {
74 el.wlxmlNode.document.transaction(function() {
75 el.wlxmlNode.setAttr('src', event.formData.src);
79 description: gettext('Edit image')
80 //fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
83 //el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
88 $(".attachment-library", dialog.$el).on('click', function() {
89 attachments.select(function(v) {$("input", dialog.$el).val(v);});
94 deleteLink: function() {
96 doc = this.wlxmlNode.document;
98 el.wlxmlNode.document.transaction(function() {
99 //var f = el.canvas.getSelection().toDocumentFragment(),
103 //if(el.wlxmlNode.isPrecededByTextNode()) {
104 // prefLen = el.wlxmlNode.prev().getText().length;
107 //ret = el.wlxmlNode.unwrapContent();
108 //return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
109 el.wlxmlNode.detach();
112 description: gettext('Remove link')
117 getUrl: function(link) {
118 var pattern = /^[a-z]*:\/\//g;
119 if(!pattern.test(link) && !/^\//.test(link)) {
120 link = 'http://' + link;
122 return this.wlxmlNode.document.getUrlForLink(link);