SearchInput.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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', 'bi/admin/common/ui/WidgetView', 'bi/commons/utils/BidiUtil', 'bi/admin/nls/StringResource'], function ($, WidgetView, BidiUtil, StringResource) {
  9. 'use strict'; //NOSONAR: meant to be strict
  10. var SearchInput = WidgetView.extend({
  11. widgetName: 'searchinput',
  12. options: {
  13. value: '',
  14. hint: 'Search'
  15. },
  16. init: function init(options) {
  17. SearchInput.inherited('init', this, arguments);
  18. if (this.element.is("input[type='search']")) {
  19. this._create();
  20. this.options.value = this.element.val() || "";
  21. this.options.hint = this.element.attr('placeholder') || "";
  22. }
  23. },
  24. clear: function clear() {
  25. this.element.val("");
  26. var clearButton = this.element.parent().find(".bi-admin-search-cancel");
  27. clearButton.css("display", "none");
  28. },
  29. _create: function _create() {
  30. this.element.wrap('<div class="bi-admin-input-wrapper search-security"></div>');
  31. this.element.addClass('bi-admin-input bi-admin-input-search');
  32. var htmlIcon = '<button tabindex="-1" disable class="bi-admin-search-button"><svg class="svgIcon bi-admin-searchClear"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#common-search"></use></svg></button><button style="display: none" class="bi-admin-search-cancel"><svg class="svgIcon "><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#common-close-cancel-error"></use></svg></button>';
  33. this.element.after(htmlIcon);
  34. var clearButton = this.element.parent().find(".bi-admin-search-cancel");
  35. var searchButton = this.element.parent().find(".bi-admin-search-button");
  36. searchButton.attr("title", StringResource.get('search'));
  37. clearButton.attr("title", StringResource.get('clear'));
  38. clearButton.attr("aria-label", StringResource.get('clear'));
  39. clearButton.on('click', function () {
  40. this.clear();
  41. this.trigger('changed', {
  42. text: ""
  43. });
  44. searchButton.css("display", "inline-flex");
  45. }.bind(this));
  46. var searchHandler = function searchHandler() {
  47. searchButton.css("display", "inline-flex");
  48. if (this.element.val() !== "") {
  49. clearButton.css("display", "inline-flex");
  50. searchButton.css("display", "none");
  51. } else {
  52. searchButton.css("display", "inline-flex");
  53. clearButton.css("display", "none");
  54. }
  55. this.trigger('changed', {
  56. text: this.element.val()
  57. });
  58. };
  59. searchButton.on('click', searchHandler.bind(this));
  60. this.element.on("keydown", function (event) {
  61. if (event.which === 13) {
  62. searchHandler.apply(this);
  63. }
  64. }.bind(this));
  65. BidiUtil.initElementForBidi(this.element[0]);
  66. },
  67. _setOption: function _setOption(key, value) {
  68. var self = this;
  69. switch (key) {
  70. case "value":
  71. this.element.val(value);
  72. break;
  73. case "hint":
  74. this.element.attr('placeholder', value);
  75. this.element.attr('aria-label', value);
  76. break;
  77. default:
  78. break;
  79. }
  80. WidgetView.prototype._setOption.apply(self, arguments);
  81. }
  82. });
  83. return SearchInput;
  84. });