forked from camptocamp/cgxp
/
AddKMLFile.js
135 lines (112 loc) · 4.1 KB
/
AddKMLFile.js
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
/**
* Copyright (c) 2012 Camptocamp
*
* CGXP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CGXP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CGXP. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @requires plugins/Tool.js
* @include CGXP/widgets/GoogleEarthPanel.js
* @include Ext/examples/ux/fileuploadfield/FileUploadField.js
* @include Ext/ux/base64.js
*/
/** api: (define)
* module = cgxp.plugins
* class = AddKMLFile
*/
Ext.namespace("cgxp.plugins");
/** api: example
* Sample code showing how to add an AddKMLFile plugin to a
* `gxp.Viewer` is available in the `GoogleEarthView plugin <GoogleEarthView.html>`_.
*/
/** api: constructor
* .. class:: AddKMLFile(config)
*
* This plugin provides an "Add KML File" button that can be used to add a
* local KML file to both the 2D map and 3D Google Earth Plugin view (if
* present). As most browsers do not permit Javascript to read local files,
* this requires an "echo" service to be running on the server that reflects a
* file back to the client.
*/
/*
* TODO: Handle failures in the submit process.
*
* TODO: Use the HTML5 File API when available to avoid sending the file to
* the server and back.
*/
cgxp.plugins.AddKMLFile = Ext.extend(gxp.plugins.Tool, {
/** api: ptype = cgxp_addkmlfile */
ptype: "cgxp_addkmlfile",
/** api: config[echoUrl]
* ``String`` The "echo" service URL.
**/
echoUrl: null,
/** api: config[buttonText]
* ``String`` The "add kml" button text (i18n).
*/
buttonText: "Add KML File",
/** api: config[waitMsgText]
* ``String`` The "wait while loading" message text (i18n).
*/
waitMsgText: "Loading...",
/** private: method[addActions]
*/
addActions: function() {
var button = new Ext.ux.form.FileUploadField({
buttonOnly: true,
buttonText: this.buttonText,
name: "file",
listeners: {
fileselected: Ext.createDelegate(this.onFileselected, this)
}
});
this.form = new Ext.form.FormPanel({
unstyled: true,
fileUpload: true,
hideLabels: true,
items: button,
width: "auto"
});
return cgxp.plugins.AddKMLFile.superclass.addActions.apply(this, [this.form]);
},
onFileselected: function(button, value) {
this.form.getForm().submit({
url: this.echoUrl,
waitMsg: this.waitMsgText,
success: (function(form, action) {
var filename = action.result.filename;
var kmlString = Ext.ux.base64.decode(action.result.data);
// Add KML file to map
var map = this.target.mapPanel.map;
var kmlFormat = new OpenLayers.Format.KML({
extractStyles: true,
internalProjection: map.getProjectionObject()
});
var layer = new OpenLayers.Layer.Vector(filename);
layer.addFeatures(kmlFormat.read(kmlString));
map.addLayer(layer);
// Add KML file to GoogleEarthPanel
var googleEarthPanel = Ext.getCmp("googleearthpanel");
if (googleEarthPanel) {
var gePlugin = googleEarthPanel.earth;
if (gePlugin) {
var kmlObject = gePlugin.parseKml(kmlString);
gePlugin.getFeatures().appendChild(kmlObject);
}
}
form.reset();
}).createDelegate(this)
});
}
});
Ext.preg(cgxp.plugins.AddKMLFile.prototype.ptype, cgxp.plugins.AddKMLFile);