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 linkElement = Object.create(genericElement);
15 _.extend(linkElement, {
17 genericElement.init.call(this);
18 _.bindAll(this, 'changeLink', 'deleteLink');
20 var linkText = this.wlxmlNode.getAttr('href') || '',
21 linkUrl = this.getUrl(linkText);
23 this.box = $(_.template(boxTemplate)({text: linkText, url: linkUrl}));
24 this.box.find('.change').on('click', this.changeLink);
25 this.box.find('.delete').on('click', this.deleteLink);
27 this.addWidget(this.box);
29 markAsCurrent: function(toggle) {
30 this.box.toggle(toggle);
32 onNodeAttrChange: function(event) {
33 if(event.meta.attr === 'href') {
34 var link = this.box.find('[link]');
35 link.text(event.meta.newVal);
36 link.attr('href', this.getUrl(event.meta.newVal));
40 changeLink: function(e) {
42 dialog = Dialog.create({
43 title: gettext('Edit link'),
44 executeButtonText: gettext('Apply'),
45 cancelButtonText: gettext('Cancel'),
47 {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'),
48 prePasteHandler: function(text) {
49 return this.wlxmlNode.document.getLinkForUrl(text);
57 dialog.on('execute', function(event) {
58 el.wlxmlNode.document.transaction(function() {
59 el.wlxmlNode.setAttr('href', event.formData.href);
63 description: gettext('Edit link')
70 deleteLink: function() {
72 el.wlxmlNode.document.transaction(function() {
73 el.wlxmlNode.unwrapContent();
76 description: gettext('Remove link')
81 getUrl: function(link) {
82 var pattern = /^[a-z]*:\/\//g;
83 if(!pattern.test(link)) {
84 link = 'http://' + link;
86 return this.wlxmlNode.document.getUrlForLink(link);