You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

formatters.js 2.6 KiB

12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // for license information please see license.txt
  2. wn.provide("wn.form.formatters");
  3. wn.form.formatters = {
  4. Data: function(value) {
  5. return value==null ? "" : value
  6. },
  7. Float: function(value, docfield) {
  8. var decimals = wn.boot.sysdefaults.float_precision ?
  9. parseInt(wn.boot.sysdefaults.float_precision) : null;
  10. return "<div style='text-align: right'>" +
  11. format_number(flt(value, decimals), null, decimals) + "</div>";
  12. },
  13. Int: function(value) {
  14. return cint(value);
  15. },
  16. Currency: function(value, docfield, doc) {
  17. var currency = wn.meta.get_field_currency(docfield, doc);
  18. return "<div style='text-align: right'>" + format_currency(value, currency) + "</div>";
  19. },
  20. Check: function(value) {
  21. return value ? "<i class='icon-check'></i>" : "<i class='icon-check-empty'></i>";
  22. },
  23. Link: function(value, docfield) {
  24. if(!value) return "";
  25. if(docfield && docfield.options) {
  26. return repl('<a href="#Form/%(doctype)s/%(name)s">\
  27. <i class="icon icon-share" title="Open %(name)s" \
  28. style="margin-top:-1px"></i></a> %(name)s', {
  29. doctype: docfield.options,
  30. name: value
  31. });
  32. } else {
  33. return value;
  34. }
  35. },
  36. Date: function(value) {
  37. return dateutil.str_to_user(value);
  38. },
  39. Text: function(value) {
  40. if(value) {
  41. var tags = ["<p[^>]>", "<div[^>]>", "<br[^>]>"];
  42. var match = false;
  43. for(var i=0; i<tags.length; i++) {
  44. if(value.match(tags[i])) {
  45. match = true;
  46. }
  47. }
  48. if(!match) {
  49. return replace_newlines(value);
  50. }
  51. }
  52. return wn.form.formatters.Data(value);
  53. },
  54. Tag: function(value) {
  55. var html = "";
  56. $.each((value || "").split(","), function(i, v) {
  57. if(v) html+= '<span class="label label-info" \
  58. style="margin-right: 7px; cursor: pointer;"\
  59. data-field="_user_tags" data-label="'+v+'">'+v +'</span>';
  60. });
  61. return html;
  62. },
  63. SmallText: function(value) {
  64. return wn.form.formatters.Text(value);
  65. },
  66. WorkflowState: function(value) {
  67. workflow_state = wn.model.get("Workflow State", value)[0];
  68. if(workflow_state) {
  69. return repl("<span class='label label-%(style)s' \
  70. data-workflow-state='%(value)s'\
  71. style='padding-bottom: 4px; cursor: pointer;'>\
  72. <i class='icon-small icon-white icon-%(icon)s'></i> %(value)s</span>", {
  73. value: value,
  74. style: workflow_state.style.toLowerCase(),
  75. icon: workflow_state.icon
  76. });
  77. } else {
  78. return "<span class='label'>" + value + "</span>";
  79. }
  80. }
  81. }
  82. wn.form.get_formatter = function(fieldtype) {
  83. return wn.form.formatters[fieldtype.replace(/ /g, "")] || wn.form.formatters.Data;
  84. }
  85. wn.format = function(value, df) {
  86. if(!df) df = {"fieldtype":"Data"}
  87. return wn.form.get_formatter(df.fieldtype)(value, df);
  88. }