boundries → boundaries
[fnpeditor.git] / src / editor / plugins / core / links / linkElement.js
index fdb60e1..6db8755 100644 (file)
@@ -9,6 +9,7 @@ var $ = require('libs/jquery'),
     genericElement = require('modules/documentCanvas/canvas/genericElement'),
     Dialog = require('views/dialog/dialog'),
     boxTemplate = require('libs/text!./box.html'),
+    attachments = require('views/attachments/attachments'),
     linkElement = Object.create(genericElement);
 
 
@@ -17,7 +18,7 @@ _.extend(linkElement, {
         genericElement.init.call(this);
         _.bindAll(this, 'changeLink', 'deleteLink');
 
-        var linkText = this.wlxmlNode.getAttr('href'),
+        var linkText = this.wlxmlNode.getAttr('href') || '',
             linkUrl = this.getUrl(linkText);
 
         this.box = $(_.template(boxTemplate)({text: linkText, url: linkUrl}));
@@ -26,8 +27,11 @@ _.extend(linkElement, {
         this.box.hide();
         this.addWidget(this.box);
     },
-    markAsCurrent: function(toggle) {
-        this.box.toggle(toggle);
+    onStateChange: function(changes) {
+        genericElement.onStateChange.call(this, changes);
+        if(_.isBoolean(changes.active)) {
+            this.box.toggle(changes.active);
+        }
     },
     onNodeAttrChange: function(event) {
         if(event.meta.attr === 'href') {
@@ -39,12 +43,19 @@ _.extend(linkElement, {
 
     changeLink: function(e) {
         var el = this,
+            doc = this.wlxmlNode.document,
+            offset = el.canvas.getSelection().toDocumentFragment().offset,
             dialog = Dialog.create({
             title: gettext('Edit link'),
             executeButtonText: gettext('Apply'),
             cancelButtonText: gettext('Cancel'),
             fields: [
-                {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href')}
+                {label: gettext('Link'), name: 'href', type: 'input', initialValue: el.wlxmlNode.getAttr('href'),
+                prePasteHandler: function(text) {
+                                    return this.wlxmlNode.document.getLinkForUrl(text);
+                                }.bind(this),
+                                description: '<a href="#-" class="attachment-library">' + gettext('attachment library') + '</a>'
+            }
             ]
         });
         e.preventDefault();
@@ -56,20 +67,43 @@ _.extend(linkElement, {
                 event.success();
             }, {
                 metadata: {
-                    description: gettext('Edit link')
+                    description: gettext('Edit link'),
+                    fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
+                },
+                success: function() {
+                    el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
                 }
             });
         });
         dialog.show();
+        $(".attachment-library", dialog.$el).on('click', function() {
+            attachments.select(function(v) {$("input", dialog.$el).val(v);});
+        });
+
     },
 
     deleteLink: function() {
-        var el = this;
+        var el = this,
+            doc = this.wlxmlNode.document;
+
         el.wlxmlNode.document.transaction(function() {
-            el.wlxmlNode.unwrapContent();
+            var f = el.canvas.getSelection().toDocumentFragment(),
+                prefLen = 0,
+                ret;
+
+            if(el.wlxmlNode.isPrecededByTextNode()) {
+                prefLen = el.wlxmlNode.prev().getText().length;
+            }
+
+            ret = el.wlxmlNode.unwrapContent();
+            return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
         }, {
             metadata: {
-                description: gettext('Remove link')
+                description: gettext('Remove link'),
+                fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
+            },
+            success: function(ret) {
+                el.canvas.select(ret);
             }
         });
     },