PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/mvc/ui.js

https://bitbucket.org/jmchilton/galaxy-central-reports-config-enhancements
JavaScript | 246 lines | 116 code | 53 blank | 77 comment | 8 complexity | f3d5722a2f954f693ee055b5b42736c3 MD5 | raw file
  1. /**
  2. * -- Functions for creating large UI elements. --
  3. */
  4. // =============================================================================
  5. /**
  6. * -- Utility models and views for Galaxy objects. --
  7. */
  8. /**
  9. * Clickable button represented as an icon.
  10. */
  11. var IconButton = Backbone.Model.extend({
  12. defaults: {
  13. title: "",
  14. icon_class: "",
  15. on_click: null,
  16. tooltip_config: {},
  17. isMenuButton : true,
  18. id : null,
  19. href : null,
  20. target : null,
  21. enabled : true,
  22. visible : true
  23. }
  24. //validate : function( attributes ){
  25. //TODO: validate href or on_click
  26. //TODO: validate icon_class
  27. //}
  28. });
  29. /**
  30. *
  31. */
  32. var IconButtonView = Backbone.View.extend({
  33. initialize : function(){
  34. // better rendering this way (for me anyway)
  35. this.model.attributes.tooltip_config = { placement : 'bottom' };
  36. this.model.bind( 'change', this.render, this );
  37. },
  38. render : function(){
  39. //NOTE: not doing this hide will lead to disappearing buttons when they're both being hovered over & rendered
  40. this.$el.tooltip( 'hide' );
  41. // template in common-templates.html
  42. var newElem = $( Handlebars.partials.iconButton( this.model.toJSON() ) );
  43. newElem.tooltip( this.model.get( 'tooltip_config' ) );
  44. this.$el.replaceWith( newElem );
  45. this.setElement( newElem );
  46. return this;
  47. },
  48. events : {
  49. 'click' : 'click'
  50. },
  51. click : function( event ){
  52. // if on_click pass to that function
  53. if( this.model.attributes.on_click ){
  54. this.model.attributes.on_click( event );
  55. return false;
  56. }
  57. // otherwise, bubble up (to href or whatever)
  58. return true;
  59. }
  60. });
  61. //TODO: bc h.templates is gen. loaded AFTER ui, Handlebars.partials.iconButton === undefined
  62. IconButtonView.templates = {
  63. iconButton : Handlebars.partials.iconButton
  64. };
  65. var IconButtonCollection = Backbone.Collection.extend({
  66. model: IconButton
  67. });
  68. //------------------------------------------------------------------------------
  69. /**
  70. * Menu with multiple icon buttons. Views are not needed nor used for individual buttons.
  71. */
  72. var IconButtonMenuView = Backbone.View.extend({
  73. tagName: 'div',
  74. initialize: function() {
  75. this.render();
  76. },
  77. render: function() {
  78. var self = this;
  79. this.collection.each(function(button) {
  80. // Create and add icon button to menu.
  81. var elt =
  82. $('<a/>').attr('href', 'javascript:void(0)')
  83. .attr('title', button.attributes.title)
  84. .addClass('icon-button menu-button')
  85. .addClass(button.attributes.icon_class)
  86. .appendTo(self.$el)
  87. .click(button.attributes.on_click);
  88. if (button.attributes.tooltip_config) {
  89. elt.tooltip(button.attributes.tooltip_config);
  90. }
  91. });
  92. return this;
  93. }
  94. });
  95. /**
  96. * Returns an IconButtonMenuView for the provided configuration.
  97. * Configuration is a list of dictionaries where each dictionary
  98. * defines an icon button. Each dictionary must have the following
  99. * elements: icon_class, title, and on_click.
  100. */
  101. var create_icon_buttons_menu = function(config, global_config) {
  102. if (!global_config) { global_config = {}; }
  103. // Create and initialize menu.
  104. var buttons = new IconButtonCollection(
  105. _.map(config, function(button_config) {
  106. return new IconButton(_.extend(button_config, global_config));
  107. })
  108. );
  109. return new IconButtonMenuView( {collection: buttons} );
  110. };
  111. // =============================================================================
  112. /**
  113. *
  114. */
  115. var Grid = Backbone.Collection.extend({
  116. });
  117. /**
  118. *
  119. */
  120. var GridView = Backbone.View.extend({
  121. });
  122. // =============================================================================
  123. /**
  124. * Necessary Galaxy paths.
  125. */
  126. var GalaxyPaths = Backbone.Model.extend({
  127. defaults: {
  128. root_path: "",
  129. image_path: ""
  130. }
  131. });
  132. // =============================================================================
  133. /** Global string localization object (and global short form alias)
  134. * set with either:
  135. * GalaxyLocalization.setLocalizedString( original, localized )
  136. * GalaxyLocalization.setLocalizedString({ original1 : localized1, original2 : localized2 })
  137. * get with either:
  138. * _l( original )
  139. */
  140. //TODO: move to Galaxy.Localization
  141. var GalaxyLocalization = jQuery.extend({}, {
  142. aliasName : '_l',
  143. localizedStrings : {},
  144. setLocalizedString : function( str_or_obj, localizedString ){
  145. // pass in either two strings (english, translated) or an obj (map) of english : translated attributes
  146. //console.debug( this + '.setLocalizedString:', str_or_obj, localizedString );
  147. var self = this;
  148. // DRY non-duplicate assignment function
  149. var setStringIfNotDuplicate = function( original, localized ){
  150. // do not set if identical - strcmp expensive but should only happen once per page per word
  151. if( original !== localized ){
  152. self.localizedStrings[ original ] = localized;
  153. }
  154. };
  155. if( jQuery.type( str_or_obj ) === "string" ){
  156. setStringIfNotDuplicate( str_or_obj, localizedString );
  157. } else if( jQuery.type( str_or_obj ) === "object" ){
  158. jQuery.each( str_or_obj, function( key, val ){
  159. //console.debug( 'key=>val', key, '=>', val );
  160. // could recurse here but no reason
  161. setStringIfNotDuplicate( key, val );
  162. });
  163. } else {
  164. throw( 'Localization.setLocalizedString needs either a string or object as the first argument,' +
  165. ' given: ' + str_or_obj );
  166. }
  167. },
  168. localize : function( strToLocalize ){
  169. //console.debug( this + '.localize:', strToLocalize );
  170. // return the localized version if it's there, the strToLocalize if not
  171. // try/catch cheaper than if in
  172. try {
  173. //var localized = this.localizedStrings[ strToLocalize ];
  174. //return localized;
  175. return this.localizedStrings[ strToLocalize ];
  176. } catch( err ){
  177. //TODO??: potentially problematic catch all
  178. //console.error( err );
  179. return strToLocalize;
  180. }
  181. },
  182. toString : function(){ return 'GalaxyLocalization'; }
  183. });
  184. // global localization alias
  185. window[ GalaxyLocalization.aliasName ] = function( str ){ return GalaxyLocalization.localize( str ); };
  186. //TEST: setLocalizedString( string, string ), _l( string )
  187. //TEST: setLocalizedString( hash ), _l( string )
  188. //TEST: setLocalizedString( string === string ), _l( string )
  189. //TEST: _l( non assigned string )
  190. // =============================================================================
  191. /** UI icon-button (Backbone.View only - no model)
  192. *
  193. */