7ab5047cb8d2c886a4830b56f46d629ce092989e
[arvados.git] / apps / workbench / app / assets / javascripts / components / collections.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 window.components = window.components || {}
6 window.components.collection_table_narrow = {
7     view: function(vnode) {
8         return m('table.table.table-condensed', [
9             m('thead', m('tr', m('th', vnode.attrs.key))),
10             m('tbody', [
11                 vnode.attrs.items().map(function(item) {
12                     return m('tr', [
13                         m('td', [
14                             m('a', {href: vnode.attrs.session.baseURL.replace('://', '://workbench.')+'/collections/'+item.uuid}, item.name || '(unnamed)'),
15                             m('br'),
16                             item.modified_at,
17                         ]),
18                     ])
19                 }),
20             ]),
21         ])
22     },
23 }
24
25 window.components.collection_search = {
26     oninit: function(vnode) {
27         vnode.state.sessionDB = new window.models.SessionDB()
28         vnode.state.searchEntered = m.stream('')
29         vnode.state.searchStart = m.stream('')
30         vnode.state.items = {}
31         vnode.state.searchStart.map(function(q) {
32             var sessions = vnode.state.sessionDB.loadAll()
33             var cookie = (new Date()).getTime()
34             vnode.state.cookie = cookie
35             Object.keys(sessions).map(function(key) {
36                 if (!vnode.state.items[key])
37                     vnode.state.items[key] = m.stream([])
38                 vnode.state.items[key].dirty = true
39                 vnode.state.sessionDB.request(sessions[key], 'arvados/v1/collections', {
40                     data: {
41                         filters: JSON.stringify(!q ? [] : [['any', '@@', q+':*']]),
42                     },
43                 }).then(function(resp) {
44                     if (cookie !== vnode.state.cookie)
45                         // a newer query is in progress; ignore this result.
46                         return
47                     vnode.state.items[key](resp.items)
48                     vnode.state.items[key].dirty = false
49                 })
50             })
51         })
52     },
53     view: function(vnode) {
54         var items = vnode.state.items
55         var sessions = vnode.state.sessionDB.loadAll()
56         return m('form', {
57             onsubmit: function() {
58                 vnode.state.searchStart(vnode.state.searchEntered())
59                 return false
60             },
61         }, [
62             m('.row', [
63                 m('.col-md-6', [
64                     m('.input-group', [
65                         m('input#search.form-control[placeholder=Search]', {
66                             oninput: m.withAttr('value', debounce(200, vnode.state.searchEntered)),
67                         }),
68                         m('.input-group-btn', [
69                             m('input.btn.btn-primary[type=submit][value="Search"]'),
70                         ]),
71                     ]),
72                 ]),
73                 m('.col-md-6', [
74                     'Searching sites: ',
75                     Object.keys(items).length == 0
76                         ? m('span.label.label-xs.label-danger', 'none')
77                         : Object.keys(items).sort().map(function(key) {
78                             return [m('span.label.label-xs.label-info', key), ' ']
79                         }),
80                     ' ',
81                     m('a[href="/sessions"]', 'Add/remove sites'),
82                 ]),
83             ]),
84             m('.row', Object.keys(items).sort().map(function(key) {
85                 return m('.col-md-3', {key: key, style: {
86                     opacity: items[key].dirty ? 0.5 : 1,
87                 }}, [
88                     m(window.components.collection_table_narrow, {
89                         key: key,
90                         session: sessions[key],
91                         items: items[key],
92                     }),
93                 ])
94             })),
95         ])
96     },
97 }
98
99 function debounce(t, f) {
100     // Return a new function that waits until t milliseconds have
101     // passed since it was last called, then calls f with its most
102     // recent arguments.
103     var this_was = this
104     var pending
105     return function() {
106         var args = arguments
107         if (pending) {
108             console.log("debounce!")
109             window.clearTimeout(pending)
110         }
111         pending = window.setTimeout(function() {
112             pending = undefined
113             f.apply(this_was, args)
114         }, t)
115     }
116 }