aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/drag-drop-mediator.js
blob: bf22aed2ce2e3940e9d995fed78257fc2073d4fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */

var Montage     = require("montage/core/core").Montage,
    Component   = require("montage/ui/component").Component,
    NJUtils     = require("js/lib/NJUtils").NJUtils;

exports.DragDropMediator = Montage.create(Component, {
    dropTarget: {
        value: null,
        writable: true
    },

    baseX: {
        value: null,
        writable: true
    },

    baseY: {
        value: null,
        writable: true
    },

    dropDelegate: {
        value: null
    },

    deserializedFromTemplate: {
        value: function() {
            this.eventManager.addEventListener("appLoaded", this, false);
        }
    },

    handleAppLoaded: {
        value: function() {
            this.dropTarget = this.application.ninja.stage.drawingCanvas;

            this.dropTarget.addEventListener("dragover", this, false);
            this.dropTarget.addEventListener("dragend", this, false);
            this.dropTarget.addEventListener("drop", this, false);
        }
    },

    handleEvent: {
        value: function(event){
            event.preventDefault();
            event.stopImmediatePropagation();

            switch(event.type) {
                case "dragover":
                    return false;
                case "dragend":
                    return false;
                case "drop":
                    this.handleDropEvent(event);
                    break;
                default:
                    console.log("Default");
                    break;
            }
        }
    },

    handleDropEvent: {
        value: function(evt){
            var xferString, component, file, files, idx, len;

            this.baseX = evt.offsetX - this.application.ninja.stage.userContentLeft;
            this.baseY = evt.offsetY - this.application.ninja.stage.userContentTop;

            xferString = evt.dataTransfer.getData("text/plain");
            if(xferString) {
                // If the drop is a component, call the delegate with the top,left coordinates
                if(xferString.indexOf("componentDrop") > -1) {
                    if(this.dropDelegate && typeof this.dropDelegate === 'object') {
                        this.dropDelegate.handleComponentDrop(this.baseX, this.baseY);
                        return;
                    }
                }

            }

            // Verify that browser supports FileReader API.
            if (typeof(window.FileReader) === "undefined") {
                alert("File API and FileReader APIs are not supported.");
                // Exit function since there isn't anything else we can do.
                return;
            }

            files = evt.dataTransfer.files;
            len = files.length;

            // Loop over all dragged files...
            for (idx = 0; idx < len; idx++) {
                file = files[idx];
                // Only do anything if the current file is an image (or has an image mime-type.
                if (file.type.match("^image/")) {
                    var reader = new FileReader();
                    // Create a LoadHandler to access each outer file var
                    reader.onload = this.createLoadHandler(file, this.baseX, this.baseY);

                    if(file.type.match("^image/svg\\+xml")) {// this is SVG
                        reader.readAsText(file);
                    } else{
                        reader.readAsDataURL(file);
                    }
                    
                }
            }

            return false;
        }
    },

    createLoadHandler: {
        value: function(file, baseX, baseY) {
            return function(evt2) {
                var domElem = null;

                if(file.type.match("^image/svg\\+xml")){ // this is an SVG file
                    var tempElem = document.createElement("div");
                    tempElem.innerHTML = evt2.currentTarget.result;
                    domElem = tempElem.children[0];

                    NJUtils.makeElementModel(domElem, "SVG", "block");
                } else { // treat as a regular image
                    domElem = NJUtils.makeNJElement("image", "image", "image");
                    domElem.src = evt2.currentTarget.result;
                }


                // Not sure we need an ID for the image
                                /*
                // Use the Image filename if valid for the id
                var filename = file.fileName.substr(0, file.fileName.lastIndexOf('.')) || file.fileName;
                filename = filename.replace(/ /g,"_");


                if(this.isValidFilename(filename)) {
                    //domElem.id = filename;
                } else {
                    //domElem.id = DocumentControllerModule.DocumentController.CreateElementID(img.tagName);
                }
                */

                var rules = {
                    'position': 'absolute',
                    'top' : baseY + 'px',
                    'left' : baseX + 'px',
                    '-webkit-transform-style' : 'preserve-3d',
                    '-webkit-transform' : 'perspective(1400) matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)'
                };

                NJevent("elementAdding", {el: domElem, data:rules});
            };
        }
    },

    isValidFilename: {
        value: function(id){
            if(id && id !== "") {
                var regexID = /^([a-zA-Z])+([a-zA-Z0-9_\.\:\-])+/;
                return(regexID.test(id))
            } else {
                return false;
            }
        }
    }


});