Project

General

Profile

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

git_sitools_idoc / flarecast / workspace / client-public-3.0 / js / utils / Utils.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
Ext.namespace('sitools.public.utils');
20

    
21
/**
22
 * An utility class to use in sitools.
23
 */
24
Ext.define('sitools.public.utils.Utils', {
25
    singleton : true,    
26
        /**
27
         * Transform an Array of Sitools properties (with field name, value) into an object.
28
         * @param {Array} array the array to transform
29
         * @return {Object} An object containing all properties as attributes.
30
         */
31
        arrayProperties2Object : function (array) {
32
                var result = {};
33
                Ext.each(array, function(item){
34
                        if (!Ext.isEmpty(item.name) && !Ext.isEmpty(item.value)) {
35
                                result[item.name] = item.value;
36
                        }
37
                });
38
                return result;
39
        }, 
40
        /**
41
         * Highlight a json string inside a <pre> html tag 
42
         */
43
        syntaxHighlight : function (json) {
44
        json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
45
        return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
46
            var cls = 'number';
47
            if (/^"/.test(match)) {
48
                if (/:$/.test(match)) {
49
                    cls = 'key';
50
                } else {
51
                    cls = 'string';
52
                }
53
            } else if (/true|false/.test(match)) {
54
                cls = 'boolean';
55
            } else if (/null/.test(match)) {
56
                cls = 'null';
57
            }
58
            return '<span class="' + cls + '">' + match + '</span>';
59
        });
60
    }
61
});
62

    
63
sitoolsUtils = sitools.public.utils.Utils;