Show:

File: ia\ui\TableTools.js

  1. /**
  2. * <code>ia.TableTools</code> is a set of table tools that can be attached to a table.
  3. *
  4. * @author J Clare
  5. * @class ia.TableTools
  6. * @constructor
  7. * @param {ia.DataGroup} dataGroup The associated data group.
  8. * @param {ia.InteractionGroup} interactionGroup The associated interaction group.
  9. */
  10. ia.TableTools = function(dataGroup, interactionGroup)
  11. {
  12. var me = this;
  13.  
  14. me._clearBtn = undefined;
  15. me._filterBtn = undefined;
  16. me._interactionGroup = interactionGroup;
  17. me._dataGroup = dataGroup;
  18.  
  19. // Calculated border radius.
  20. me._borderRadius = 0;
  21. // Calculated border-radius for elements inside panels.
  22. me._borderRadius = parseInt($j(".ia-panel").css("border-top-left-radius")) - 1;
  23. if (me._borderRadius < 0) me._borderRadius = 0;
  24.  
  25. // Create the container element.
  26. me.container = $j('<div class="ia-table-toolbar"></div>');
  27. me.container.css("border-bottom-left-radius", me._borderRadius+"px");
  28. me.container.css("border-bottom-right-radius", me._borderRadius+"px");
  29.  
  30. // Listen for the selection changing.
  31. interactionGroup.addEventListener(ia.InteractionEvent.SELECTION_CHANGED, this._updateToolbar.bind(this));
  32.  
  33. // Listen for the filter changing.
  34. dataGroup.addEventListener(ia.FilterEvent.FILTER_CHANGED, this._updateToolbar.bind(this));
  35. };
  36.  
  37. /**
  38. * Updates the tool bar.
  39. *
  40. * @property updateToolbar
  41. * @private
  42. */
  43. ia.TableTools.prototype._updateToolbar = function()
  44. {
  45. if (this._interactionGroup.getSelection().length > 0)
  46. {
  47. if (this._clearBtn)
  48. this._clearBtn.removeClass('ia-cross-btn-disabled ia-toolbar-text-btn-disabled').addClass('ia-list-item ia-cross-btn');
  49. if (this._filterBtn)
  50. this._filterBtn.removeClass('ia-cross-btn ia-toolbar-text-btn-disabled').addClass('ia-list-item ia-cross-btn-disabled');
  51. }
  52. else
  53. {
  54. if (this._clearBtn)
  55. this._clearBtn.removeClass('ia-list-item ia-cross-btn').addClass('ia-cross-btn-disabled ia-toolbar-text-btn-disabled');
  56. if (this._filterBtn)
  57. {
  58. if (this._dataGroup.getFilteredFeatures().length > 0)
  59. this._filterBtn.removeClass('ia-cross-btn-disabled ia-toolbar-text-btn-disabled').addClass('ia-list-item ia-cross-btn');
  60. else
  61. this._filterBtn.removeClass('ia-list-item ia-cross-btn').addClass('ia-cross-btn-disabled ia-toolbar-text-btn-disabled');
  62. }
  63. }
  64. };
  65. /**
  66. * The container that holds the object.
  67. *
  68. * @property container
  69. * @type JQUERY Element
  70. */
  71. ia.TableTools.prototype.container;
  72.  
  73. /**
  74. * Allows a custom function to be set for when the filter button is pressed.
  75. *
  76. * @property filterFunction
  77. * @type Function
  78. */
  79. ia.TableTools.prototype.filterFunction = function() {};
  80.  
  81. /**
  82. * Allows a custom function to be set for when the clear button is pressed.
  83. *
  84. * @property filterFunction
  85. * @type Function
  86. */
  87. ia.TableTools.prototype.clearFunction = function() {};
  88.  
  89. /**
  90. * The text for the clear button.
  91. *
  92. * @property clearButtonText
  93. * @type String
  94. */
  95. ia.TableTools.prototype.clearButtonText;
  96.  
  97. /**
  98. * The text for the filter button.
  99. *
  100. * @property filterButtonText
  101. * @type String
  102. */
  103. ia.TableTools.prototype.filterButtonText;
  104.  
  105. /**
  106. * Render the toolbar.
  107. *
  108. * @method render
  109. */
  110. ia.TableTools.prototype.render = function()
  111. {
  112. this.container.empty();
  113.  
  114. var me = this;
  115. // Add clear selection button
  116. if (this.clearButtonText)
  117. {
  118. this._clearBtn = $j("<div class='ia-toolbar-text-btn ia-list-item ia-toolbar-text-btn-disabled ia-cross-btn-disabled'>").html(this.clearButtonText);
  119. this.container.append(this._clearBtn);
  120. this._clearBtn.bind(ia.CLICK_TYPE, function(e)
  121. {
  122. e.stopPropagation();
  123. e.preventDefault();
  124. me.clearFunction();
  125. });
  126. this._clearBtn.css("border-radius", this._borderRadius+"px");
  127. }
  128.  
  129. // Add filter button
  130. if (this.filterButtonText)
  131. {
  132. this._filterBtn = $j("<div class='ia-toolbar-text-btn ia-list-item ia-toolbar-text-btn-disabled ia-cross-btn-disabled'>").html(this.filterButtonText);
  133. this.container.append(this._filterBtn);
  134. this._filterBtn.bind(ia.CLICK_TYPE, function(e)
  135. {
  136. e.stopPropagation();
  137. e.preventDefault();
  138. me.filterFunction();
  139. });
  140. this._filterBtn.css("border-radius", this._borderRadius+"px");
  141. }
  142.  
  143. this._updateToolbar();
  144. };