| 1 |
define([ |
| 2 |
"dojo/_base/lang", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojox/string/tokenize", |
| 5 |
"dojox/string/sprintf", |
| 6 |
"../filter/htmlstrings", |
| 7 |
"../_base" |
| 8 |
], function(lang,array,Tokenize,Sprintf,htmlstrings,dd){ |
| 9 |
|
| 10 |
var strings = lang.getObject("filter.strings", true, dd); |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
lang.mixin(strings, { |
| 18 |
_urlquote: function( url, safe){ |
| 19 |
if(!safe){ |
| 20 |
safe = "/"; |
| 21 |
} |
| 22 |
return Tokenize(url, /([^\w-_.])/g, function(token){ |
| 23 |
if(safe.indexOf(token) == -1){ |
| 24 |
if(token == " "){ |
| 25 |
return "+"; |
| 26 |
}else{ |
| 27 |
var hex = token.charCodeAt(0).toString(16).toUpperCase(); |
| 28 |
while(hex.length < 2){ |
| 29 |
hex = "0" + hex; |
| 30 |
} |
| 31 |
return "%" + hex; |
| 32 |
} |
| 33 |
} |
| 34 |
return token; |
| 35 |
}).join(""); |
| 36 |
}, |
| 37 |
addslashes: function(value){ |
| 38 |
|
| 39 |
|
| 40 |
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/'/g, "\\'"); |
| 41 |
}, |
| 42 |
capfirst: function(value){ |
| 43 |
|
| 44 |
|
| 45 |
value = "" + value; |
| 46 |
return value.charAt(0).toUpperCase() + value.substring(1); |
| 47 |
}, |
| 48 |
center: function(value, arg){ |
| 49 |
|
| 50 |
|
| 51 |
arg = arg || value.length; |
| 52 |
value = value + ""; |
| 53 |
var diff = arg - value.length; |
| 54 |
if(diff % 2){ |
| 55 |
value = value + " "; |
| 56 |
diff -= 1; |
| 57 |
} |
| 58 |
for(var i = 0; i < diff; i += 2){ |
| 59 |
value = " " + value + " "; |
| 60 |
} |
| 61 |
return value; |
| 62 |
}, |
| 63 |
cut: function(value, arg){ |
| 64 |
|
| 65 |
|
| 66 |
arg = arg + "" || ""; |
| 67 |
value = value + ""; |
| 68 |
return value.replace(new RegExp(arg, "g"), ""); |
| 69 |
}, |
| 70 |
_fix_ampersands: /&(?!(\w+|#\d+);)/g, |
| 71 |
fix_ampersands: function(value){ |
| 72 |
|
| 73 |
|
| 74 |
return value.replace(strings._fix_ampersands, "&"); |
| 75 |
}, |
| 76 |
floatformat: function(value, arg){ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
arg = parseInt(arg || -1, 10); |
| 87 |
value = parseFloat(value); |
| 88 |
var m = value - value.toFixed(0); |
| 89 |
if(!m && arg < 0){ |
| 90 |
return value.toFixed(); |
| 91 |
} |
| 92 |
value = value.toFixed(Math.abs(arg)); |
| 93 |
return (arg < 0) ? parseFloat(value) + "" : value; |
| 94 |
}, |
| 95 |
iriencode: function(value){ |
| 96 |
return strings._urlquote(value, "/#%[]=:;$&()+,!"); |
| 97 |
}, |
| 98 |
linenumbers: function(value){ |
| 99 |
|
| 100 |
|
| 101 |
var df = dojox.dtl.filter; |
| 102 |
var lines = value.split("\n"); |
| 103 |
var output = []; |
| 104 |
var width = (lines.length + "").length; |
| 105 |
for(var i = 0, line; i < lines.length; i++){ |
| 106 |
line = lines[i]; |
| 107 |
output.push(df.strings.ljust(i + 1, width) + ". " + dojox.dtl._base.escape(line)); |
| 108 |
} |
| 109 |
return output.join("\n"); |
| 110 |
}, |
| 111 |
ljust: function(value, arg){ |
| 112 |
value = value + ""; |
| 113 |
arg = parseInt(arg, 10); |
| 114 |
while(value.length < arg){ |
| 115 |
value = value + " "; |
| 116 |
} |
| 117 |
return value; |
| 118 |
}, |
| 119 |
lower: function(value){ |
| 120 |
|
| 121 |
|
| 122 |
return (value + "").toLowerCase(); |
| 123 |
}, |
| 124 |
make_list: function(value){ |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
var output = []; |
| 129 |
if(typeof value == "number"){ |
| 130 |
value = value + ""; |
| 131 |
} |
| 132 |
if(value.charAt){ |
| 133 |
for(var i = 0; i < value.length; i++){ |
| 134 |
output.push(value.charAt(i)); |
| 135 |
} |
| 136 |
return output; |
| 137 |
} |
| 138 |
if(typeof value == "object"){ |
| 139 |
for(var key in value){ |
| 140 |
output.push(value[key]); |
| 141 |
} |
| 142 |
return output; |
| 143 |
} |
| 144 |
return []; |
| 145 |
}, |
| 146 |
rjust: function(value, arg){ |
| 147 |
value = value + ""; |
| 148 |
arg = parseInt(arg, 10); |
| 149 |
while(value.length < arg){ |
| 150 |
value = " " + value; |
| 151 |
} |
| 152 |
return value; |
| 153 |
}, |
| 154 |
slugify: function(value){ |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
value = value.replace(/[^\w\s-]/g, "").toLowerCase(); |
| 159 |
return value.replace(/[\-\s]+/g, "-"); |
| 160 |
}, |
| 161 |
_strings: {}, |
| 162 |
stringformat: function(value, arg){ |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
arg = "" + arg; |
| 168 |
var strs = strings._strings; |
| 169 |
if(!strs[arg]){ |
| 170 |
strs[arg] = new Sprintf.Formatter("%" + arg); |
| 171 |
} |
| 172 |
return strs[arg].format(value); |
| 173 |
}, |
| 174 |
title: function(value){ |
| 175 |
|
| 176 |
|
| 177 |
var last, title = ""; |
| 178 |
for(var i = 0, current; i < value.length; i++){ |
| 179 |
current = value.charAt(i); |
| 180 |
if(last == " " || last == "\n" || last == "\t" || !last){ |
| 181 |
title += current.toUpperCase(); |
| 182 |
}else{ |
| 183 |
title += current.toLowerCase(); |
| 184 |
} |
| 185 |
last = current; |
| 186 |
} |
| 187 |
return title; |
| 188 |
}, |
| 189 |
_truncatewords: /[ \n\r\t]/, |
| 190 |
truncatewords: function(value, arg){ |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
arg = parseInt(arg, 10); |
| 196 |
if(!arg){ |
| 197 |
return value; |
| 198 |
} |
| 199 |
|
| 200 |
for(var i = 0, j = value.length, count = 0, current, last; i < value.length; i++){ |
| 201 |
current = value.charAt(i); |
| 202 |
if(strings._truncatewords.test(last)){ |
| 203 |
if(!strings._truncatewords.test(current)){ |
| 204 |
++count; |
| 205 |
if(count == arg){ |
| 206 |
return value.substring(0, j + 1) + ' ...'; |
| 207 |
} |
| 208 |
} |
| 209 |
}else if(!strings._truncatewords.test(current)){ |
| 210 |
j = i; |
| 211 |
} |
| 212 |
last = current; |
| 213 |
} |
| 214 |
return value; |
| 215 |
}, |
| 216 |
_truncate_words: /(&.*?;|<.*?>|(\w[\w\-]*))/g, |
| 217 |
_truncate_tag: /<(\/)?([^ ]+?)(?: (\/)| .*?)?>/, |
| 218 |
_truncate_singlets: { br: true, col: true, link: true, base: true, img: true, param: true, area: true, hr: true, input: true }, |
| 219 |
truncatewords_html: function(value, arg){ |
| 220 |
arg = parseInt(arg, 10); |
| 221 |
|
| 222 |
if(arg <= 0){ |
| 223 |
return ""; |
| 224 |
} |
| 225 |
|
| 226 |
var words = 0; |
| 227 |
var open = []; |
| 228 |
|
| 229 |
var output = Tokenize(value, strings._truncate_words, function(all, word){ |
| 230 |
if(word){ |
| 231 |
|
| 232 |
++words; |
| 233 |
if(words < arg){ |
| 234 |
return word; |
| 235 |
}else if(words == arg){ |
| 236 |
return word + " ..."; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
var tag = all.match(strings._truncate_tag); |
| 241 |
if(!tag || words >= arg){ |
| 242 |
|
| 243 |
return; |
| 244 |
} |
| 245 |
var closing = tag[1]; |
| 246 |
var tagname = tag[2].toLowerCase(); |
| 247 |
var selfclosing = tag[3]; |
| 248 |
if(closing || strings._truncate_singlets[tagname]){ |
| 249 |
}else if(closing){ |
| 250 |
var i = array.indexOf(open, tagname); |
| 251 |
if(i != -1){ |
| 252 |
open = open.slice(i + 1); |
| 253 |
} |
| 254 |
}else{ |
| 255 |
open.unshift(tagname); |
| 256 |
} |
| 257 |
return all; |
| 258 |
}).join(""); |
| 259 |
|
| 260 |
output = output.replace(/\s+$/g, ""); |
| 261 |
|
| 262 |
for(var i = 0, tag; tag = open[i]; i++){ |
| 263 |
output += "</" + tag + ">"; |
| 264 |
} |
| 265 |
|
| 266 |
return output; |
| 267 |
}, |
| 268 |
upper: function(value){ |
| 269 |
return value.toUpperCase(); |
| 270 |
}, |
| 271 |
urlencode: function(value){ |
| 272 |
return strings._urlquote(value); |
| 273 |
}, |
| 274 |
_urlize: /^((?:[(>]|<)*)(.*?)((?:[.,)>\n]|>)*)$/, |
| 275 |
_urlize2: /^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$/, |
| 276 |
urlize: function(value){ |
| 277 |
return strings.urlizetrunc(value); |
| 278 |
}, |
| 279 |
urlizetrunc: function(value, arg){ |
| 280 |
arg = parseInt(arg); |
| 281 |
return Tokenize(value, /(\S+)/g, function(word){ |
| 282 |
var matches = strings._urlize.exec(word); |
| 283 |
if(!matches){ |
| 284 |
return word; |
| 285 |
} |
| 286 |
var lead = matches[1]; |
| 287 |
var middle = matches[2]; |
| 288 |
var trail = matches[3]; |
| 289 |
|
| 290 |
var startsWww = middle.indexOf("www.") == 0; |
| 291 |
var hasAt = middle.indexOf("@") != -1; |
| 292 |
var hasColon = middle.indexOf(":") != -1; |
| 293 |
var startsHttp = middle.indexOf("http://") == 0; |
| 294 |
var startsHttps = middle.indexOf("https://") == 0; |
| 295 |
var firstAlpha = /[a-zA-Z0-9]/.test(middle.charAt(0)); |
| 296 |
var last4 = middle.substring(middle.length - 4); |
| 297 |
|
| 298 |
var trimmed = middle; |
| 299 |
if(arg > 3){ |
| 300 |
trimmed = trimmed.substring(0, arg - 3) + "..."; |
| 301 |
} |
| 302 |
|
| 303 |
if(startsWww || (!hasAt && !startsHttp && middle.length && firstAlpha && (last4 == ".org" || last4 == ".net" || last4 == ".com"))){ |
| 304 |
return '<a href="http://' + middle + '" rel="nofollow">' + trimmed + '</a>'; |
| 305 |
}else if(startsHttp || startsHttps){ |
| 306 |
return '<a href="' + middle + '" rel="nofollow">' + trimmed + '</a>'; |
| 307 |
}else if(hasAt && !startsWww && !hasColon && strings._urlize2.test(middle)){ |
| 308 |
return '<a href="mailto:' + middle + '">' + middle + '</a>'; |
| 309 |
} |
| 310 |
return word; |
| 311 |
}).join(""); |
| 312 |
}, |
| 313 |
wordcount: function(value){ |
| 314 |
value = lang.trim(value); |
| 315 |
if(!value){ return 0; } |
| 316 |
return value.split(/\s+/g).length; |
| 317 |
}, |
| 318 |
wordwrap: function(value, arg){ |
| 319 |
arg = parseInt(arg); |
| 320 |
|
| 321 |
|
| 322 |
var output = []; |
| 323 |
var parts = value.split(/\s+/g); |
| 324 |
if(parts.length){ |
| 325 |
var word = parts.shift(); |
| 326 |
output.push(word); |
| 327 |
var pos = word.length - word.lastIndexOf("\n") - 1; |
| 328 |
for(var i = 0; i < parts.length; i++){ |
| 329 |
word = parts[i]; |
| 330 |
if(word.indexOf("\n") != -1){ |
| 331 |
var lines = word.split(/\n/g); |
| 332 |
}else{ |
| 333 |
var lines = [word]; |
| 334 |
} |
| 335 |
pos += lines[0].length + 1; |
| 336 |
if(arg && pos > arg){ |
| 337 |
output.push("\n"); |
| 338 |
pos = lines[lines.length - 1].length; |
| 339 |
}else{ |
| 340 |
output.push(" "); |
| 341 |
if(lines.length > 1){ |
| 342 |
pos = lines[lines.length - 1].length; |
| 343 |
} |
| 344 |
} |
| 345 |
output.push(word); |
| 346 |
} |
| 347 |
} |
| 348 |
return output.join(""); |
| 349 |
} |
| 350 |
}); |
| 351 |
|
| 352 |
return strings; |
| 353 |
}); |