503e958787b8cbeef2717d56d341410e8b9ec6d9
[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             dialog = Dialog.create({
44             title: gettext('Edit link'),
45             executeButtonText: gettext('Apply'),
46             cancelButtonText: gettext('Cancel'),
47             fields: [
48                 {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'),
49                 prePasteHandler: function(text) {
50                                     return this.wlxmlNode.document.getLinkForUrl(text);
51                                 }.bind(this)
52             }
53             ]
54         });
55         e.preventDefault();
56         e.stopPropagation();
57
58         dialog.on('execute', function(event) {
59             el.wlxmlNode.document.transaction(function() {
60                 el.wlxmlNode.setAttr('href', event.formData.href);
61                 event.success();
62             }, {
63                 metadata: {
64                     description: gettext('Edit link'),
65                     fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
66                 }
67             });
68         });
69         dialog.show();
70     },
71
72     deleteLink: function() {
73         var el = this,
74             doc = this.wlxmlNode.document;
75
76         el.wlxmlNode.document.transaction(function() {
77             el.wlxmlNode.unwrapContent();
78         }, {
79             metadata: {
80                 description: gettext('Remove link'),
81                 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
82             }
83         });
84     },
85
86     getUrl: function(link) {
87         var pattern = /^[a-z]*:\/\//g;
88         if(!pattern.test(link)) {
89             link = 'http://' + link;
90         }
91         return this.wlxmlNode.document.getUrlForLink(link);
92     }
93 });
94
95 return linkElement;
96
97 });