From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dojox/dtl/filter/dates.js | 54 +++++ includes/js/dojox/dtl/filter/htmlstrings.js | 59 +++++ includes/js/dojox/dtl/filter/integers.js | 32 +++ includes/js/dojox/dtl/filter/lists.js | 137 ++++++++++++ includes/js/dojox/dtl/filter/logic.js | 34 +++ includes/js/dojox/dtl/filter/misc.js | 59 +++++ includes/js/dojox/dtl/filter/strings.js | 327 ++++++++++++++++++++++++++++ 7 files changed, 702 insertions(+) create mode 100644 includes/js/dojox/dtl/filter/dates.js create mode 100644 includes/js/dojox/dtl/filter/htmlstrings.js create mode 100644 includes/js/dojox/dtl/filter/integers.js create mode 100644 includes/js/dojox/dtl/filter/lists.js create mode 100644 includes/js/dojox/dtl/filter/logic.js create mode 100644 includes/js/dojox/dtl/filter/misc.js create mode 100644 includes/js/dojox/dtl/filter/strings.js (limited to 'includes/js/dojox/dtl/filter') diff --git a/includes/js/dojox/dtl/filter/dates.js b/includes/js/dojox/dtl/filter/dates.js new file mode 100644 index 0000000..3ca2022 --- /dev/null +++ b/includes/js/dojox/dtl/filter/dates.js @@ -0,0 +1,54 @@ +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"); + +(function(){ + var ddfd = dojox.dtl.filter.dates; + + dojo.mixin(ddfd, { + _toDate: function(value){ + if(value instanceof Date){ + return value; + } + value = new Date(value); + if(value.getTime() == new Date(0).getTime()){ + return ""; + } + return value; + }, + date: function(value, arg){ + // summary: Formats a date according to the given format + value = ddfd._toDate(value); + if(!value) 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 + value = ddfd._toDate(value); + if(!value) 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") + value = ddfd._toDate(value); + if(!value) return ""; + var timesince = dojox.dtl.utils.date.timesince; + 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") + value = ddfd._toDate(value); + if(!value) return ""; + var timesince = dojox.dtl.utils.date.timesince; + if(arg) return timesince(arg, value); + return timesince(new Date(), value); + } + }); +})(); + +} diff --git a/includes/js/dojox/dtl/filter/htmlstrings.js b/includes/js/dojox/dtl/filter/htmlstrings.js new file mode 100644 index 0000000..d4feb93 --- /dev/null +++ b/includes/js/dojox/dtl/filter/htmlstrings.js @@ -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, + _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

and
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, "
") + output.push("

" + part + "

"); + } + + return output.join("\n\n"); + }, + linebreaksbr: function(value){ + // summary: Converts newlines into
s + var dh = dojox.dtl.filter.htmlstrings; + return value.replace(dh._linebreaksrn, "\n").replace(dh._linebreaksbr, "
"); + }, + 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("]*>", "gi"), ""); + }, + striptags: function(value){ + // summary: Strips all [X]HTML tags + return value.replace(dojox.dtl.filter.htmlstrings._striptags, ""); + } +}); + +} diff --git a/includes/js/dojox/dtl/filter/integers.js b/includes/js/dojox/dtl/filter/integers.js new file mode 100644 index 0000000..0c54a90 --- /dev/null +++ b/includes/js/dojox/dtl/filter/integers.js @@ -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); + } +}); + +} diff --git a/includes/js/dojox/dtl/filter/lists.js b/includes/js/dojox/dtl/filter/lists.js new file mode 100644 index 0000000..b095242 --- /dev/null +++ b/includes/js/dojox/dtl/filter/lists.js @@ -0,0 +1,137 @@ +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 i, item, items = []; + if(!dojo.isArray(value)){ + var obj = value, value = []; + for(var key in obj){ + value.push(obj[k]); + } + } + for(i = 0; i < value.length; i++){ + items.push([new dojox.dtl._Filter('var.' + arg).resolve(new dojox.dtl._Context({ 'var' : value[i]})), value[i]]); + } + items.sort(dojox.dtl.filter.lists._dictsort); + var output = []; + for(i = 0; 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 i, indent = ""; + for(i = 0; i < tabs; i++){ + indent += "\t"; + } + if(value[1] && value[1].length){ + var recurse = []; + for(i = 0; i < value[1].length; i++){ + recurse.push(ddl._unordered_list(value[1][i], tabs + 1)) + } + return indent + "
  • " + value[0] + "\n" + indent + "\n" + indent + "
  • "; + }else{ + return indent + "
  • " + value[0] + "
  • "; + } + }, + unordered_list: function(value){ + // summary: + // Recursively takes a self-nested list and returns an HTML unordered list -- + // WITHOUT opening and closing