/public/javascripts/dojo/release/dojo/dojox/dtl/filter/lists.js

http://enginey.googlecode.com/ · JavaScript · 144 lines · 94 code · 9 blank · 41 comment · 23 complexity · 6bc1a5156059fc199b5921cfabd84daa MD5 · raw file

  1. /*
  2. Copyright (c) 2004-2008, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.dtl.filter.lists"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.dtl.filter.lists"] = true;
  8. dojo.provide("dojox.dtl.filter.lists")
  9. dojo.require("dojox.dtl._base");
  10. dojo.mixin(dojox.dtl.filter.lists, {
  11. _dictsort: function(a, b){
  12. if(a[0] == b[0]) return 0;
  13. return (a[0] < b[0]) ? -1 : 1;
  14. },
  15. dictsort: function(value, arg){
  16. // summary: Takes a list of dicts, returns that list sorted by the property given in the argument.
  17. if(!arg) return value;
  18. var i, item, items = [];
  19. if(!dojo.isArray(value)){
  20. var obj = value, value = [];
  21. for(var key in obj){
  22. value.push(obj[k]);
  23. }
  24. }
  25. for(i = 0; i < value.length; i++){
  26. items.push([new dojox.dtl._Filter('var.' + arg).resolve(new dojox.dtl._Context({ 'var' : value[i]})), value[i]]);
  27. }
  28. items.sort(dojox.dtl.filter.lists._dictsort);
  29. var output = [];
  30. for(i = 0; item = items[i]; i++){
  31. output.push(item[1]);
  32. }
  33. return output;
  34. },
  35. dictsortreversed: function(value, arg){
  36. // summary: Takes a list of dicts, returns that list sorted in reverse order by the property given in the argument.
  37. if(!arg) return value;
  38. var dictsort = dojox.dtl.filter.lists.dictsort(value, arg);
  39. return dictsort.reverse();
  40. },
  41. first: function(value){
  42. // summary: Returns the first item in a list
  43. return (value.length) ? value[0] : "";
  44. },
  45. join: function(value, arg){
  46. // summary: Joins a list with a string, like Python's ``str.join(list)``
  47. // description:
  48. // Django throws a compile error, but JS can't do arg checks
  49. // so we're left with run time errors, which aren't wise for something
  50. // as trivial here as an empty arg.
  51. return value.join(arg || ",");
  52. },
  53. length: function(value){
  54. // summary: Returns the length of the value - useful for lists
  55. return (isNaN(value.length)) ? (value + "").length : value.length;
  56. },
  57. length_is: function(value, arg){
  58. // summary: Returns a boolean of whether the value's length is the argument
  59. return value.length == parseInt(arg);
  60. },
  61. random: function(value){
  62. // summary: Returns a random item from the list
  63. return value[Math.floor(Math.random() * value.length)];
  64. },
  65. slice: function(value, arg){
  66. // summary: Returns a slice of the list.
  67. // description:
  68. // Uses the same syntax as Python's list slicing; see
  69. // http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
  70. // for an introduction.
  71. // Also uses the optional third value to denote every X item.
  72. arg = arg || "";
  73. var parts = arg.split(":");
  74. var bits = [];
  75. for(var i = 0; i < parts.length; i++){
  76. if(!parts[i].length){
  77. bits.push(null);
  78. }else{
  79. bits.push(parseInt(parts[i]));
  80. }
  81. }
  82. if(bits[0] === null){
  83. bits[0] = 0;
  84. }
  85. if(bits[0] < 0){
  86. bits[0] = value.length + bits[0];
  87. }
  88. if(bits.length < 2 || bits[1] === null){
  89. bits[1] = value.length;
  90. }
  91. if(bits[1] < 0){
  92. bits[1] = value.length + bits[1];
  93. }
  94. return value.slice(bits[0], bits[1]);
  95. },
  96. _unordered_list: function(value, tabs){
  97. var ddl = dojox.dtl.filter.lists;
  98. var i, indent = "";
  99. for(i = 0; i < tabs; i++){
  100. indent += "\t";
  101. }
  102. if(value[1] && value[1].length){
  103. var recurse = [];
  104. for(i = 0; i < value[1].length; i++){
  105. recurse.push(ddl._unordered_list(value[1][i], tabs + 1))
  106. }
  107. return indent + "<li>" + value[0] + "\n" + indent + "<ul>\n" + recurse.join("\n") + "\n" + indent + "</ul>\n" + indent + "</li>";
  108. }else{
  109. return indent + "<li>" + value[0] + "</li>";
  110. }
  111. },
  112. unordered_list: function(value){
  113. // summary:
  114. // Recursively takes a self-nested list and returns an HTML unordered list --
  115. // WITHOUT opening and closing <ul> tags.
  116. // description:
  117. // The list is assumed to be in the proper format. For example, if ``var`` contains
  118. // ``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
  119. // then ``{{ var|unordered_list }}`` would return::
  120. //
  121. // | <li>States
  122. // | <ul>
  123. // | <li>Kansas
  124. // | <ul>
  125. // | <li>Lawrence</li>
  126. // | <li>Topeka</li>
  127. // | </ul>
  128. // | </li>
  129. // | <li>Illinois</li>
  130. // | </ul>
  131. // | </li>
  132. return dojox.dtl.filter.lists._unordered_list(value, 1);
  133. }
  134. });
  135. }