PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/qooxdoo/framework/source/class/qx/ui/progressive/renderer/table/cell/Conditional.js

https://github.com/Wkasel/qooxdoo
JavaScript | 463 lines | 243 code | 51 blank | 169 comment | 43 complexity | 27773d4021aee6e2506a97bd1d989943 MD5 | raw file
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2007 by Tartan Solutions, Inc, http://www.tartansolutions.com
  6. 2008 Derrell Lipman
  7. License:
  8. LGPL: http://www.gnu.org/licenses/lgpl.html
  9. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  10. See the LICENSE file in the project's top-level directory for details.
  11. Authors:
  12. * Dan Hummon
  13. * Derrell Lipman (derrell)
  14. ************************************************************************ */
  15. /**
  16. * Table Cell Renderer for Progressive.
  17. */
  18. qx.Class.define("qx.ui.progressive.renderer.table.cell.Conditional",
  19. {
  20. extend : qx.ui.progressive.renderer.table.cell.Abstract,
  21. /**
  22. * @param align {String}
  23. * The default alignment to format the cell with if the condition matches.
  24. *
  25. * @param color {String}
  26. * The default color to format the cell with if the condition matches.
  27. *
  28. * @param style {String}
  29. * The default style to format the cell with if the condition matches.
  30. *
  31. * @param weight {String}
  32. * The default weight to format the cell with if the condition matches.
  33. */
  34. construct : function(align, color, style, weight)
  35. {
  36. this.base(arguments);
  37. this.__numericAllowed =
  38. [
  39. "==",
  40. "!=",
  41. ">",
  42. "<",
  43. ">=",
  44. "<="
  45. ];
  46. this.__betweenAllowed =
  47. [
  48. "between",
  49. "!between"
  50. ];
  51. this.__conditions = [ ];
  52. this.__defaultTextAlign = align || "";
  53. this.__defaultColor = color || "";
  54. this.__defaultFontStyle = style || "";
  55. this.__defaultFontWeight = weight || "";
  56. },
  57. members :
  58. {
  59. __numericAllowed : null,
  60. __betweenAllowed : null,
  61. __conditions : null,
  62. __defaultTextAlign : null,
  63. __defaultColor : null,
  64. __defaultFontStyle : null,
  65. __defaultFontWeight : null,
  66. /**
  67. * Applies the cell styles to the style map.
  68. *
  69. * @param condition {Array}
  70. * The matched condition
  71. *
  72. * @param style {Map}
  73. * map of already applied styles.
  74. */
  75. __applyFormatting : function(condition, style)
  76. {
  77. if (condition.align)
  78. {
  79. style["text-align"] = condition.align;
  80. }
  81. if (condition.color)
  82. {
  83. style["color"] = condition.color;
  84. }
  85. if (condition.style)
  86. {
  87. style["font-style"] = condition.style;
  88. }
  89. if (condition.weight)
  90. {
  91. style["font-weight"] = condition.weight;
  92. }
  93. },
  94. /**
  95. * The addNumericCondition method is used to add a basic numeric condition
  96. * to the cell renderer.
  97. *
  98. * Note: Passing null is different from passing an empty string in the
  99. * align, color, style and weight arguments. Null will allow pre-existing
  100. * formatting to pass through, where an empty string will clear it back to
  101. * the default formatting set in the constructor.
  102. *
  103. *
  104. *
  105. * @param condition {String}
  106. * The type of condition. Accepted strings are "==", "!=", ">", "<",
  107. * ">=", and "<=".
  108. *
  109. * @param value1 {Integer}
  110. * The value to compare against.
  111. *
  112. * @param align {String}
  113. * The alignment to format the cell with if the condition matches.
  114. *
  115. * @param color {String}
  116. * The color to format the cell with if the condition matches.
  117. *
  118. * @param style {String}
  119. * The style to format the cell with if the condition matches.
  120. *
  121. * @param weight {String}
  122. * The weight to format the cell with if the condition matches.
  123. *
  124. * @param target {String}
  125. * The text value of the column to compare against. If this is null,
  126. * comparisons will be against the contents of this cell.
  127. *
  128. * @throws {Error} If the condition can not be recognized or the value
  129. * is null.
  130. */
  131. addNumericCondition : function(condition, value1, align,
  132. color, style, weight, target)
  133. {
  134. if (! qx.lang.Array.contains(this.__numericAllowed, condition) ||
  135. value1 == null)
  136. {
  137. throw new Error("Condition not recognized or value is null!");
  138. }
  139. this.__conditions.push(
  140. {
  141. condition : condition,
  142. align : align,
  143. color : color,
  144. style : style,
  145. weight : weight,
  146. value1 : value1,
  147. target :target
  148. });
  149. },
  150. /**
  151. * The addBetweenCondition method is used to add a between condition to
  152. * the cell renderer.
  153. *
  154. * Note: Passing null is different from passing an empty string in the
  155. * align, color, style and weight arguments. Null will allow pre-existing
  156. * formatting to pass through, where an empty string will clear it back to
  157. * the default formatting set in the constructor.
  158. *
  159. *
  160. *
  161. * @param condition {String}
  162. * The type of condition. Accepted strings are "between" and "!between".
  163. *
  164. * @param value1 {Integer}
  165. * The first value to compare against.
  166. *
  167. * @param value2 {Integer}
  168. * The second value to compare against.
  169. *
  170. * @param align {String}
  171. * The alignment to format the cell with if the condition matches.
  172. *
  173. * @param color {String}
  174. * The color to format the cell with if the condition matches.
  175. *
  176. * @param style {String}
  177. * The style to format the cell with if the condition matches.
  178. *
  179. * @param weight {String}
  180. * The weight to format the cell with if the condition matches.
  181. *
  182. * @param target {String}
  183. * The text value of the column to compare against. If this is null,
  184. * comparisons will be against the contents of this cell.
  185. *
  186. * @return {void}
  187. *
  188. * @throws {Error} If the condition can not recognized or one of the
  189. * values is null.
  190. */
  191. addBetweenCondition : function(condition, value1, value2, align,
  192. color, style, weight, target)
  193. {
  194. if (! qx.lang.Array.contains(this.__betweenAllowed, condition) ||
  195. value1 == null ||
  196. value2 == null)
  197. {
  198. throw new Error("Condition not recognized or value1/value2 is null!");
  199. }
  200. this.__conditions.push(
  201. {
  202. condition : condition,
  203. align : align,
  204. color : color,
  205. style : style,
  206. weight : weight,
  207. value1 : value1,
  208. value2 : value2,
  209. target : target
  210. });
  211. },
  212. /**
  213. * The addRegex method is used to add a regular expression condition to
  214. * the cell renderer.
  215. *
  216. * Note: Passing null is different from passing an empty string in the
  217. * align, color, style and weight arguments. Null will allow pre-existing
  218. * formatting to pass through, where an empty string will clear it back to
  219. * the default formatting set in the constructor.
  220. *
  221. *
  222. *
  223. * @param regex {String}
  224. * The regular expression to match against.
  225. *
  226. * @param align {String}
  227. * The alignment to format the cell with if the condition matches.
  228. *
  229. * @param color {String}
  230. * The color to format the cell with if the condition matches.
  231. *
  232. * @param style {String}
  233. * The style to format the cell with if the condition matches.
  234. *
  235. * @param weight {String}
  236. * The weight to format the cell with if the condition matches.
  237. *
  238. * @param target {String}
  239. * The text value of the column to compare against. If this is null,
  240. * comparisons will be against the contents of this cell.
  241. *
  242. * @throws {Error} If the regex is null.
  243. */
  244. addRegex : function(regex, align, color, style, weight, target)
  245. {
  246. if (! regex)
  247. {
  248. throw new Error("regex cannot be null!");
  249. }
  250. this.__conditions.push(
  251. {
  252. condition : "regex",
  253. align : align,
  254. color : color,
  255. style : style,
  256. weight : weight,
  257. regex : regex,
  258. target : target
  259. });
  260. },
  261. /**
  262. * Overridden; called whenever the cell updates. The cell will iterate
  263. * through each available condition and apply formatting for those that
  264. * match. Multiple conditions can match, but later conditions will
  265. * override earlier ones. Conditions with null values will stack with
  266. * other conditions that apply to that value.
  267. *
  268. *
  269. * @param cellInfo {Map}
  270. * The information about the cell. See {@link qx.ui.table.cellrenderer.Abstract#createDataCellHtml}.
  271. *
  272. * @return {Map}
  273. */
  274. _getCellStyle : function(cellInfo)
  275. {
  276. if (this.__conditions.length == 0)
  277. {
  278. return cellInfo.style || "";
  279. }
  280. var i;
  281. var bTestPassed;
  282. var compareValue;
  283. var style =
  284. {
  285. "text-align" : this.__defaultTextAlign,
  286. "color" : this.__defaultColor,
  287. "font-style" : this.__defaultFontStyle,
  288. "font-weight" : this.__defaultFontWeight
  289. };
  290. for (i = 0; i < this.__conditions.length; i++)
  291. {
  292. var test = this.__conditions[i];
  293. bTestPassed = false;
  294. if (qx.lang.Array.contains(this.__numericAllowed, test.condition))
  295. {
  296. if (test.target == null)
  297. {
  298. compareValue = cellInfo.cellData;
  299. }
  300. else
  301. {
  302. compareValue = cellInfo.element.data[test.target];
  303. }
  304. switch(test.condition)
  305. {
  306. case "==":
  307. if (compareValue == test.value1)
  308. {
  309. bTestPassed = true;
  310. }
  311. break;
  312. case "!=":
  313. if (compareValue != test.value1)
  314. {
  315. bTestPassed = true;
  316. }
  317. break;
  318. case ">":
  319. if (compareValue > test.value1)
  320. {
  321. bTestPassed = true;
  322. }
  323. break;
  324. case "<":
  325. if (compareValue < test.value1)
  326. {
  327. bTestPassed = true;
  328. }
  329. break;
  330. case ">=":
  331. if (compareValue >= test.value1)
  332. {
  333. bTestPassed = true;
  334. }
  335. break;
  336. case "<=":
  337. if (compareValue <= test.value1)
  338. {
  339. bTestPassed = true;
  340. }
  341. break;
  342. }
  343. }
  344. else if (qx.lang.Array.contains(this.__betweenAllowed,
  345. test.condition))
  346. {
  347. if (test.target == null)
  348. {
  349. compareValue = cellInfo.cellData;
  350. }
  351. else
  352. {
  353. compareValue = cellInfo.element.data[test.target];
  354. }
  355. switch(test.condition)
  356. {
  357. case "between":
  358. if (compareValue >= test.value1 &&
  359. compareValue <= test.value2)
  360. {
  361. bTestPassed = true;
  362. }
  363. break;
  364. case "!between":
  365. if (compareValue < test.value1 &&
  366. compareValue > test.value2)
  367. {
  368. bTestPassed = true;
  369. }
  370. break;
  371. }
  372. }
  373. else if (test.condition == "regex")
  374. {
  375. if (test.target == null)
  376. {
  377. compareValue = cellInfo.cellData;
  378. }
  379. else
  380. {
  381. compareValue = cellInfo.element.data[test.target];
  382. }
  383. var the_pattern = new RegExp(test.value1, 'g');
  384. bTestPassed = the_pattern.test(compareValue);
  385. }
  386. // Apply formatting, if any.
  387. if (bTestPassed)
  388. {
  389. this.__applyFormatting(test, style);
  390. }
  391. var styleString = [];
  392. for(var key in style)
  393. {
  394. if (style[key])
  395. {
  396. styleString.push(key, ":", style[key], ";");
  397. }
  398. }
  399. }
  400. return styleString.join("");
  401. }
  402. },
  403. destruct : function() {
  404. this.__numericAllowed = this.__betweenAllowed = this.__conditions = null;
  405. }
  406. });