2872: Make infinite scroll work with regular window scrollbars too.
[arvados.git] / apps / workbench / app / controllers / folders_controller.rb
1 class FoldersController < ApplicationController
2   def model_class
3     Group
4   end
5
6   def index_pane_list
7     %w(Folders)
8   end
9
10   def show_pane_list
11     %w(Contents Permissions Advanced)
12   end
13
14   def remove_item
15     @removed_uuids = []
16     links = []
17     item = ArvadosBase.find params[:item_uuid]
18     if (item.class == Link and
19         item.link_class == 'name' and
20         item.tail_uuid = @object.uuid)
21       # Given uuid is a name link, linking an object to this
22       # folder. First follow the link to find the item we're removing,
23       # then delete the link.
24       links << item
25       item = ArvadosBase.find item.head_uuid
26     else
27       # Given uuid is an object. Delete all names.
28       links += Link.where(tail_uuid: @object.uuid,
29                           head_uuid: item.uuid,
30                           link_class: 'name')
31     end
32     links.each do |link|
33       @removed_uuids << link.uuid
34       link.destroy
35     end
36     if item.owner_uuid == @object.uuid
37       # Object is owned by this folder. Remove it from the folder by
38       # changing owner to the current user.
39       item.update_attributes owner_uuid: current_user.uuid
40       @removed_uuids << item.uuid
41     end
42   end
43
44   def find_objects_for_index
45     @objects = Group.where(group_class: 'folder').order('name')
46     super
47     parent_of = {current_user.uuid => 'me'}
48     @objects.each do |ob|
49       parent_of[ob.uuid] = ob.owner_uuid
50     end
51     children_of = {false => [], 'me' => [current_user]}
52     @objects.each do |ob|
53       if ob.owner_uuid != current_user.uuid and
54           not parent_of.has_key? ob.owner_uuid
55         parent_of[ob.uuid] = false
56       end
57       children_of[parent_of[ob.uuid]] ||= []
58       children_of[parent_of[ob.uuid]] << ob
59     end
60     buildtree = lambda do |children_of, root_uuid=false|
61       tree = {}
62       children_of[root_uuid].andand.each do |ob|
63         tree[ob] = buildtree.call(children_of, ob.uuid)
64       end
65       tree
66     end
67     sorted_paths = lambda do |tree, depth=0|
68       paths = []
69       tree.keys.sort_by { |ob|
70         ob.is_a?(String) ? ob : ob.friendly_link_name
71       }.each do |ob|
72         paths << {object: ob, depth: depth}
73         paths += sorted_paths.call tree[ob], depth+1
74       end
75       paths
76     end
77     @my_folder_tree =
78       sorted_paths.call buildtree.call(children_of, 'me')
79     @shared_folder_tree =
80       sorted_paths.call({'Shared with me' =>
81                           buildtree.call(children_of, false)})
82   end
83
84   def show
85     @objects = @object.contents(limit: 50,
86                                 include_linked: true,
87                                 offset: params[:offset] || 0)
88     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
89                                 ['link_class', '=', 'permission']])
90     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
91
92     @objects_and_names = []
93     @objects.each do |object|
94       if !(name_links = @objects.links_for(object, 'name')).empty?
95         name_links.each do |name_link|
96           @objects_and_names << [object, name_link]
97         end
98       else
99         @objects_and_names << [object,
100                                Link.new(owner_uuid: @object.uuid,
101                                         tail_uuid: @object.uuid,
102                                         head_uuid: object.uuid,
103                                         link_class: "name",
104                                         name: "")]
105       end
106     end
107     if params[:partial]
108       respond_to do |f|
109         f.json {
110           render json: {
111             content: render_to_string(partial: 'show_contents_rows.html',
112                                       formats: [:html],
113                                       locals: {
114                                         objects_and_names: @objects_and_names,
115                                         folder: @object
116                                       }),
117             next_page_href: (next_page_offset and
118                              url_for(offset: next_page_offset, partial: true))
119           }
120         }
121       end
122     else
123       super
124     end
125   end
126
127   def create
128     @new_resource_attrs = (params['folder'] || {}).merge(group_class: 'folder')
129     @new_resource_attrs[:name] ||= 'New folder'
130     super
131   end
132
133   def update
134     @updates = params['folder']
135     super
136   end
137 end