handlebars_helpers.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * Helper function for bound property helper registration
  21. * @memberof App
  22. * @method registerBoundHelper
  23. * @param name {String} name of helper
  24. * @param view {Em.View} view
  25. */
  26. App.registerBoundHelper = function(name, view) {
  27. Em.Handlebars.registerHelper(name, function(property, options) {
  28. options.hash.contentBinding = property;
  29. return Em.Handlebars.helpers.view.call(this, view, options);
  30. });
  31. };
  32. /*
  33. * Return singular or plural word based on Em.I18n, view|controller context property key.
  34. *
  35. * Example: {{pluralize hostsCount singular="t:host" plural="t:hosts"}}
  36. * {{pluralize hostsCount singular="@view.hostName"}}
  37. */
  38. App.registerBoundHelper('pluralize', App.PluralizeView);
  39. /**
  40. * Return defined string instead of empty if value is null/undefined
  41. * by default is `n/a`.
  42. *
  43. * @param empty {String} - value instead of empty string (not required)
  44. * can be used with Em.I18n pass value started with't:'
  45. *
  46. * Examples:
  47. *
  48. * default value will be returned
  49. * {{formatNull service.someValue}}
  50. *
  51. * <code>empty<code> will be returned
  52. * {{formatNull service.someValue empty="I'm empty"}}
  53. *
  54. * Em.I18n translation will be returned
  55. * {{formatNull service.someValue empty="t:my.key.to.translate"
  56. */
  57. App.registerBoundHelper('formatNull', App.FormatNullView);
  58. /**
  59. * Return formatted string with inserted <code>wbr</code>-tag after each dot or each '_'
  60. *
  61. * @param {String} content
  62. *
  63. * Examples:
  64. *
  65. * returns 'apple'
  66. * {{formatWordBreak 'apple'}}
  67. *
  68. * returns 'apple.<wbr />banana'
  69. * {{formatWordBreak 'apple.banana'}}
  70. *
  71. * returns 'apple.<wbr />banana.<wbr />uranium'
  72. * {{formatWordBreak 'apple.banana.uranium'}}
  73. *
  74. * returns 'apple_<wbr />banana_<wbr />uranium'
  75. * {{formatWordBreak 'apple_banana_uranium'}}
  76. *
  77. * returns 'Very<wbr />Long<wbr />String<wbr />With<wbr />Uppercase'
  78. * {{formatWordBreak 'VeryLongStringWithUppercase'}}
  79. */
  80. App.registerBoundHelper('formatWordBreak', App.FormatWordBreakView);
  81. /**
  82. * Return <i></i> with class that correspond to status
  83. *
  84. * @param {string} content - status
  85. *
  86. * Examples:
  87. *
  88. * {{statusIcon view.status}}
  89. * returns 'icon-cog'
  90. *
  91. */
  92. App.registerBoundHelper('statusIcon', App.StatusIconView);
  93. /**
  94. * Return `span` with formatted service name
  95. * @param {string} content - serviceName
  96. */
  97. App.registerBoundHelper('formatRole', App.FormatRoleView);