PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/mvc/history/history-content-model.js

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 133 lines | 74 code | 17 blank | 42 comment | 9 complexity | 247dcdcb8bae4bd36fa5fc1040de3f15 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. "mvc/dataset/states",
  3. "mvc/base-mvc",
  4. "utils/localization"
  5. ], function( STATES, BASE_MVC, _l ){
  6. //==============================================================================
  7. /** How the type_id attribute is built for the history's mixed contents collection */
  8. var typeIdStr = function _typeIdStr( type, id ){
  9. return [ type, id ].join( '-' );
  10. };
  11. //==============================================================================
  12. /** @class Mixin for HistoryContents content (HDAs, HDCAs).
  13. */
  14. var HistoryContentMixin = {
  15. //TODO:?? into true Backbone.Model?
  16. /** default attributes for a model */
  17. defaults : {
  18. /** parent (containing) history */
  19. history_id : null,
  20. /** some content_type (HistoryContents can contain mixed model classes) */
  21. history_content_type: null,
  22. /** indicating when/what order the content was generated in the context of the history */
  23. hid : null,
  24. /** whether the user wants the content shown (visible) */
  25. visible : true
  26. },
  27. // ........................................................................ mixed content element
  28. //TODO: there's got to be a way to move this into HistoryContents - if we can do that, this class might not be needed
  29. // In order to be part of a MIXED bbone collection, we can't rely on the id
  30. // (which may collide btwn models of different classes)
  31. // Build a new id (type_id) that prefixes the history_content_type so the bbone collection can differentiate
  32. idAttribute : 'type_id',
  33. /** override constructor to build type_id and insert into original attributes */
  34. constructor : function( attrs, options ){
  35. attrs.type_id = typeIdStr( attrs.history_content_type, attrs.id );
  36. this.debug( 'HistoryContentMixin.constructor:', attrs.type_id );
  37. Backbone.Model.apply( this, arguments );
  38. },
  39. /** object level fn for building the type_id string */
  40. _typeIdStr : function(){
  41. return typeIdStr( this.get( 'history_content_type' ), this.get( 'id' ) );
  42. },
  43. /** add listener to re-create type_id when the id changes */
  44. initialize : function( attrs, options ){
  45. this.on( 'change:id', this._createTypeId );
  46. },
  47. /** set the type_id in the model attributes */
  48. _createTypeId : function(){
  49. this.set( 'type_id', this._typeIdStr() );
  50. },
  51. // ........................................................................ common queries
  52. /** the more common alias of visible */
  53. hidden : function(){
  54. return !this.get( 'visible' );
  55. },
  56. /** based on show_deleted, show_hidden (gen. from the container control),
  57. * would this ds show in the list of ds's?
  58. * @param {Boolean} show_deleted are we showing deleted hdas?
  59. * @param {Boolean} show_hidden are we showing hidden hdas?
  60. */
  61. isVisible : function( show_deleted, show_hidden ){
  62. //TODO:?? Another unfortunate name collision
  63. var isVisible = true;
  64. if( ( !show_deleted )
  65. && ( this.get( 'deleted' ) || this.get( 'purged' ) ) ){
  66. isVisible = false;
  67. }
  68. if( ( !show_hidden )
  69. && ( !this.get( 'visible' ) ) ){
  70. isVisible = false;
  71. }
  72. return isVisible;
  73. },
  74. // ........................................................................ ajax
  75. //TODO: global
  76. //TODO: these are probably better done on the leaf classes
  77. /** history content goes through the 'api/histories' API */
  78. urlRoot: galaxy_config.root + 'api/histories/',
  79. /** full url spec. for this content */
  80. url : function(){
  81. var url = this.urlRoot + this.get( 'history_id' ) + '/contents/'
  82. + this.get('history_content_type') + 's/' + this.get( 'id' );
  83. return url;
  84. },
  85. /** save this content as not visible */
  86. hide : function( options ){
  87. if( !this.get( 'visible' ) ){ return jQuery.when(); }
  88. return this.save( { visible: false }, options );
  89. },
  90. /** save this content as visible */
  91. unhide : function( options ){
  92. if( this.get( 'visible' ) ){ return jQuery.when(); }
  93. return this.save( { visible: true }, options );
  94. },
  95. // ........................................................................ misc
  96. /** String representation */
  97. toString : function(){
  98. var nameAndId = this.get( 'id' ) || '';
  99. if( this.get( 'name' ) ){
  100. nameAndId = this.get( 'hid' ) + ' :"' + this.get( 'name' ) + '",' + nameAndId;
  101. }
  102. return 'HistoryContent(' + nameAndId + ')';
  103. }
  104. };
  105. //==============================================================================
  106. //TODO: needed?
  107. /** @class (Concrete/non-mixin) base model for content items.
  108. */
  109. var HistoryContent = Backbone.Model.extend( BASE_MVC.LoggableMixin ).extend( HistoryContentMixin );
  110. //==============================================================================
  111. return {
  112. typeIdStr : typeIdStr,
  113. HistoryContentMixin : HistoryContentMixin,
  114. HistoryContent : HistoryContent
  115. };
  116. });