code_junkie

Have a favorite custom Grails tag?

Nov 11th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. // Formats a number to 3 significant digits, appending appropriate scale marker
  2. // (k, m, b, t, etc.). Defining var allows you to use a string representation
  3. // of the formatted number anywhere you need it within the tag body, and
  4. // provides the scale as well (in case highlighting or other special formatting
  5. // based upon scale is desired).
  6. def formatNumberScaled = {attrs, body -> // number, prefix, suffix, invalid, var
  7. Double number
  8. String numberString
  9. String scale
  10.  
  11. try {
  12. number = attrs.'number'.toDouble()
  13. } catch (Exception e) {
  14. number = Double.NaN
  15. }
  16.  
  17. if (number.isNaN() || number.isInfinite()) {
  18. numberString = scale = attrs.'invalid' ?: "N/A"
  19. } else {
  20. Boolean negative = number < 0d
  21. number = negative ? -number : number
  22.  
  23. if (number < 1000d) {
  24. scale = ''
  25. } else if (number < 1000000d) {
  26. scale = 'k'
  27. number /= 1000d
  28. } else if (number < 1000000000d) {
  29. scale = 'm'
  30. number /= 1000000d
  31. } else if (number < 1000000000000d) {
  32. scale = 'b'
  33. number /= 1000000000d
  34. } else if (number < 1000000000000000d) {
  35. scale = 't'
  36. number /= 1000000000000d
  37. }
  38.  
  39. String format
  40. if (number < 10d) {
  41. format = '#.00'
  42. } else if (number < 100d) {
  43. format = '##.0'
  44. } else {
  45. format = '###'
  46. }
  47. format = "'${attrs.'prefix' ?: ''}'${format}'${scale} ${attrs.'suffix' ?: ''}'"
  48.  
  49. numberString = g.formatNumber('number': negative ? -number : number, 'format': format)
  50. }
  51.  
  52. // Now, either print the number or output the tag body with
  53. // the appropriate variables set
  54. if (attrs.'var') {
  55. out << body((attrs.'var'): numberString, 'scale': scale)
  56. } else {
  57. out << numberString
  58. }
  59. }
  60.  
  61. class CustomRemotePaginateTagLib {
  62.  
  63. static namespace = 'myTagLib'
  64.  
  65. /** * Creates next/previous links to support pagination for the current controller * * <g:paginate total="$ { Account.count() } " /> */
  66. def remotePaginate = {attrs ->
  67. def writer = out
  68. if (attrs.total == null) throwTagError("Tag [remotePaginate] is missing required attribute [total]")
  69.  
  70. if (attrs.update == null) throwTagError("Tag [remotePaginate] is missing required attribute [update]")
  71.  
  72. def locale = RequestContextUtils.getLocale(request)
  73.  
  74. def total = attrs.total.toInteger()
  75.  
  76. def update = attrs.update
  77.  
  78. def action = (attrs.action ? attrs.action : (params.action ? params.action : "list"))
  79. def controller = (attrs.controller ? attrs.controller : params.controller)
  80. def offset = params.offset?.toInteger()
  81. def max = params.max?.toInteger()
  82. def maxsteps = (attrs.maxsteps ? attrs.maxsteps.toInteger() : 10)
  83.  
  84. if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
  85. if (!max) max = (attrs.max ? attrs.max.toInteger() : 10)
  86.  
  87. def linkParams = [offset: offset - max, max: max]
  88. if (params.sort) linkParams.sort = params.sort
  89. if (params.order) linkParams.order = params.order
  90. if (attrs.params) linkParams.putAll(attrs.params)
  91. linkParams['action'] = action
  92. linkParams['controller'] = controller
  93.  
  94. def linkTagAttrs = [url: "#"]
  95. if (attrs.controller) { linkTagAttrs.controller = attrs.controller }
  96. if (attrs.id != null) { linkTagAttrs.id = attrs.id }
  97.  
  98. // determine paging variables
  99. def steps = maxsteps > 0
  100. int currentstep = (offset / max) + 1
  101. int firststep = 1
  102. int laststep = Math.round(Math.ceil(total / max))
  103.  
  104. // display previous link when not on firststep
  105. if (currentstep > firststep) {
  106. linkTagAttrs.class = 'prevLink'
  107. def prevOffset = linkParams.offset
  108.  
  109. def params = attrs.params ?: []
  110. params.'offset' = prevOffset
  111.  
  112. linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
  113. writer << link(linkTagAttrs.clone()) {
  114. (attrs.prev ? attrs.prev : g.message(code: 'default.paginate.prev', default: 'Previous'))
  115. }
  116. }
  117.  
  118. // display steps when steps are enabled and laststep is not firststep
  119. if (steps && laststep > firststep) {
  120. linkTagAttrs.class = 'step'
  121.  
  122. // determine begin and endstep paging variables
  123. int beginstep = currentstep - Math.round(maxsteps / 2) + (maxsteps % 2)
  124. int endstep = currentstep + Math.round(maxsteps / 2) - 1
  125.  
  126. if (beginstep < firststep) {
  127. beginstep = firststep
  128. endstep = maxsteps
  129. }
  130. if (endstep > laststep) {
  131. beginstep = laststep - maxsteps + 1
  132. if (beginstep < firststep) {
  133. beginstep = firststep
  134. }
  135. endstep = laststep
  136. }
  137.  
  138. // display firststep link when beginstep is not firststep
  139. if (beginstep > firststep) {
  140. linkParams.offset = 0
  141.  
  142. def params = attrs.params ?: []
  143. params['offset'] = linkParams.offset
  144.  
  145. linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
  146. writer << link(linkTagAttrs.clone()) { firststep.toString() }
  147. writer << '<span class="step">..</span>'
  148. }
  149.  
  150. // display paginate steps
  151. (beginstep..endstep).each {i ->
  152. if (currentstep == i) {
  153. writer << "<span class="currentStep">${i}</span>"
  154. } else {
  155. linkParams.offset = (i - 1) * max
  156.  
  157. def params = attrs.params ?: []
  158. params['offset'] = linkParams.offset
  159.  
  160. linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
  161. writer << link(linkTagAttrs.clone()) { i.toString() }
  162. }
  163. }
  164.  
  165. // display laststep link when endstep is not laststep
  166. if (endstep < laststep) {
  167. writer << '<span class="step">..</span>'
  168. linkParams.offset = (laststep - 1) * max
  169.  
  170. def params = attrs.params ?: []
  171. params['offset'] = linkParams.offset
  172.  
  173. linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
  174. writer << link(linkTagAttrs.clone()) { laststep.toString() }
  175. }
  176. }
  177.  
  178. // display next link when not on laststep
  179. if (currentstep < laststep) {
  180. linkTagAttrs.class = 'nextLink'
  181. linkParams.offset = offset + max
  182.  
  183. def params = attrs.params ?: []
  184. params['offset'] = linkParams.offset
  185.  
  186. linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
  187. writer << link(linkTagAttrs.clone()) {
  188. (attrs.next ? attrs.next : g.message(code: 'default.paginate.next', default: 'Next'))
  189. }
  190. }
  191.  
  192. }
Add Comment
Please, Sign In to add comment