smartxml: Allow for describing node properties
[fnpeditor.git] / src / editor / plugins / core / links / linkElement.js
1 define(function(require) {
2     
3 'use strict';
4 /* globals gettext */
5
6
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);
13
14
15 _.extend(linkElement, {
16     init: function() {
17         genericElement.init.call(this);
18         _.bindAll(this, 'changeLink', 'deleteLink');
19
20         var linkText = this.wlxmlNode.getAttr('href') || '',
21             linkUrl = this.getUrl(linkText);
22
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);
26         this.box.hide();
27         this.addWidget(this.box);
28     },
29     markAsCurrent: function(toggle) {
30         this.box.toggle(toggle);
31     },
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));
37         }
38     },
39
40     changeLink: function(e) {
41         var el = this,
42             doc = this.wlxmlNode.document,
43             offset = el.canvas.getSelection().toDocumentFragment().offset,
44             dialog = Dialog.create({
45             title: gettext('Edit link'),
46             executeButtonText: gettext('Apply'),
47             cancelButtonText: gettext('Cancel'),
48             fields: [
49                 {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'),
50                 prePasteHandler: function(text) {
51                                     return this.wlxmlNode.document.getLinkForUrl(text);
52                                 }.bind(this)
53             }
54             ]
55         });
56         e.preventDefault();
57         e.stopPropagation();
58
59         dialog.on('execute', function(event) {
60             el.wlxmlNode.document.transaction(function() {
61                 el.wlxmlNode.setAttr('href', event.formData.href);
62                 event.success();
63             }, {
64                 metadata: {
65                     description: gettext('Edit link'),
66                     fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
67                 },
68                 success: function() {
69                     el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
70                 }
71             });
72         });
73         dialog.show();
74     },
75
76     deleteLink: function() {
77         var el = this,
78             doc = this.wlxmlNode.document;
79
80         el.wlxmlNode.document.transaction(function() {
81             var f = el.canvas.getSelection().toDocumentFragment(),
82                 prefLen = 0,
83                 ret;
84
85             if(el.wlxmlNode.isPrecededByTextNode()) {
86                 prefLen = el.wlxmlNode.prev().getText().length;
87             }
88
89             ret = el.wlxmlNode.unwrapContent();
90             return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
91         }, {
92             metadata: {
93                 description: gettext('Remove link'),
94                 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
95             },
96             success: function(ret) {
97                 el.canvas.select(ret);
98             }
99         });
100     },
101
102     getUrl: function(link) {
103         var pattern = /^[a-z]*:\/\//g;
104         if(!pattern.test(link)) {
105             link = 'http://' + link;
106         }
107         return this.wlxmlNode.document.getUrlForLink(link);
108     }
109 });
110
111 return linkElement;
112
113 });