Project

General

Profile

Download (12 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / flarecast / workspace / client-admin / js / def.js @ master

1
/***************************************
2
 * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
3
 *
4
 * This file is part of SITools2.
5
 *
6
 * SITools2 is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * SITools2 is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with SITools2.  If not, see <http://www.gnu.org/licenses/>.
18
 ***************************************/
19
/*global Ext, sitools, ID, i18n, document, Digest, window*/
20

    
21
Ext.define("sitools.admin.def", {
22
    singleton: true,
23
    require: ["sitools.public.utils.LoginDef"],
24

    
25
    init: function () {
26
        Ext.Ajax.on('requestexception', onRequestException, this);
27
    },
28

    
29
    initLocalizedVariables: function () {
30
        //Basic formFields Validation
31
        Ext.apply(Ext.form.VTypes, {
32
            'name': function () {
33
                var re = new RegExp("^.*[!\"#$%&\'()*+,:;<=>?@\\`{}|~/]+.*$");
34
                return function (v) {
35
                    return !re.test(v);
36
                };
37
            }(),
38
            'nameText': "Invalid caracters : should not contain . * [ ! \" # $ % & ' ( ) * + , : ; < = > ? @ \ ` { } | ~ / ] + . * $",
39
            'image': function () {
40
                var re = new RegExp("^[http://]+[/:a-zA-Z0-9-_]+.*(jpg|gif|png|bmp|ico)$");
41
                return function (v) {
42
                    return re.test(v);
43
                };
44
            }(),
45
            'imageText': "Invalid image URL : should start with http and finish with a image extension",
46
            'attachment': function () {
47
                var re1 = new RegExp("^/.*$");
48
                var re2 = new RegExp("^.*//.*$");
49
                var re3 = new RegExp("^.*[!\"#$%&\'()*+,:;<=>?@\\`{}|~]+.*$");
50
                return function (v) {
51
                    return (re1.test(v) && !re2.test(v) && !re3.test(v));
52
                };
53
            }(),
54
            'attachmentText': "invalid Attachment : should not contain ! \" # $ % & ' ( ) * + , : ; < = > ? @ \ ` { } | ~ ] + . * and must begin with /",
55
            'nameWithoutSpace': function () {
56
                var re = new RegExp("^.*[!\"#$%&\'()*+,:;<=>?@\\`{}|~ ]+.*$");
57
                return function (v) {
58
                    return !re.test(v);
59
                };
60
            }(),
61
            'nameWithoutSpaceText': i18n.get('label.invalidCharacters'),
62
            'withoutSpace': function () {
63
                var re = new RegExp("^.*[ ]+.*$");
64
                return function (v) {
65
                    return !re.test(v);
66
                };
67
            }(),
68
            'withoutSpaceText': i18n.get('label.invalidCharacters'),
69
            'uri': function () {
70
                var re = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
71
                var regEspace = new RegExp("^.*[ ]+$");
72
                return function (v) {
73
                    return re.test(v) && !regEspace.test(v);
74
                };
75
            }(),
76
            'uriText': "The format of the URL is invalid, should start with http, ftp or https and finish without space"
77
        });
78
    }
79
});
80

    
81

    
82
// GLOBAL BEHAVIOUR
83
var DEFAULT_NATIVEJSON = false; // can be set to true if greater than FF 3.5, IE
84
// 8, Opera 10.5, Webkit-based browsers
85
var DEFAULT_TIMEOUT = 30000; // server request timeout (msec)
86
var DEFAULT_TIMEBUF = 10; // time to wait before sending request (msec)
87
var DEFAULT_NBRETRY = 0;// 3; // nb of request retries (if failure)
88
var LOCALE = 'en';
89
var DEFAULT_HELP_WIDTH = 600;
90
var DEFAULT_HELP_HEIGHT = 400;
91
var ADMIN_PANEL_HEIGHT = 360;
92
var ADMIN_PANEL_NB_ELEMENTS = 10;
93
var SHOW_HELP = true;
94
var SITOOLS_DATE_FORMAT = 'Y-m-d\\TH:i:s.u';
95
var SITOOLS_DEFAULT_IHM_DATE_FORMAT = 'Y-m-d H:i:s.u';
96
var EXT_JS_VERSION = Ext.versions.extjs.version;
97
var EXT_JS_FOLDER = "extjs4/ext-" + EXT_JS_VERSION;
98

    
99
var JAVA_TYPES = [{
100
    name: "String"
101
}, {
102
    name: "Date"
103
}, {
104
    name: "Double"
105
}, {
106
    name: "Float"
107
}, {
108
    name: "Integer"
109
}, {
110
    name: "Boolean"
111
}, {
112
    name: "BasicDBObject"
113
}, {
114
    name: "BasicDBList"
115
}];
116
Ext.Ajax.defaultHeaders = {
117
    "Accept": "application/json",
118
    "X-User-Agent": "Sitools"
119
};
120

    
121
var helpUrl;
122
Ext.BLANK_IMAGE_URL = '/sitools/client-public/cots/' + EXT_JS_FOLDER + '/resources/themes/images/default/tree/s.gif';
123
Ext.USE_NATIVE_JSON = DEFAULT_NATIVEJSON;
124
Ext.Ajax.timeout = DEFAULT_TIMEOUT;
125
// Default headers to pass in every request
126

    
127
// GLOBAL PANELS
128
var treePanel, mainPanel, helpPanel;
129

    
130
var SERVER_OK = 200;
131

    
132
var predicatOperators = {
133
    operators: [['EQ', '='], ['GT', '>'], ['GTE', '>='], ['LT', '<'], ['LTE', '<='], ['LIKE', 'like'],
134
        ['NE', '!=']],
135

    
136
    getOperatorValueForServer: function (clientValue) {
137
        var value = null;
138
        Ext.each(this.operators, function (operator) {
139
            if (operator[1] == clientValue) {
140
                value = operator[0];
141
                return false;
142
            }
143
        }, this);
144
        return value;
145
    },
146

    
147
    getOperatorValueForClient: function (serverValue) {
148
        var value = null;
149
        Ext.each(this.operators, function (operator) {
150
            if (operator[0] == serverValue) {
151
                value = operator[1];
152
                return false;
153
            }
154
        }, this);
155
        return value;
156
    }
157
};
158

    
159
/**
160
 * Open a basic alert popup that shows the repsonse Message
161
 * @param {string} response the server response
162
 * @param {} opts the options of the Ajax Call
163
 */
164
function alertFailure(response, opts) {
165
    var txt;
166
    if (response.status == SERVER_OK) {
167
        var ret = Ext.decode(response.responseText).message;
168
        txt = i18n.get('msg.error') + ': ' + i18n.get(ret);
169
    } else {
170
        txt = i18n.get('warning.serverError') + ': ' + i18n.get(response.statusText);
171
    }
172
    Ext.Msg.alert(i18n.get('label.warning'), txt);
173
}
174

    
175
function showResponse(ret) {
176
    try {
177
        var Json = Ext.decode(ret.responseText);
178
        if (!Json.success) {
179
            Ext.Msg.alert(i18n.get('label.warning'), i18n.get(Json.message));
180
            return false;
181
        }
182
        popupMessage("", i18n.get(Json.message), loadUrl.get('APP_URL') + loadUrl.get('APP_CLIENT_PUBLIC_URL') + '/res/images/msgBox/16/icon-info.png');
183

    
184
        return true;
185
    } catch (err) {
186
        Ext.Msg.alert(i18n.get('label.warning'), i18n.get('warning.javascriptError') + " : " + err);
187
        return false;
188
    }
189
}
190

    
191
Ext.override(Ext.grid.GridPanel, {
192
    stripeRows: true
193
});
194

    
195
/**
196
 * Add a tooltip on every form field: tooltip could be an object like tooltip : {
197
 * text : string width : number }, or a simple string
198
 */
199
//TODO search to resolve bug tooltip on fields
200
Ext.override(Ext.form.field.Base, {
201
    tooltip: null,
202
    listeners: {
203
        afterrender: function () {
204
            if (!Ext.isEmpty(this.tooltip)) {
205
                var ttConfig = {};
206
                if (Ext.isString(this.tooltip)) {
207
                    ttConfig = {
208
                        html: this.tooltip,
209
                        width: 200,
210
                        dismissDelay: 5000
211
                    };
212
                }
213
                else if (Ext.isObject(this.tooltip)) {
214
                    ttConfig = this.tooltip;
215
                } else {
216
                    return;
217
                }
218
                Ext.apply(ttConfig, {
219
                    target: this.el
220
                });
221
                this.tTip = new Ext.ToolTip(ttConfig);
222
            }
223
//                        this.callParent(arguments);
224
        }
225
    }
226
});
227

    
228
//
229
function onClickOption(urloption) {
230
// CHROME : OK        
231

    
232
//  SOUS FIREFOX PB : preferer le tunneling si autorisation.
233

    
234
    var urltunnel = urloption + "?method=options&media=text/html";
235
    var saved = Ext.Ajax.defaultHeaders.Authorization;
236
    Ext.Ajax.defaultHeaders.Authorization = '';
237
    var savedScheme = Ext.util.Cookies.get('scheme');
238
    var savedHashCode = Ext.util.Cookies.get('hashCode');
239
    Ext.util.Cookies.set('scheme', null);
240
    Ext.util.Cookies.set('hashCode', null);
241

    
242
    Ext.Ajax.un('requestexception', onRequestException, this);
243

    
244
    Ext.Ajax.request({
245
        url: urltunnel,
246
        headers: {
247
            'Authorization': 'none'
248
        },
249
        method: "GET",
250
        success: function (response) {
251
            if (response.status == 200) {
252
                var OpenWindow = window.open(urltunnel);
253
            }
254
            // SINON ? ...
255
            Ext.Ajax.defaultHeaders.Authorization = saved;
256
            Ext.util.Cookies.set('scheme', savedScheme);
257
            Ext.util.Cookies.set('hashCode', savedHashCode);
258
            Ext.Ajax.on('requestexception', onRequestException, this);
259
        },
260
        callback: function () {
261
            Ext.util.Cookies.set('scheme', savedScheme);
262
            Ext.util.Cookies.set('hashCode', savedHashCode);
263
            Ext.Ajax.on('requestexception', onRequestException, this);
264
        },
265
        failure: function (response) {
266
            Ext.Ajax.defaultHeaders.Authorization = saved;
267
            Ext.util.Cookies.set('scheme', savedScheme);
268
            Ext.util.Cookies.set('hashCode', savedHashCode);
269
            if (response.status == 403) {
270
                Ext.Ajax.request({
271
                    url: urloption + "?media=text/html",
272
                    method: "OPTIONS",
273
                    success: function (response) {
274
                        var OpenWindow = window.open("", urloption);
275
                        OpenWindow.document.write(response.responseText);
276
                        OpenWindow.document.close();
277
                    },
278
                    failure: function (response) {
279
                    },
280
                    scope: this
281
                });
282
            } // SINON ? ...
283
            Ext.Ajax.on('requestexception', onRequestException, this);
284
        },
285
        scope: this
286
        //the scope in which to execute the callbacks
287
    });
288

    
289
}
290

    
291

    
292
Ext.override(Ext.menu.DatePicker, {
293
    initComponent: function () {
294
        this.on('beforeshow', this.onBeforeShow, this);
295
        if (this.strict == (Ext.isIE7 && Ext.isStrict)) {
296
            this.on('show', this.onShow, this, {single: true, delay: 20});
297
        }
298
        this.callParent();
299
    }
300
});
301

    
302
//DA Fix a bug on ExtJs 4.2.1 on grouping feature selection
303
Ext.define('App.overrides.view.Table', {
304
    override: 'Ext.view.Table',
305
    getRecord: function (node) {
306
        var me = this;
307
        if (me.dataSource.buffered) {
308
            node = this.getNode(node);
309
            if (node) {
310
                var recordIndex = node.getAttribute('data-recordIndex');
311
                if (recordIndex) {
312
                    recordIndex = parseInt(recordIndex, 10);
313
                    if (recordIndex > -1) {
314
                        // The index is the index in the original Store, not in
315
                        // a GroupStore
316
                        // The Grouping Feature increments the index to skip
317
                        // over unrendered records in collapsed groups
318
                        return this.store.data.getAt(recordIndex);
319
                    }
320
                }
321
                return this.dataSource.data.get(node.getAttribute('data-recordId'));
322
            }
323
        } else {
324
            node = this.getNode(node);
325
            if (node) {
326
                return this.dataSource.data.get(node.getAttribute('data-recordId'));
327
            }
328
        }
329
    },
330
    indexInStore: function (node) {
331
        var me = this;
332
        if (me.dataSource.buffered) {
333
            node = this.getNode(node, true);
334
            if (!node && node !== 0) {
335
                return -1;
336
            }
337
            var recordIndex = node.getAttribute('data-recordIndex');
338
            if (recordIndex) {
339
                return parseInt(recordIndex, 10);
340
            }
341
            return this.dataSource.indexOf(this.getRecord(node));
342
        } else {
343
            node = this.getNode(node, true);
344
            if (!node && node !== 0) {
345
                return -1;
346
            }
347
            return this.dataSource.indexOf(this.getRecord(node));
348
        }
349
    }
350
});