PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/static/scripts/mvc/upload/upload-ftp.js

https://bitbucket.org/remy_d1/galaxy-central-manageapi
JavaScript | 180 lines | 116 code | 28 blank | 36 comment | 13 complexity | 42859f6e5ce2daaa881b692fb10286d6 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. // dependencies
  2. define(['utils/utils'], function(Utils) {
  3. // item view
  4. return Backbone.View.extend({
  5. // options
  6. options: {
  7. class_add : 'upload-icon-button fa fa-square-o',
  8. class_remove : 'upload-icon-button fa fa-check-square-o'
  9. },
  10. // render
  11. initialize: function(app) {
  12. // link app
  13. this.app = app;
  14. // link this
  15. var self = this;
  16. // set template
  17. this.setElement(this._template());
  18. // load extension
  19. Utils.get({
  20. url : galaxy_config.root + 'api/ftp_files',
  21. success : function(ftp_files) { self._fill(ftp_files); },
  22. error : function() { self._fill(); }
  23. });
  24. },
  25. // events
  26. events: {
  27. 'mousedown' : function(e) { e.preventDefault(); }
  28. },
  29. // fill table
  30. _fill: function(ftp_files) {
  31. if (ftp_files && ftp_files.length > 0) {
  32. // add table
  33. this.$el.find('#upload-ftp-content').html($(this._templateTable()));
  34. // add files to table
  35. var size = 0;
  36. for (key in ftp_files) {
  37. this.add(ftp_files[key]);
  38. size += ftp_files[key].size;
  39. }
  40. // update stats
  41. this.$el.find('#upload-ftp-number').html(ftp_files.length + ' files');
  42. this.$el.find('#upload-ftp-disk').html(Utils.bytesToString (size, true));
  43. } else {
  44. // add info
  45. this.$el.find('#upload-ftp-content').html($(this._templateInfo()));
  46. }
  47. // hide spinner
  48. this.$el.find('#upload-ftp-wait').hide();
  49. },
  50. // add
  51. add: function(ftp_file) {
  52. // link this
  53. var self = this;
  54. // create new item
  55. var $it = $(this._templateRow(ftp_file));
  56. // identify icon
  57. var $icon = $it.find('.icon');
  58. // append to table
  59. $(this.el).find('tbody').append($it);
  60. // find model and set initial 'add' icon class
  61. var icon_class = '';
  62. if (this._find(ftp_file)) {
  63. icon_class = this.options.class_remove;
  64. } else {
  65. icon_class = this.options.class_add;
  66. }
  67. // add icon class
  68. $icon.addClass(icon_class);
  69. // click to add ftp files
  70. $it.on('click', function() {
  71. // find model
  72. var model_index = self._find(ftp_file);
  73. // update icon
  74. $icon.removeClass();
  75. // add model
  76. if (!model_index) {
  77. // add to uploadbox
  78. self.app.uploadbox.add([{
  79. mode : 'ftp',
  80. name : ftp_file.path,
  81. size : ftp_file.size,
  82. path : ftp_file.path
  83. }]);
  84. // add new icon class
  85. $icon.addClass(self.options.class_remove);
  86. } else {
  87. // remove
  88. self.app.collection.remove(model_index);
  89. // add new icon class
  90. $icon.addClass(self.options.class_add);
  91. }
  92. });
  93. },
  94. // get model index
  95. _find: function(ftp_file) {
  96. // check if exists already
  97. var filtered = this.app.collection.where({file_path : ftp_file.path});
  98. var model_index = null;
  99. for (var key in filtered) {
  100. var item = filtered[key];
  101. if (item.get('status') == 'init' && item.get('file_mode') == 'ftp') {
  102. model_index = item.get('id');
  103. }
  104. }
  105. return model_index;
  106. },
  107. // template row
  108. _templateRow: function(options) {
  109. return '<tr class="upload-ftp-row" style="cursor: pointer;">' +
  110. '<td><div class="icon"/></td>' +
  111. '<td style="width: 200px"><p style="width: inherit; word-wrap: break-word;">' + options.path + '</p></td>' +
  112. '<td style="white-space: nowrap;">' + Utils.bytesToString(options.size) + '</td>' +
  113. '<td style="white-space: nowrap;">' + options.ctime + '</td>' +
  114. '</tr>';
  115. },
  116. // load table template
  117. _templateTable: function() {
  118. return '<span style="whitespace: nowrap; float: left;">Available files: </span>' +
  119. '<span style="whitespace: nowrap; float: right;">' +
  120. '<span class="upload-icon fa fa-file-text-o"/>' +
  121. '<span id="upload-ftp-number"/>&nbsp;&nbsp;' +
  122. '<span class="upload-icon fa fa-hdd-o"/>' +
  123. '<span id="upload-ftp-disk"/>' +
  124. '</span>' +
  125. '<table class="grid" style="float: left;">' +
  126. '<thead>' +
  127. '<tr>' +
  128. '<th></th>' +
  129. '<th>Name</th>' +
  130. '<th>Size</th>' +
  131. '<th>Created</th>' +
  132. '</tr>' +
  133. '</thead>' +
  134. '<tbody></tbody>' +
  135. '</table>';
  136. },
  137. // load table template
  138. _templateInfo: function() {
  139. return '<div class="upload-ftp-warning warningmessage">' +
  140. 'Your FTP directory does not contain any files.' +
  141. '</div>';
  142. },
  143. // load html template
  144. _template: function() {
  145. return '<div class="upload-ftp">' +
  146. '<div id="upload-ftp-wait" class="upload-ftp-wait fa fa-spinner fa-spin"/>' +
  147. '<div class="upload-ftp-help">This Galaxy server allows you to upload files via FTP. To upload some files, log in to the FTP server at <strong>' + this.app.options.ftp_upload_site + '</strong> using your Galaxy credentials (email address and password).</div>' +
  148. '<div id="upload-ftp-content"></div>' +
  149. '<div>';
  150. }
  151. });
  152. });