0ec3ca5bfdb320e7d11c8a7eff09acb09850e4f1
[fnpeditor.git] / src / editor / plugins / core / img / imgElement.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('src') || '',
22             linkUrl = this.getUrl(linkText);
23         
24         this._container().attr('style', 'background-image: url(\'' + linkUrl + '\');');
25
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);
29         this.box.hide();
30         this.addWidget(this.box);
31     },
32     onStateChange: function(changes) {
33         genericElement.onStateChange.call(this, changes);
34         if(_.isBoolean(changes.active)) {
35             this.box.toggle(changes.active);
36         }
37     },
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._container().attr('style', 'background-image: url(\'' + linkUrl + '\');');
45         }
46     },
47
48     changeLink: function(e) {
49         var el = this,
50             //doc = this.wlxmlNode.document,
51             //offset = el.canvas.getSelection().toDocumentFragment().offset,
52             dialog = Dialog.create({
53             title: gettext('Edit image'),
54             executeButtonText: gettext('Apply'),
55             cancelButtonText: gettext('Cancel'),
56             fields: [
57                 {label: gettext('Image'), name: 'src', type: 'input', initialValue: el.wlxmlNode.getAttr('src'),
58                 prePasteHandler: function(text) {
59                                     return this.wlxmlNode.document.getLinkForUrl(text);
60                                 }.bind(this),
61                                 description: '<a href="#-" class="attachment-library">attachment library</a>'
62             }
63             ]
64         });
65         e.preventDefault();
66         e.stopPropagation();
67
68         dialog.on('execute', function(event) {
69             el.wlxmlNode.document.transaction(function() {
70                 el.wlxmlNode.setAttr('src', event.formData.src);
71                 event.success();
72             }, {
73                 metadata: {
74                     description: gettext('Edit image')
75                     //fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
76                 },
77                 success: function() {
78                     //el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
79                 }
80             });
81         });
82         dialog.show();
83         $(".attachment-library", dialog.$el).on('click', function() {
84             attachments.select(function(v) {$("input", dialog.$el).val(v);});
85         });
86
87     },
88
89     deleteLink: function() {
90         var el = this,
91             doc = this.wlxmlNode.document;
92
93         el.wlxmlNode.document.transaction(function() {
94             //var f = el.canvas.getSelection().toDocumentFragment(),
95             //    prefLen = 0,
96             //    ret;
97             //
98             //if(el.wlxmlNode.isPrecededByTextNode()) {
99             //    prefLen = el.wlxmlNode.prev().getText().length;
100             //}
101             //
102             //ret = el.wlxmlNode.unwrapContent();
103             //return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
104             el.wlxmlNode.detach();
105         }, {
106             metadata: {
107                 description: gettext('Remove link')
108             }
109         });
110     },
111
112     getUrl: function(link) {
113         var pattern = /^[a-z]*:\/\//g;
114         if(!pattern.test(link) && !/^\//.test(link)) {
115             link = 'http://' + link;
116         }
117         return this.wlxmlNode.document.getUrlForLink(link);
118     }
119 });
120
121 return linkElement;
122
123 });