PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/client/galaxy/scripts/mvc/ui/icon-button.js

https://bitbucket.org/galaxy/galaxy-central/
JavaScript | 183 lines | 120 code | 24 blank | 39 comment | 9 complexity | 19f9539311300b1bc9bb60e0373c3c87 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. //jquery
  3. //backbone
  4. ], function(){
  5. //=============================================================================
  6. /**
  7. * backbone model for icon buttons
  8. */
  9. var IconButton = Backbone.Model.extend({
  10. defaults: {
  11. title : "",
  12. icon_class : "",
  13. on_click : null,
  14. menu_options : null,
  15. is_menu_button : true,
  16. id : null,
  17. href : null,
  18. target : null,
  19. enabled : true,
  20. visible : true,
  21. tooltip_config : {}
  22. }
  23. });
  24. /**
  25. * backbone view for icon buttons
  26. */
  27. var IconButtonView = Backbone.View.extend({
  28. initialize : function(){
  29. // better rendering this way
  30. this.model.attributes.tooltip_config = { placement : 'bottom' };
  31. this.model.bind( 'change', this.render, this );
  32. },
  33. render : function( ){
  34. // hide tooltip
  35. this.$el.tooltip( 'hide' );
  36. var new_elem = this.template( this.model.toJSON() );
  37. // configure tooltip
  38. new_elem.tooltip( this.model.get( 'tooltip_config' ));
  39. this.$el.replaceWith( new_elem );
  40. this.setElement( new_elem );
  41. return this;
  42. },
  43. events : {
  44. 'click' : 'click'
  45. },
  46. click : function( event ){
  47. // if on_click pass to that function
  48. if( _.isFunction( this.model.get( 'on_click' ) ) ){
  49. this.model.get( 'on_click' )( event );
  50. return false;
  51. }
  52. // otherwise, bubble up ( to href or whatever )
  53. return true;
  54. },
  55. // generate html element
  56. template: function( options ){
  57. var buffer = 'title="' + options.title + '" class="icon-button';
  58. if( options.is_menu_button ){
  59. buffer += ' menu-button';
  60. }
  61. buffer += ' ' + options.icon_class;
  62. if( !options.enabled ){
  63. buffer += '_disabled';
  64. }
  65. // close class tag
  66. buffer += '"';
  67. if( options.id ){
  68. buffer += ' id="' + options.id + '"';
  69. }
  70. buffer += ' href="' + options.href + '"';
  71. // add target for href
  72. if( options.target ){
  73. buffer += ' target="' + options.target + '"';
  74. }
  75. // set visibility
  76. if( !options.visible ){
  77. buffer += ' style="display: none;"';
  78. }
  79. // enabled/disabled
  80. if ( options.enabled ){
  81. buffer = '<a ' + buffer + '/>';
  82. } else {
  83. buffer = '<span ' + buffer + '/>';
  84. }
  85. // return element
  86. return $( buffer );
  87. }
  88. } );
  89. // define collection
  90. var IconButtonCollection = Backbone.Collection.extend({
  91. model: IconButton
  92. });
  93. /**
  94. * menu with multiple icon buttons
  95. * views are not needed nor used for individual buttons
  96. */
  97. var IconButtonMenuView = Backbone.View.extend({
  98. tagName: 'div',
  99. initialize: function(){
  100. this.render();
  101. },
  102. render: function(){
  103. // initialize icon buttons
  104. var self = this;
  105. this.collection.each(function(button){
  106. // create and add icon button to menu
  107. var elt = $('<a/>')
  108. .attr('href', 'javascript:void(0)')
  109. .attr('title', button.attributes.title)
  110. .addClass('icon-button menu-button')
  111. .addClass(button.attributes.icon_class)
  112. .appendTo(self.$el)
  113. .click(button.attributes.on_click);
  114. // configure tooltip
  115. if (button.attributes.tooltip_config){
  116. elt.tooltip(button.attributes.tooltip_config);
  117. }
  118. // add popup menu to icon
  119. var menu_options = button.get('options');
  120. if (menu_options){
  121. make_popupmenu(elt, menu_options);
  122. }
  123. });
  124. // return
  125. return this;
  126. }
  127. });
  128. /**
  129. * Returns an IconButtonMenuView for the provided configuration.
  130. * Configuration is a list of dictionaries where each dictionary
  131. * defines an icon button. Each dictionary must have the following
  132. * elements: icon_class, title, and on_click.
  133. */
  134. var create_icon_buttons_menu = function(config, global_config)
  135. {
  136. // initialize global configuration
  137. if (!global_config) global_config = {};
  138. // create and initialize menu
  139. var buttons = new IconButtonCollection(
  140. _.map(config, function(button_config){
  141. return new IconButton(_.extend(button_config, global_config));
  142. })
  143. );
  144. // return menu
  145. return new IconButtonMenuView( {collection: buttons} );
  146. };
  147. //=============================================================================
  148. return {
  149. IconButton : IconButton,
  150. IconButtonView : IconButtonView,
  151. IconButtonCollection : IconButtonCollection,
  152. IconButtonMenuView : IconButtonMenuView,
  153. create_icon_buttons_menu: create_icon_buttons_menu
  154. };
  155. })