PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/DocDoku-ear/DocDoku-war/src/main/webapp/js/sidebar-nav.js

http://docdoku.googlecode.com/
JavaScript | 221 lines | 159 code | 55 blank | 7 comment | 9 complexity | 8b31ff1453238ec2161cd9980e1794e9 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. Folder = Backbone.Model.extend({
  6. defaults:{
  7. id:null,
  8. name:null,
  9. completePath:null
  10. },
  11. initialize:function(){
  12. this.bind("error", function(model, error){
  13. console.log( error );
  14. });
  15. },
  16. getId : function() {
  17. return this.get('id');
  18. },
  19. setId : function(value) {
  20. this.set({ id : value });
  21. return this;
  22. },
  23. getName : function() {
  24. return this.get('name');
  25. },
  26. setName : function(value) {
  27. this.set({ name : value });
  28. return this;
  29. },
  30. getCompletePath : function() {
  31. return this.get('completePath');
  32. },
  33. setCompletePath : function(value) {
  34. this.set({ completePath : value });
  35. return this;
  36. },
  37. isOpen : function() {
  38. return this.get('isOpen');
  39. },
  40. setOpen : function(value) {
  41. this.set({ isOpen : value });
  42. return this;
  43. },
  44. isHome : function() {
  45. return this.get('folderType')=="home";
  46. },
  47. });
  48. FolderList = Backbone.Collection.extend({
  49. model: Folder,
  50. initialize : function(models, options) {
  51. this.path = options.completePath;
  52. if (options.homeFolder) {
  53. this.homeFolder = options.homeFolder;
  54. }
  55. this.url = "../api/folders/"+this.path;
  56. },
  57. parse: function(data) {
  58. var folders = [];
  59. var that = this;
  60. _.each(data,function(item){
  61. var folder = {};
  62. folder.id = that.path +"/"+ item;
  63. folder.name = item;
  64. folder.isOpen = false;
  65. folder.completePath = that.path +"/"+ item;
  66. folder.folderType= "regular";
  67. folders.push(folder);
  68. });
  69. return folders;
  70. },
  71. comparator:function(folderA, folderB){
  72. folderNameA=folderA.getName();
  73. folderNameB=folderB.getName();
  74. if(folderB.isHome()){
  75. return 1;
  76. }
  77. if(folderA.isHome()){
  78. return -1;
  79. }
  80. if(folderNameA==folderNameB){
  81. return 0;
  82. }
  83. return (folderNameA<folderNameB) ? -1 : 1;
  84. }
  85. });
  86. FolderView = Backbone.View.extend({
  87. tagName:"li",
  88. className: "nav-header closed_folder",
  89. template:_.template("<%= name %>"),
  90. events: {"click":"onFolderClick"},
  91. initialize:function(){
  92. this.model.bind("change:isOpen", this.setOpen, this);
  93. },
  94. setOpen: function() {
  95. if (this.model.isOpen()) {
  96. $(this.el).removeClass("closed_folder");
  97. $(this.el).addClass("open_folder");
  98. } else {
  99. $(this.el).removeClass("open_folder");
  100. $(this.el).addClass("closed_folder");
  101. }
  102. },
  103. render : function() {
  104. $(this.el).html(this.template(this.model.toJSON()));
  105. if(this.model.isHome()){
  106. $(this.el).addClass("account_home_folder");
  107. }
  108. return this;
  109. },
  110. onFolderClick: function(e){
  111. e.stopPropagation();
  112. // View Folder Status Closed
  113. if(!this.model.isOpen()){
  114. this.subFolders = new FolderList([],{completePath : this.model.getCompletePath()});
  115. var subFolderContainerView = new FolderContainerView({collection: this.subFolders});
  116. $(this.el).append(subFolderContainerView.render().el);
  117. this.model.setOpen(true);
  118. }
  119. else{
  120. this.model.setOpen(false);
  121. // View Folder Status Closed
  122. $(this.el).find('ul').slideUp('normal', function() {
  123. $(this).remove();
  124. })
  125. this.isFolderViewOpen==false;
  126. }
  127. }
  128. });
  129. FolderContainerView = Backbone.View.extend({
  130. tagName:"ul",
  131. initialize: function() {
  132. _.bindAll(this,'addOne','addAllAndShow');
  133. this.collection.bind('add', this.addOne);
  134. this.collection.bind('reset', this.addAllAndShow);
  135. this.collection.fetch();
  136. },
  137. addAllAndShow: function(){
  138. if (this.collection.homeFolder) {
  139. this.collection.add(this.collection.homeFolder, {silent:true});
  140. //this.collection.sort( {silent:true});
  141. }
  142. this.collection.each(this.addOne);
  143. $(this.el).slideDown();
  144. },
  145. addOne: function(folder){
  146. var view = new FolderView({model: folder});
  147. $(this.el).append(view.render().el);
  148. }
  149. });
  150. AppView = Backbone.View.extend({
  151. el:$(".sidebar-nav"),
  152. initialize: function() {
  153. var home = {};
  154. home.id = inputs.workspaceID+"/~"+inputs.login;
  155. home.name = "~"+inputs.login;
  156. home.isOpen = false;
  157. home.completePath = inputs.workspaceID+"/~"+inputs.login;
  158. home.folderType = "home";
  159. var firstLevelFolders = new FolderList([],{homeFolder: home, completePath:inputs.workspaceID});
  160. var firstLevelView = new FolderContainerView({collection: firstLevelFolders});
  161. $(this.el).append(firstLevelView.render().el);
  162. }
  163. });
  164. $(document).ready(function () {
  165. new AppView();
  166. });