1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 window.SearchResultsTable = {
6 maybeLoadMore: function(dom) {
7 var loader = this.loader
8 if (loader.state != loader.READY)
9 // Can't start getting more items anyway: no point in
10 // checking anything else.
12 var contentRect = dom.getBoundingClientRect()
13 var scroller = window // TODO: use dom's nearest ancestor with scrollbars
14 if (contentRect.bottom < 2 * scroller.innerHeight) {
15 // We have less than 1 page worth of content available
16 // below the visible area. Load more.
18 // Indicate loading is in progress.
19 window.requestAnimationFrame(m.redraw)
22 oncreate: function(vnode) {
23 vnode.state.maybeLoadMore = vnode.state.maybeLoadMore.bind(vnode.state, vnode.dom)
24 window.addEventListener('scroll', vnode.state.maybeLoadMore)
25 window.addEventListener('resize', vnode.state.maybeLoadMore)
26 vnode.state.timer = window.setInterval(vnode.state.maybeLoadMore, 200)
27 vnode.state.loader = vnode.attrs.loader
28 vnode.state.onupdate(vnode)
30 onupdate: function(vnode) {
31 vnode.state.loader = vnode.attrs.loader
33 onremove: function(vnode) {
34 window.clearInterval(vnode.state.timer)
35 window.removeEventListener('scroll', vnode.state.maybeLoadMore)
36 window.removeEventListener('resize', vnode.state.maybeLoadMore)
38 view: function(vnode) {
39 var loader = vnode.attrs.loader
41 collections: m('i.fa.fa-fw.fa-archive'),
42 projects: m('i.fa.fa-fw.fa-folder'),
44 var db = new SessionDB()
45 var sessions = db.loadActive()
46 return m('table.table.table-condensed', [
51 m('th', 'last modified'),
54 loader.items().map(function(item) {
55 var session = sessions[item.uuid.slice(0,5)]
57 // Add the salted token to search result links from federated
59 if (!session.isFromRails && session.token.indexOf('v2/') == 0) {
60 tokenParam = session.token
64 action: item.workbenchBaseURL() + '/' + item.objectType.wb_path + '/' + item.uuid,
68 m('input[type=hidden][name=api_token][value='+tokenParam+']'),
69 item.workbenchBaseURL() &&
70 m('button.btn.btn-xs.btn-default[type=submit]', {
71 'data-original-title': 'show '+item.objectType.description,
72 'data-placement': 'top',
73 'data-toggle': 'tooltip',
74 // Bootstrap's tooltip feature
75 oncreate: function(vnode) { $(vnode.dom).tooltip() },
76 }, iconsMap[item.objectType.wb_path]),
78 m('td.arvados-uuid', item.uuid),
79 m('td', item.name || '(unnamed)'),
80 m('td', m(LocalizedDateTime, {parse: item.modified_at})),
84 loader.state == loader.DONE ? null : m('tfoot', m('tr', [
85 m('th[colspan=4]', m('button.btn.btn-xs', {
86 className: loader.state == loader.LOADING ? 'btn-default' : 'btn-primary',
93 disabled: loader.state == loader.LOADING,
98 }, loader.state == loader.LOADING ? '(loading)' : 'Load more')),
105 oninit: function(vnode) {
106 vnode.state.sessionDB = new SessionDB()
107 vnode.state.searchEntered = m.stream()
108 vnode.state.searchActive = m.stream()
109 // When searchActive changes (e.g., when restoring state
110 // after navigation), update the text field too.
111 vnode.state.searchActive.map(vnode.state.searchEntered)
112 // When searchActive changes, create a new loader that filters
113 // with the given search term.
114 vnode.state.searchActive.map(function(q) {
115 var sessions = vnode.state.sessionDB.loadActive()
116 vnode.state.loader = new MergingLoader({
117 children: Object.keys(sessions).map(function(key) {
118 var session = sessions[key]
119 var workbenchBaseURL = function() {
120 return vnode.state.sessionDB.workbenchBaseURL(session)
122 var searchable_objects = [
125 api_path: 'arvados/v1/groups',
126 filters: [['group_class', '=', 'project']],
127 description: 'project',
130 wb_path: 'collections',
131 api_path: 'arvados/v1/collections',
133 description: 'collection',
136 return new MergingLoader({
138 // For every session, search for every object type
139 children: searchable_objects.map(function(obj_type) {
140 return new MultipageLoader({
142 loadFunc: function(filters) {
143 // Apply additional type dependant filters
144 filters = filters.concat(obj_type.filters)
145 var tsquery = to_tsquery(q)
147 filters.push(['any', '@@', tsquery])
149 return vnode.state.sessionDB.request(session, obj_type.api_path, {
151 filters: JSON.stringify(filters),
154 }).then(function(resp) {
155 resp.items.map(function(item) {
156 item.workbenchBaseURL = workbenchBaseURL
157 item.objectType = obj_type
169 view: function(vnode) {
171 onsubmit: function() {
172 vnode.state.searchActive(vnode.state.searchEntered())
173 vnode.state.forgetSavedHeight = true
179 currentState: vnode.state.searchActive,
180 forgetSavedHeight: vnode.state.forgetSavedHeight,
181 saveBodyHeight: true,
183 vnode.state.loader && [
187 m('input#search.form-control[placeholder=Search collections and projects]', {
188 oninput: m.withAttr('value', vnode.state.searchEntered),
189 value: vnode.state.searchEntered(),
191 m('.input-group-btn', [
192 m('input.btn.btn-primary[type=submit][value="Search"]'),
198 vnode.state.loader.children.length == 0
199 ? m('span.label.label-xs.label-danger', 'none')
200 : vnode.state.loader.children.map(function(child) {
201 return [m('span.label.label-xs', {
202 className: child.state == child.LOADING ? 'label-warning' : 'label-success',
203 }, child.sessionKey), ' ']
206 m('a[href="/sessions"]', 'Add/remove sites'),
209 m(SearchResultsTable, {
210 loader: vnode.state.loader,