36163d162bdb617d139b85388ad2de8a6bcaef71
[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     attachments = require('views/attachments/attachments'),
13     linkElement = Object.create(genericElement);
14
15
16 _.extend(linkElement, {
17     init: function() {
18         genericElement.init.call(this);
19         _.bindAll(this, 'changeLink', 'deleteLink');
20
21         var linkText = this.wlxmlNode.getAttr('href') || '',
22             linkUrl = this.getUrl(linkText);
23
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);
27         this.box.hide();
28         this.addWidget(this.box);
29     },
30     onStateChange: function(changes) {
31         genericElement.onStateChange.call(this, changes);
32         if(_.isBoolean(changes.active)) {
33             this.box.toggle(changes.active);
34         }
35     },
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));
41         }
42     },
43
44     changeLink: function(e) {
45         var el = this,
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'),
52             fields: [
53                 {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'),
54                 prePasteHandler: function(text) {
55                                     return this.wlxmlNode.document.getLinkForUrl(text);
56                                 }.bind(this),
57                                 description: '<a href="#-" class="attachment-library">attachment library</a>'
58             }
59             ]
60         });
61         e.preventDefault();
62         e.stopPropagation();
63
64         dialog.on('execute', function(event) {
65             el.wlxmlNode.document.transaction(function() {
66                 el.wlxmlNode.setAttr('href', event.formData.href);
67                 event.success();
68             }, {
69                 metadata: {
70                     description: gettext('Edit link'),
71                     fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
72                 },
73                 success: function() {
74                     el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
75                 }
76             });
77         });
78         dialog.show();
79         $(".attachment-library", dialog.$el).on('click', function() {
80             attachments.select(function(v) {$("input", dialog.$el).val(v);});
81         });
82
83     },
84
85     deleteLink: function() {
86         var el = this,
87             doc = this.wlxmlNode.document;
88
89         el.wlxmlNode.document.transaction(function() {
90             var f = el.canvas.getSelection().toDocumentFragment(),
91                 prefLen = 0,
92                 ret;
93
94             if(el.wlxmlNode.isPrecededByTextNode()) {
95                 prefLen = el.wlxmlNode.prev().getText().length;
96             }
97
98             ret = el.wlxmlNode.unwrapContent();
99             return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
100         }, {
101             metadata: {
102                 description: gettext('Remove link'),
103                 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
104             },
105             success: function(ret) {
106                 el.canvas.select(ret);
107             }
108         });
109     },
110
111     getUrl: function(link) {
112         var pattern = /^[a-z]*:\/\//g;
113         if(!pattern.test(link)) {
114             link = 'http://' + link;
115         }
116         return this.wlxmlNode.document.getUrlForLink(link);
117     }
118 });
119
120 return linkElement;
121
122 });