MagicWand.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. /**
  3.  * Licensed Materials - Property of IBM
  4.  * IBM Cognos Products: Cognos Analytics
  5.  * Copyright IBM Corp. 2015, 2016
  6.  * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7.  */
  8. define(['jquery', 'underscore', 'q', 'bi/commons/ui/core/Class', 'bi/admin/nls/StringResource'], function ($, _, Q, Class, StringResource) {
  9. 'use strict'; //NOSONAR: meant to be strict
  10. var _singletonInstance = null;
  11. var MagicWand = Class.extend({
  12. _types: {
  13. 'searchInput': {
  14. 'query': 'input[type="search"]',
  15. 'ariaLabel': StringResource.get('editFilter'),
  16. 'module': 'bi/admin/common/ui/input/SearchInput'
  17. },
  18. 'radioBox': {
  19. 'query': 'input[type="radio"]',
  20. 'module': 'bi/admin/common/ui/input/RadioBox'
  21. },
  22. 'checkBox': {
  23. 'query': 'input[type="checkbox"]',
  24. 'module': 'bi/admin/common/ui/input/CheckBox'
  25. },
  26. 'button': {
  27. 'query': 'button',
  28. 'module': 'bi/admin/common/ui/input/Button'
  29. },
  30. 'testFlow': {
  31. 'query': '[data-bi-testflow]',
  32. 'module': 'bi/admin/common/ui/testflow/TestFlow'
  33. }
  34. },
  35. init: function init(options) {
  36. $.extend(this, options);
  37. },
  38. searchInput: function searchInput(scope) {
  39. var spec = this._types['searchInput'];
  40. return this._doTransform(spec, scope);
  41. },
  42. radioBox: function radioBox(scope) {
  43. var spec = this._types['radioBox'];
  44. return this._doTransform(spec, scope);
  45. },
  46. checkBox: function checkBox(scope) {
  47. var spec = this._types['checkBox'];
  48. return this._doTransform(spec, scope);
  49. },
  50. button: function button(scope) {
  51. var spec = this._types['button'];
  52. return this._doTransform(spec, scope);
  53. },
  54. testFlow: function testFlow(scope) {
  55. var spec = this._types['testFlow'];
  56. return this._doTransform(spec, scope);
  57. },
  58. all: function all(scope) {
  59. // May optimize the transform process with faster quering
  60. this.searchInput(scope);
  61. this.radioBox(scope);
  62. this.checkBox(scope);
  63. this.button(scope);
  64. },
  65. _doTransform: function _doTransform(spec, scope) {
  66. var deferred = Q.defer();
  67. var elements = $(spec.query, scope);
  68. elements.hide();
  69. require([spec.module], function (WidgetClass) {
  70. var widgets = [];
  71. elements.each(function (index, el) {
  72. var w;
  73. var $el = $(el);
  74. if (spec.ariaLabel) {
  75. $el.attr('aria-label', spec.ariaLabel);
  76. }
  77. var widgetName = $el.data('widgetName');
  78. if (widgetName) {
  79. w = $el.data(widgetName);
  80. if (w && w.refresh) {
  81. w.refresh();
  82. }
  83. } else {
  84. w = new WidgetClass({
  85. el: this,
  86. scope: scope
  87. });
  88. }
  89. widgets.push(w);
  90. });
  91. elements.show();
  92. deferred.resolve(widgets);
  93. });
  94. return deferred.promise;
  95. },
  96. transform: function transform() {// to do...
  97. }
  98. });
  99. var _static = {
  100. getInstance: function getInstance() {
  101. if (!_singletonInstance) {
  102. _singletonInstance = new MagicWand();
  103. }
  104. return _singletonInstance;
  105. }
  106. };
  107. return _static.getInstance();
  108. });