forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
148 lines (131 loc) · 5.01 KB
/
utils.js
File metadata and controls
148 lines (131 loc) · 5.01 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
(function($, cloudStack) {
// General utils
cloudStack.serializeForm = function($form, options) {
if (!options) options = {};
var data = {};
$($form.serializeArray()).each(function() {
var dataItem = data[this.name];
var value = _s(this.value.toString());
if (options.escapeSlashes) {
value = value.replace(/\//g, '__forwardSlash__');
}
if (!dataItem) {
data[this.name] = value;
} else if (dataItem && !$.isArray(dataItem)) {
data[this.name] = [dataItem, value];
} else if ($.isArray(dataItem)) {
dataItem.push(value);
}
});
return data;
};
// Even/odd row handling
cloudStack.evenOdd = function($container, itemSelector, args) {
var even = false;
$container.find(itemSelector).each(function() {
var $elem = $(this);
if (even) {
even = false;
args.odd($elem);
} else {
even = true;
args.even($elem);
}
});
};
/**
* Localization -- shortcut _l
*
* Takes string and runs through localization function -- if no code
* exists or function isn't present, return string as-is
*/
cloudStack.localize = window._l = function(str) {
var localized = cloudStack.localizationFn ?
cloudStack.localizationFn(str) : null;
return localized ? localized : str;
};
/**
* Sanitize user input (HTML Encoding) -- shortcut _s
*
* Strip unwanted characters from user-based input
*/
cloudStack.sanitize = window._s = function(value) {
if (typeof(value) == "number") {
//alert("number does not need to be sanitized. Only string needs to be sanitized.");
return value;
} else if (typeof(value) == "boolean") {
//alert("boolean does not need to be sanitized. Only string needs to be sanitized.");
return value;
} else if (typeof(value) == "object") {
//alert("object cant not be sanitized. Only string can be sanitized.");
return value;
} else if (typeof(value) == null || typeof(value) == "undefined") {
return '';
}
var sanitized = value
.replace(/</g, "<")
.replace(/>/g, ">");
return sanitized;
};
/**
* Reverse sanitization (HTML Decoding)
*/
cloudStack.sanitizeReverse = function(value) {
var reversedValue = value
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
return reversedValue;
};
/**
* If the str.length is > maxLen,
* then concatenate and add '...' to the end of the string
*/
cloudStack.concat = function(str, maxLen) {
if (str.length > maxLen) {
return str.substr(0, maxLen) + '...';
} else {
return str;
}
};
/**
* Localize validator messages
*/
cloudStack.localizeValidatorMessages = function() {
$.extend($.validator.messages, {
required: _l('message.validate.fieldrequired'),
remote: _l('message.validate.fixfield'),
email: _l('message.validate.email.address'),
url: _l('message.validate.URL'),
date: _l('message.validate.date'),
dateISO: _l('message.validate.date.ISO'),
number: _l('message.validate.number'),
digits: _l('message.validate.digits'),
creditcard: _l('message.validate.creditcard'),
equalTo: _l('message.validate.equalto'),
accept: _l('message.validate.accept'),
maxlength: $.validator.format(_l('message.validate.maxlength')),
minlength: $.validator.format(_l('message.validate.minlength')),
rangelength: $.validator.format(_l('message.validate.range.length')),
range: $.validator.format(_l('message.validate.range')),
max: $.validator.format(_l('message.validate.max')),
min: $.validator.format(_l('messgae.validate.min'))
});
};
})(jQuery, cloudStack);