-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GeoNode-mixin.js
177 lines (172 loc) · 6.29 KB
/
GeoNode-mixin.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
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
175
176
177
Ext.ns("GeoNode");
/** api: constructor
* .. class:: ComposerMixin(config)
*
* A mixin class to add in functions needed by GeoNode.Composer as well as
* an SDK app.
*/
GeoNode.ComposerMixin = {
/** private: method[checkLayerPermissions]
* :arg layerRecord: ``GeoExt.data.LayerRecord`` The currently selected layer.
* :arg layerTreeId: ``gxp.LayerTree`` or ``gxp.LayerManager`` The layer tree identifier.
*
* Check the editing permissions on the layer and enable/disable the
* editing tools.
*/
checkLayerPermissions: function (layerRecord, layerTreeId) {
var buttons;
for (var key in this.tools) {
var tool = this.tools[key];
if (tool instanceof gxp.plugins.FeatureEditor) {
buttons = tool.actions;
break;
}
}
var toggleButtons = function(enabled) {
for (var i = 0; i < buttons.length; i++) {
enabled ? buttons[i].enable() : buttons[i].disable();
}
};
//Disable if layer is null or selected layer in tree doesn't match input layer
var tree_node = Ext.getCmp(layerTreeId).getSelectionModel().getSelectedNode();
if (layerRecord === null) {
toggleButtons(false);
}
else {
//Proceed if this is a local queryable WMS layer
var layer = layerRecord.getLayer();
if (layer instanceof OpenLayers.Layer.WMS && (layer.url == "/geoserver/wms" ||
layer.url.indexOf(app.localGeoServerBaseUrl.replace(app.urlPortRegEx, "$1/")) === 0)) {
Ext.Ajax.request({
/* TODO: use a template variable here if possible */
url:"/gs/" + layer.params.LAYERS + "/edit-check",
method:"POST",
success:function (response) {
var result = Ext.decode(response.responseText);
if (result.authorized === false) {
toggleButtons(false);
} else {
layer.displayOutsideMaxExtent = true;
toggleButtons(true);
}
},
failure:function () {
toggleButtons(false);
}
});
} else {
toggleButtons(false);
}
}
},
/* TODO: we are overriding a private method here for now
* The true solution would involve fixing:
* https://github.com/opengeo/gxp/issues/163
*/
showEmbedWindow: function() {
if (this.id) {
new Ext.Window({
title: this.publishActionText,
layout: "fit",
width: 380,
autoHeight: true,
items: [{
xtype: "gxp_embedmapdialog",
url: this.rest + this.id + "/embed"
}]
}).show();
}
},
/** private: method[getCRSFToken]
* Read the CSRFToken from the cookie.
*/
getCRSFToken: function() {
var csrfToken, csrfMatch = document.cookie.match(/csrftoken=(\w+)/);
if (csrfMatch && csrfMatch.length > 0) {
csrfToken = csrfMatch[1];
}
return csrfToken;
},
/** private: method[authenticate]
* Show the login dialog for the user to login.
*/
authenticate: function(options) {
var submit = function() {
form.getForm().submit({
waitMsg: "Logging in...",
success: function(form, action) {
this.setAuthorizedRoles(["ROLE_ADMINISTRATOR"]);
win.close();
OpenLayers.Request.DEFAULT_CONFIG.headers['X-CSRFToken'] = this.getCRSFToken();
// resend the original request
if (options) {
Ext.Ajax.request(options);
}
},
failure: function(form, action) {
var username = form.items.get(0);
var password = form.items.get(1);
username.markInvalid();
password.markInvalid();
username.focus(true);
},
scope: this
});
}.createDelegate(this);
var csrfToken = this.getCRSFToken();
var win = new Ext.Window({
title: "GeoNode Login",
modal: true,
width: 230,
autoHeight: true,
layout: "fit",
items: [{
xtype: "form",
autoHeight: true,
labelWidth: 55,
border: false,
bodyStyle: "padding: 10px;",
url: this.ajaxLoginUrl,
waitMsgTarget: true,
errorReader: {
// teach ExtJS a bit of RESTfulness
read: function(response) {
return {
success: response.status == 200,
records: []
};
}
},
defaults: {
anchor: "100%"
},
items: [{
xtype: "textfield",
name: "username",
fieldLabel: "Username"
}, {
xtype: "textfield",
name: "password",
fieldLabel: "Password",
inputType: "password"
}, {
xtype: "hidden",
name: "csrfmiddlewaretoken",
value: csrfToken
}, {
xtype: "button",
text: "Login",
inputType: "submit",
handler: submit
}]
}],
keys: {
"key": Ext.EventObject.ENTER,
"fn": submit
}
});
win.show();
var form = win.items.get(0);
form.items.get(0).focus(false, 100);
}
};