/trunk/api/js/dojo1.0/dojox/dtl/filter/misc.js |
---|
New file |
0,0 → 1,59 |
if(!dojo._hasResource["dojox.dtl.filter.misc"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
dojo._hasResource["dojox.dtl.filter.misc"] = true; |
dojo.provide("dojox.dtl.filter.misc"); |
dojo.mixin(dojox.dtl.filter.misc, { |
filesizeformat: function(value){ |
// summary: Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102bytes, etc). |
value = parseFloat(value); |
if(value < 1024){ |
return (value == 1) ? value + " byte" : value + " bytes"; |
}else if(value < 1024 * 1024){ |
return (value / 1024).toFixed(1) + " KB"; |
}else if(value < 1024 * 1024 * 1024){ |
return (value / 1024 / 1024).toFixed(1) + " MB"; |
} |
return (value / 1024 / 1024 / 1024).toFixed(1) + " GB"; |
}, |
pluralize: function(value, arg){ |
// summary: |
// Returns a plural suffix if the value is not 1, for '1 vote' vs. '2 votes' |
// description: |
// By default, 's' is used as a suffix; if an argument is provided, that string |
// is used instead. If the provided argument contains a comma, the text before |
// the comma is used for the singular case. |
arg = arg || 's'; |
if(arg.indexOf(",") == -1){ |
arg = "," + arg; |
} |
var parts = arg.split(","); |
if(parts.length > 2){ |
return ""; |
} |
var singular = parts[0]; |
var plural = parts[1]; |
if(parseInt(value) != 1){ |
return plural; |
} |
return singular; |
}, |
_phone2numeric: { a: 2, b: 2, c: 2, d: 3, e: 3, f: 3, g: 4, h: 4, i: 4, j: 5, k: 5, l: 5, m: 6, n: 6, o: 6, p: 7, r: 7, s: 7, t: 8, u: 8, v: 8, w: 9, x: 9, y: 9 }, |
phone2numeric: function(value){ |
// summary: Takes a phone number and converts it in to its numerical equivalent |
var dm = dojox.dtl.filter.misc; |
value = value + ""; |
var output = ""; |
for(var i = 0; i < value.length; i++){ |
var chr = value.charAt(i).toLowerCase(); |
(dm._phone2numeric[chr]) ? output += dm._phone2numeric[chr] : output += value.charAt(i); |
} |
return output; |
}, |
pprint: function(value){ |
// summary: A wrapper around toJson unless something better comes along |
return dojo.toJson(value); |
} |
}); |
} |
/trunk/api/js/dojo1.0/dojox/dtl/filter/logic.js |
---|
New file |
0,0 → 1,34 |
if(!dojo._hasResource["dojox.dtl.filter.logic"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
dojo._hasResource["dojox.dtl.filter.logic"] = true; |
dojo.provide("dojox.dtl.filter.logic"); |
dojo.mixin(dojox.dtl.filter.logic, { |
default_: function(value, arg){ |
// summary: If value is unavailable, use given default |
return value || arg || ""; |
}, |
default_if_none: function(value, arg){ |
// summary: If value is null, use given default |
return (value === null) ? arg || "" : value || ""; |
}, |
divisibleby: function(value, arg){ |
// summary: Returns true if the value is devisible by the argument" |
return (parseInt(value) % parseInt(arg)) == 0; |
}, |
_yesno: /\s*,\s*/g, |
yesno: function(value, arg){ |
// summary: |
// arg being a comma-delimited string, value of true/false/none |
// chooses the appropriate item from the string |
if(!arg) arg = 'yes,no,maybe'; |
var parts = arg.split(dojox.dtl.filter.logic._yesno); |
if(parts.length < 2){ |
return value; |
} |
if(value) return parts[0]; |
if((!value && value !== null) || parts.length < 3) return parts[1]; |
return parts[2]; |
} |
}); |
} |
/trunk/api/js/dojo1.0/dojox/dtl/filter/htmlstrings.js |
---|
New file |
0,0 → 1,59 |
if(!dojo._hasResource["dojox.dtl.filter.htmlstrings"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
dojo._hasResource["dojox.dtl.filter.htmlstrings"] = true; |
dojo.provide("dojox.dtl.filter.htmlstrings"); |
dojo.require("dojox.dtl._base"); |
dojo.mixin(dojox.dtl.filter.htmlstrings, { |
_escapeamp: /&/g, |
_escapelt: /</g, |
_escapegt: />/g, |
_escapeqt: /'/g, |
_escapedblqt: /"/g, |
_linebreaksrn: /(\r\n|\n\r)/g, |
_linebreaksn: /\n{2,}/g, |
_linebreakss: /(^\s+|\s+$)/g, |
_linebreaksbr: /\n/g, |
_removetagsfind: /[a-z0-9]+/g, |
_striptags: /<[^>]*?>/g, |
escape: function(value){ |
// summary: Escapes a string's HTML |
var dh = dojox.dtl.filter.htmlstrings; |
return value.replace(dh._escapeamp, '&').replace(dh._escapelt, '<').replace(dh._escapegt, '>').replace(dh._escapedblqt, '"').replace(dh._escapeqt, '''); |
}, |
linebreaks: function(value){ |
// summary: Converts newlines into <p> and <br />s |
var output = []; |
var dh = dojox.dtl.filter.htmlstrings; |
value = value.replace(dh._linebreaksrn, "\n"); |
var parts = value.split(dh._linebreaksn); |
for(var i = 0; i < parts.length; i++){ |
var part = parts[i].replace(dh._linebreakss, "").replace(dh._linebreaksbr, "<br />") |
output.push("<p>" + part + "</p>"); |
} |
return output.join("\n\n"); |
}, |
linebreaksbr: function(value){ |
// summary: Converts newlines into <br />s |
var dh = dojox.dtl.filter.htmlstrings; |
return value.replace(dh._linebreaksrn, "\n").replace(dh._linebreaksbr, "<br />"); |
}, |
removetags: function(value, arg){ |
// summary: Removes a space separated list of [X]HTML tags from the output" |
var dh = dojox.dtl.filter.htmlstrings; |
var tags = []; |
var group; |
while(group = dh._removetagsfind.exec(arg)){ |
tags.push(group[0]); |
} |
tags = "(" + tags.join("|") + ")"; |
return value.replace(new RegExp("</?\s*" + tags + "\s*[^>]*>", "gi"), ""); |
}, |
striptags: function(value){ |
// summary: Strips all [X]HTML tags |
return value.replace(dojox.dtl.filter.htmlstrings._striptags, ""); |
} |
}); |
} |
/trunk/api/js/dojo1.0/dojox/dtl/filter/lists.js |
---|
New file |
0,0 → 1,131 |
if(!dojo._hasResource["dojox.dtl.filter.lists"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
dojo._hasResource["dojox.dtl.filter.lists"] = true; |
dojo.provide("dojox.dtl.filter.lists") |
dojo.require("dojox.dtl._base"); |
dojo.mixin(dojox.dtl.filter.lists, { |
_dictsort: function(a, b){ |
if(a[0] == b[0]) return 0; |
return (a[0] < b[0]) ? -1 : 1; |
}, |
dictsort: function(value, arg){ |
// summary: Takes a list of dicts, returns that list sorted by the property given in the argument. |
if(!arg) return value; |
var items = []; |
for(var key in value){ |
items.push([dojox.dtl.resolveVariable('var.' + arg, new dojox.dtl.Context({ 'var' : value[key]})), value[key]]); |
} |
items.sort(dojox.dtl.filter.lists._dictsort); |
var output = []; |
for(var i = 0, item; item = items[i]; i++){ |
output.push(item[1]); |
} |
return output; |
}, |
dictsortreversed: function(value, arg){ |
// summary: Takes a list of dicts, returns that list sorted in reverse order by the property given in the argument. |
if(!arg) return value; |
var dictsort = dojox.dtl.filter.lists.dictsort(value, arg); |
return dictsort.reverse(); |
}, |
first: function(value){ |
// summary: Returns the first item in a list |
return (value.length) ? value[0] : ""; |
}, |
join: function(value, arg){ |
// summary: Joins a list with a string, like Python's ``str.join(list)`` |
// description: |
// Django throws a compile error, but JS can't do arg checks |
// so we're left with run time errors, which aren't wise for something |
// as trivial here as an empty arg. |
return value.join(arg || ","); |
}, |
length: function(value){ |
// summary: Returns the length of the value - useful for lists |
return (isNaN(value.length)) ? (value + "").length : value.length; |
}, |
length_is: function(value, arg){ |
// summary: Returns a boolean of whether the value's length is the argument |
return value.length == parseInt(arg); |
}, |
random: function(value){ |
// summary: Returns a random item from the list |
return value[Math.floor(Math.random() * value.length)]; |
}, |
slice: function(value, arg){ |
// summary: Returns a slice of the list. |
// description: |
// Uses the same syntax as Python's list slicing; see |
// http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice |
// for an introduction. |
// Also uses the optional third value to denote every X item. |
arg = arg || ""; |
var parts = arg.split(":"); |
var bits = []; |
for(var i = 0; i < parts.length; i++){ |
if(!parts[i].length){ |
bits.push(null); |
}else{ |
bits.push(parseInt(parts[i])); |
} |
} |
if(bits[0] === null){ |
bits[0] = 0; |
} |
if(bits[0] < 0){ |
bits[0] = value.length + bits[0]; |
} |
if(bits.length < 2 || bits[1] === null){ |
bits[1] = value.length; |
} |
if(bits[1] < 0){ |
bits[1] = value.length + bits[1]; |
} |
return value.slice(bits[0], bits[1]); |
}, |
_unordered_list: function(value, tabs){ |
var ddl = dojox.dtl.filter.lists; |
var indent = ""; |
for(var i = 0; i < tabs; i++){ |
indent += "\t"; |
} |
if(value[1] && value[1].length){ |
var recurse = []; |
for(var i = 0; i < value[1].length; i++){ |
recurse.push(ddl._unordered_list(value[1][i], tabs + 1)) |
} |
return indent + "<li>" + value[0] + "\n" + indent + "<ul>\n" + recurse.join("\n") + "\n" + indent + "</ul>\n" + indent + "</li>"; |
}else{ |
return indent + "<li>" + value[0] + "</li>"; |
} |
}, |
unordered_list: function(value){ |
// summary: |
// Recursively takes a self-nested list and returns an HTML unordered list -- |
// WITHOUT opening and closing <ul> tags. |
// description: |
// The list is assumed to be in the proper format. For example, if ``var`` contains |
// ``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``, |
// then ``{{ var|unordered_list }}`` would return:: |
// |
// <li>States |
// <ul> |
// <li>Kansas |
// <ul> |
// <li>Lawrence</li> |
// <li>Topeka</li> |
// </ul> |
// </li> |
// <li>Illinois</li> |
// </ul> |
// </li> |
return dojox.dtl.filter.lists._unordered_list(value, 1); |
} |
}); |
} |
/trunk/api/js/dojo1.0/dojox/dtl/filter/dates.js |
---|
New file |
0,0 → 1,36 |
if(!dojo._hasResource["dojox.dtl.filter.dates"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
dojo._hasResource["dojox.dtl.filter.dates"] = true; |
dojo.provide("dojox.dtl.filter.dates"); |
dojo.require("dojox.dtl.utils.date"); |
dojo.mixin(dojox.dtl.filter.dates, { |
date: function(value, arg){ |
// summary: Formats a date according to the given format |
if(!value || !(value instanceof Date)) return ""; |
arg = arg || "N j, Y"; |
return dojox.dtl.utils.date.format(value, arg); |
}, |
time: function(value, arg){ |
// summary: Formats a time according to the given format |
if(!value || !(value instanceof Date)) return ""; |
arg = arg || "P"; |
return dojox.dtl.utils.date.format(value, arg); |
}, |
timesince: function(value, arg){ |
// summary: Formats a date as the time since that date (i.e. "4 days, 6 hours") |
var timesince = dojox.dtl.utils.date.timesince; |
if(!value) return ""; |
if(arg) return timesince(arg, value); |
return timesince(value); |
}, |
timeuntil: function(value, arg){ |
// summary: Formats a date as the time until that date (i.e. "4 days, 6 hours") |
var timesince = dojox.dtl.utils.date.timesince; |
if(!value) return ""; |
if(arg) return timesince(arg, value); |
return timesince(new Date(), value); |
} |
}); |
} |
/trunk/api/js/dojo1.0/dojox/dtl/filter/integers.js |
---|
New file |
0,0 → 1,32 |
if(!dojo._hasResource["dojox.dtl.filter.integers"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
dojo._hasResource["dojox.dtl.filter.integers"] = true; |
dojo.provide("dojox.dtl.filter.integers"); |
dojo.mixin(dojox.dtl.filter.integers, { |
add: function(value, arg){ |
value = parseInt(value); |
arg = parseInt(arg); |
return isNaN(arg) ? value : value + arg; |
}, |
get_digit: function(value, arg){ |
// summary: |
// Given a whole number, returns the 1-based requested digit of it |
// desciprtion: |
// 1 is the right-most digit, 2 is the second-right-most digit, etc. Returns the |
// original value for invalid input (if input or argument is not an integer, |
// or if argument is less than 1). Otherwise, output is always an integer. |
value = parseInt(value); |
arg = parseInt(arg) - 1; |
if(arg >= 0){ |
value += ""; |
if(arg < value.length){ |
value = parseInt(value.charAt(arg)); |
}else{ |
value = 0; |
} |
} |
return (isNaN(value) ? 0 : value); |
} |
}); |
} |
/trunk/api/js/dojo1.0/dojox/dtl/filter/strings.js |
---|
New file |
0,0 → 1,312 |
if(!dojo._hasResource["dojox.dtl.filter.strings"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
dojo._hasResource["dojox.dtl.filter.strings"] = true; |
dojo.provide("dojox.dtl.filter.strings"); |
dojo.require("dojox.dtl.filter.htmlstrings"); |
dojo.require("dojox.string.sprintf"); |
dojo.require("dojox.string.tokenize"); |
dojo.mixin(dojox.dtl.filter.strings, { |
addslashes: function(value){ |
// summary: Adds slashes - useful for passing strings to JavaScript, for example. |
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/'/g, "\\'"); |
}, |
capfirst: function(value){ |
// summary: Capitalizes the first character of the value |
value = "" + value; |
return value.charAt(0).toUpperCase() + value.substring(1); |
}, |
center: function(value, arg){ |
// summary: Centers the value in a field of a given width |
arg = arg || value.length; |
value = value + ""; |
var diff = arg - value.length; |
if(diff % 2){ |
value = value + " "; |
diff -= 1; |
} |
for(var i = 0; i < diff; i += 2){ |
value = " " + value + " "; |
} |
return value; |
}, |
cut: function(value, arg){ |
// summary: Removes all values of arg from the given string |
arg = arg + "" || ""; |
value = value + ""; |
return value.replace(new RegExp(arg, "g"), ""); |
}, |
_fix_ampersands: /&(?!(\w+|#\d+);)/g, |
fix_ampersands: function(value){ |
// summary: Replaces ampersands with ``&`` entities |
return value.replace(dojox.dtl.filter.strings._fix_ampersands, "&"); |
}, |
floatformat: function(value, arg){ |
// summary: Format a number according to arg |
// description: |
// If called without an argument, displays a floating point |
// number as 34.2 -- but only if there's a point to be displayed. |
// With a positive numeric argument, it displays that many decimal places |
// always. |
// With a negative numeric argument, it will display that many decimal |
// places -- but only if there's places to be displayed. |
arg = parseInt(arg || -1); |
value = parseFloat(value); |
var m = value - value.toFixed(0); |
if(!m && arg < 0){ |
return value.toFixed(); |
} |
value = value.toFixed(Math.abs(arg)); |
return (arg < 0) ? parseFloat(value) + "" : value; |
}, |
iriencode: function(value){ |
return dojox.dtl.text.urlquote(value, "/#%[]=:;$&()+,!"); |
}, |
linenumbers: function(value){ |
// summary: Displays text with line numbers |
var df = dojox.dtl.filter; |
var lines = value.split("\n"); |
var output = []; |
var width = (lines.length + "").length; |
for(var i = 0, line; i < lines.length; i++){ |
line = lines[i]; |
output.push(df.strings.ljust(i + 1, width) + ". " + df.htmlstrings.escape(line)); |
} |
return output.join("\n"); |
}, |
ljust: function(value, arg){ |
value = value + ""; |
arg = parseInt(arg); |
while(value.length < arg){ |
value = value + " "; |
} |
return value; |
}, |
lower: function(value){ |
// summary: Converts a string into all lowercase |
return (value + "").toLowerCase(); |
}, |
make_list: function(value){ |
// summary: |
// Returns the value turned into a list. For an integer, it's a list of |
// digits. For a string, it's a list of characters. |
var output = []; |
if(typeof value == "number"){ |
value = value + ""; |
} |
if(value.charAt){ |
for(var i = 0; i < value.length; i++){ |
output.push(value.charAt(i)); |
} |
return output; |
} |
if(typeof value == "object"){ |
for(var key in value){ |
output.push(value[key]); |
} |
return output; |
} |
return []; |
}, |
rjust: function(value, arg){ |
value = value + ""; |
arg = parseInt(arg); |
while(value.length < arg){ |
value = " " + value; |
} |
return value; |
}, |
slugify: function(value){ |
// summary: Converts to lowercase, removes |
// non-alpha chars and converts spaces to hyphens |
value = value.replace(/[^\w\s-]/g, "").toLowerCase(); |
return value.replace(/[\-\s]+/g, "-"); |
}, |
_strings: {}, |
stringformat: function(value, arg){ |
// summary: |
// Formats the variable according to the argument, a string formatting specifier. |
// This specifier uses Python string formating syntax, with the exception that |
// the leading "%" is dropped. |
arg = "" + arg; |
var strings = dojox.dtl.filter.strings._strings; |
if(!strings[arg]){ |
strings[arg] = new dojox.string.sprintf.Formatter("%" + arg); |
} |
return strings[arg].format(value); |
}, |
title: function(value){ |
// summary: Converts a string into titlecase |
var last, title = ""; |
for(var i = 0, current; i < value.length; i++){ |
current = value.charAt(i); |
if(last == " " || last == "\n" || last == "\t" || !last){ |
title += current.toUpperCase(); |
}else{ |
title += current.toLowerCase(); |
} |
last = current; |
} |
return title; |
}, |
_truncatewords: /[ \n\r\t]/, |
truncatewords: function(value, arg){ |
// summary: Truncates a string after a certain number of words |
// arg: Integer |
// Number of words to truncate after |
arg = parseInt(arg); |
if(!arg){ |
return value; |
} |
for(var i = 0, j = value.length, count = 0, current, last; i < value.length; i++){ |
current = value.charAt(i); |
if(dojox.dtl.filter.strings._truncatewords.test(last)){ |
if(!dojox.dtl.filter.strings._truncatewords.test(current)){ |
++count; |
if(count == arg){ |
return value.substring(0, j + 1); |
} |
} |
}else if(!dojox.dtl.filter.strings._truncatewords.test(current)){ |
j = i; |
} |
last = current; |
} |
return value; |
}, |
_truncate_words: /(&.*?;|<.*?>|(\w[\w-]*))/g, |
_truncate_tag: /<(\/)?([^ ]+?)(?: (\/)| .*?)?>/, |
_truncate_singlets: { br: true, col: true, link: true, base: true, img: true, param: true, area: true, hr: true, input: true }, |
truncatewords_html: function(value, arg){ |
arg = parseInt(arg); |
if(arg <= 0){ |
return ""; |
} |
var strings = dojox.dtl.filter.strings; |
var words = 0; |
var open = []; |
var output = dojox.string.tokenize(value, strings._truncate_words, function(all, word){ |
if(word){ |
// It's an actual non-HTML word |
++words; |
if(words < arg){ |
return word; |
}else if(words == arg){ |
return word + " ..."; |
} |
} |
// Check for tag |
var tag = all.match(strings._truncate_tag); |
if(!tag || words >= arg){ |
// Don't worry about non tags or tags after our truncate point |
return; |
} |
var closing = tag[1]; |
var tagname = tag[2].toLowerCase(); |
var selfclosing = tag[3]; |
if(closing || strings._truncate_singlets[tagname]){ |
}else if(closing){ |
var i = dojo.indexOf(open, tagname); |
if(i != -1){ |
open = open.slice(i + 1); |
} |
}else{ |
open.unshift(tagname); |
} |
return all; |
}).join(""); |
output = output.replace(/\s+$/g, ""); |
for(var i = 0, tag; tag = open[i]; i++){ |
output += "</" + tag + ">"; |
} |
return output; |
}, |
upper: function(value){ |
return value.toUpperCase(); |
}, |
urlencode: function(value){ |
return dojox.dtl.text.urlquote(value); |
}, |
_urlize: /^((?:[(>]|<)*)(.*?)((?:[.,)>\n]|>)*)$/, |
_urlize2: /^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$/, |
urlize: function(value){ |
return dojox.dtl.filter.strings.urlizetrunc(value); |
}, |
urlizetrunc: function(value, arg){ |
arg = parseInt(arg); |
return dojox.string.tokenize(value, /(\S+)/g, function(word){ |
var matches = dojox.dtl.filter.strings._urlize.exec(word); |
if(!matches){ |
return word; |
} |
var lead = matches[1]; |
var middle = matches[2]; |
var trail = matches[3]; |
var startsWww = middle.indexOf("www.") == 0; |
var hasAt = middle.indexOf("@") != -1; |
var hasColon = middle.indexOf(":") != -1; |
var startsHttp = middle.indexOf("http://") == 0; |
var startsHttps = middle.indexOf("https://") == 0; |
var firstAlpha = /[a-zA-Z0-9]/.test(middle.charAt(0)); |
var last4 = middle.substring(middle.length - 4); |
var trimmed = middle; |
if(arg > 3){ |
trimmed = trimmed.substring(0, arg - 3) + "..."; |
} |
if(startsWww || (!hasAt && !startsHttp && middle.length && firstAlpha && (last4 == ".org" || last4 == ".net" || last4 == ".com"))){ |
return '<a href="http://' + middle + '" rel="nofollow">' + trimmed + '</a>'; |
}else if(startsHttp || startsHttps){ |
return '<a href="' + middle + '" rel="nofollow">' + trimmed + '</a>'; |
}else if(hasAt && !startsWww && !hasColon && dojox.dtl.filter.strings._urlize2.test(middle)){ |
return '<a href="mailto:' + middle + '">' + middle + '</a>'; |
} |
return word; |
}).join(""); |
}, |
wordcount: function(value){ |
return dojox.dtl.text.pySplit(value).length; |
}, |
wordwrap: function(value, arg){ |
arg = parseInt(arg); |
// summary: Wraps words at specified line length |
var output = []; |
var parts = value.split(/ /g); |
if(parts.length){ |
var word = parts.shift(); |
output.push(word); |
var pos = word.length - word.lastIndexOf("\n") - 1; |
for(var i = 0; i < parts.length; i++){ |
word = parts[i]; |
if(word.indexOf("\n") != -1){ |
var lines = word.split(/\n/g); |
}else{ |
var lines = [word]; |
} |
pos += lines[0].length + 1; |
if(arg && pos > arg){ |
output.push("\n"); |
pos = lines[lines.length - 1].length; |
}else{ |
output.push(" "); |
if(lines.length > 1){ |
pos = lines[lines.length - 1].length; |
} |
} |
output.push(word); |
} |
} |
return output.join(""); |
} |
}); |
} |