PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/client/galaxy/scripts/mvc/user/user-model.js

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 130 lines | 72 code | 19 blank | 39 comment | 9 complexity | 6dd8600daef4c09f7d950055c5c48a80 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. "mvc/base-mvc",
  3. "utils/localization"
  4. ], function( baseMVC, _l ){
  5. //==============================================================================
  6. /** @class Model for a Galaxy user (including anonymous users).
  7. * @name User
  8. *
  9. * @augments Backbone.Model
  10. * @borrows LoggableMixin#logger as #logger
  11. * @borrows LoggableMixin#log as #log
  12. * @constructs
  13. */
  14. var User = Backbone.Model.extend( baseMVC.LoggableMixin ).extend(
  15. /** @lends User.prototype */{
  16. ///** logger used to record this.log messages, commonly set to console */
  17. //// comment this out to suppress log output
  18. //logger : console,
  19. /** API location for this resource */
  20. urlRoot : galaxy_config.root + 'api/users',
  21. /** Model defaults
  22. * Note: don't check for anon-users with the username as the default is '(anonymous user)'
  23. * a safer method is if( !user.get( 'email' ) ) -> anon user
  24. */
  25. defaults : /** @lends User.prototype */{
  26. id : null,
  27. username : '(' + _l( "anonymous user" ) + ')',
  28. email : "",
  29. total_disk_usage : 0,
  30. nice_total_disk_usage : "",
  31. quota_percent : null,
  32. is_admin : false
  33. },
  34. /** Set up and bind events
  35. * @param {Object} data Initial model data.
  36. */
  37. initialize : function( data ){
  38. this.log( 'User.initialize:', data );
  39. this.on( 'loaded', function( model, resp ){ this.log( this + ' has loaded:', model, resp ); });
  40. this.on( 'change', function( model, data ){ this.log( this + ' has changed:', model, data.changes ); });
  41. },
  42. isAnonymous : function(){
  43. return ( !this.get( 'email' ) );
  44. },
  45. isAdmin : function(){
  46. return ( this.get( 'is_admin' ) );
  47. },
  48. /** Load a user with the API using an id.
  49. * If getting an anonymous user or no access to a user id, pass the User.CURRENT_ID_STR
  50. * (e.g. 'current') and the API will return the current transaction's user data.
  51. * @param {String} idOrCurrent encoded user id or the User.CURRENT_ID_STR
  52. * @param {Object} options hash to pass to Backbone.Model.fetch. Can contain success, error fns.
  53. * @fires loaded when the model has been loaded from the API, passing the newModel and AJAX response.
  54. */
  55. loadFromApi : function( idOrCurrent, options ){
  56. idOrCurrent = idOrCurrent || User.CURRENT_ID_STR;
  57. options = options || {};
  58. var model = this,
  59. userFn = options.success;
  60. /** @ignore */
  61. options.success = function( newModel, response ){
  62. model.trigger( 'loaded', newModel, response );
  63. if( userFn ){ userFn( newModel, response ); }
  64. };
  65. // requests for the current user must have a sep. constructed url (fetch don't work, ma)
  66. if( idOrCurrent === User.CURRENT_ID_STR ){
  67. options.url = this.urlRoot + '/' + User.CURRENT_ID_STR;
  68. }
  69. return Backbone.Model.prototype.fetch.call( this, options );
  70. },
  71. /** Clears all data from the sessionStorage.
  72. */
  73. clearSessionStorage : function(){
  74. for( var key in sessionStorage ){
  75. //TODO: store these under the user key so we don't have to do this
  76. // currently only history
  77. if( key.indexOf( 'history:' ) === 0 ){
  78. sessionStorage.removeItem( key );
  79. } else if( key === 'history-panel' ){
  80. sessionStorage.removeItem( key );
  81. }
  82. }
  83. },
  84. /** string representation */
  85. toString : function(){
  86. var userInfo = [ this.get( 'username' ) ];
  87. if( this.get( 'id' ) ){
  88. userInfo.unshift( this.get( 'id' ) );
  89. userInfo.push( this.get( 'email' ) );
  90. }
  91. return 'User(' + userInfo.join( ':' ) + ')';
  92. }
  93. });
  94. // string to send to tell server to return this transaction's user (see api/users.py)
  95. User.CURRENT_ID_STR = 'current';
  96. // class method to load the current user via the api and return that model
  97. User.getCurrentUserFromApi = function( options ){
  98. var currentUser = new User();
  99. currentUser.loadFromApi( User.CURRENT_ID_STR, options );
  100. return currentUser;
  101. };
  102. // (stub) collection for users (shouldn't be common unless admin UI)
  103. var UserCollection = Backbone.Collection.extend( baseMVC.LoggableMixin ).extend({
  104. model : User,
  105. urlRoot : galaxy_config.root + 'api/users'
  106. //logger : console,
  107. });
  108. //==============================================================================
  109. return {
  110. User : User
  111. };});