X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/c3af5da0100777902f4d968c5431630e713a511f..3854e6bfcd5344bce5ee0388248cb115e3c5e902:/apps/workbench/app/assets/javascripts/components/collections.js diff --git a/apps/workbench/app/assets/javascripts/components/collections.js b/apps/workbench/app/assets/javascripts/components/collections.js index 625539c267..33fca6c7b7 100644 --- a/apps/workbench/app/assets/javascripts/components/collections.js +++ b/apps/workbench/app/assets/javascripts/components/collections.js @@ -2,116 +2,172 @@ // // SPDX-License-Identifier: AGPL-3.0 -window.components = window.components || {} -window.components.collection_table_narrow = { +window.CollectionsTable = { + maybeLoadMore: function(dom) { + var loader = this.loader + if (loader.state != loader.READY) + // Can't start getting more items anyway: no point in + // checking anything else. + return + var contentRect = dom.getBoundingClientRect() + var scroller = window // TODO: use dom's nearest ancestor with scrollbars + if (contentRect.bottom < 2 * scroller.innerHeight) { + // We have less than 1 page worth of content available + // below the visible area. Load more. + loader.loadMore() + // Indicate loading is in progress. + window.requestAnimationFrame(m.redraw) + } + }, + oncreate: function(vnode) { + vnode.state.maybeLoadMore = vnode.state.maybeLoadMore.bind(vnode.state, vnode.dom) + window.addEventListener('scroll', vnode.state.maybeLoadMore) + window.addEventListener('resize', vnode.state.maybeLoadMore) + vnode.state.timer = window.setInterval(vnode.state.maybeLoadMore, 200) + vnode.state.loader = vnode.attrs.loader + vnode.state.onupdate(vnode) + }, + onupdate: function(vnode) { + vnode.state.loader = vnode.attrs.loader + }, + onremove: function(vnode) { + window.clearInterval(vnode.state.timer) + window.removeEventListener('scroll', vnode.state.maybeLoadMore) + window.removeEventListener('resize', vnode.state.maybeLoadMore) + }, view: function(vnode) { + var loader = vnode.attrs.loader return m('table.table.table-condensed', [ - m('thead', m('tr', m('th', vnode.attrs.key))), + m('thead', m('tr', [ + m('th'), + m('th', 'uuid'), + m('th', 'name'), + m('th', 'last modified'), + ])), m('tbody', [ - vnode.attrs.items().map(function(item) { + loader.items().map(function(item) { return m('tr', [ m('td', [ - m('a', {href: vnode.attrs.session.baseURL.replace('://', '://workbench.')+'/collections/'+item.uuid}, item.name || '(unnamed)'), - m('br'), - m(window.components.datetime, {parse: item.modified_at}), + // Guess workbench.{apihostport} is a + // Workbench... unless the host part of + // apihostport is an IPv4 or [IPv6] + // address. + item.session.baseURL.match('://(\\[|\\d+\\.\\d+\\.\\d+\\.\\d+[:/])') ? null : + m('a.btn.btn-xs.btn-default', { + href: item.session.baseURL.replace('://', '://workbench.')+'collections/'+item.uuid, + }, 'Show'), ]), + m('td.arvados-uuid', item.uuid), + m('td', item.name || '(unnamed)'), + m('td', m(LocalizedDateTime, {parse: item.modified_at})), ]) }), ]), + loader.state == loader.DONE ? null : m('tfoot', m('tr', [ + m('th[colspan=4]', m('button.btn.btn-xs', { + className: loader.state == loader.LOADING ? 'btn-default' : 'btn-primary', + style: { + display: 'block', + width: '12em', + marginLeft: 'auto', + marginRight: 'auto', + }, + disabled: loader.state == loader.LOADING, + onclick: function() { + loader.loadMore() + return false + }, + }, loader.state == loader.LOADING ? '(loading)' : 'Load more')), + ])), ]) }, } -window.components.collection_search = { +window.CollectionsSearch = { oninit: function(vnode) { - vnode.state.sessionDB = new window.models.SessionDB() - vnode.state.searchEntered = m.stream('') - vnode.state.searchStart = m.stream('') - vnode.state.items = {} - vnode.state.searchStart.map(function(q) { - var sessions = vnode.state.sessionDB.loadAll() - var cookie = (new Date()).getTime() - vnode.state.cookie = cookie - Object.keys(sessions).map(function(key) { - if (!vnode.state.items[key]) - vnode.state.items[key] = m.stream([]) - vnode.state.items[key].dirty = true - vnode.state.sessionDB.request(sessions[key], 'arvados/v1/collections', { - data: { - filters: JSON.stringify(!q ? [] : [['any', '@@', q+':*']]), - count: 'none', - }, - }).then(function(resp) { - if (cookie !== vnode.state.cookie) - // a newer query is in progress; ignore this result. - return - vnode.state.items[key](resp.items) - vnode.state.items[key].dirty = false + vnode.state.sessionDB = new SessionDB() + vnode.state.searchEntered = m.stream() + vnode.state.searchActive = m.stream() + // When searchActive changes (e.g., when restoring state + // after navigation), update the text field too. + vnode.state.searchActive.map(vnode.state.searchEntered) + // When searchActive changes, create a new loader that filters + // with the given search term. + vnode.state.searchActive.map(function(q) { + var sessions = vnode.state.sessionDB.loadActive() + vnode.state.loader = new MergingLoader({ + children: Object.keys(sessions).map(function(key) { + var session = sessions[key] + return new MultipageLoader({ + sessionKey: key, + loadFunc: function(filters) { + var tsquery = to_tsquery(q) + if (tsquery) { + filters = filters.slice(0) + filters.push(['any', '@@', tsquery]) + } + return vnode.state.sessionDB.request(session, 'arvados/v1/collections', { + data: { + filters: JSON.stringify(filters), + count: 'none', + }, + }).then(function(resp) { + resp.items.map(function(item) { + item.session = session + }) + return resp + }) + }, + }) }) }) }) }, view: function(vnode) { - var items = vnode.state.items var sessions = vnode.state.sessionDB.loadAll() return m('form', { onsubmit: function() { - vnode.state.searchStart(vnode.state.searchEntered()) + vnode.state.searchActive(vnode.state.searchEntered()) + vnode.state.forgetSavedHeight = true return false }, }, [ - m('.row', [ - m('.col-md-6', [ - m('.input-group', [ - m('input#search.form-control[placeholder=Search]', { - oninput: m.withAttr('value', debounce(200, vnode.state.searchEntered)), - }), - m('.input-group-btn', [ - m('input.btn.btn-primary[type=submit][value="Search"]'), + m(SaveUIState, { + defaultState: '', + currentState: vnode.state.searchActive, + forgetSavedHeight: vnode.state.forgetSavedHeight, + saveBodyHeight: true, + }), + vnode.state.loader && [ + m('.row', [ + m('.col-md-6', [ + m('.input-group', [ + m('input#search.form-control[placeholder=Search]', { + oninput: m.withAttr('value', vnode.state.searchEntered), + value: vnode.state.searchEntered(), + }), + m('.input-group-btn', [ + m('input.btn.btn-primary[type=submit][value="Search"]'), + ]), ]), ]), + m('.col-md-6', [ + 'Searching sites: ', + vnode.state.loader.children.length == 0 + ? m('span.label.label-xs.label-danger', 'none') + : vnode.state.loader.children.map(function(child) { + return [m('span.label.label-xs', { + className: child.state == child.LOADING ? 'label-warning' : 'label-success', + }, child.sessionKey), ' '] + }), + ' ', + m('a[href="/sessions"]', 'Add/remove sites'), + ]), ]), - m('.col-md-6', [ - 'Searching sites: ', - Object.keys(items).length == 0 - ? m('span.label.label-xs.label-danger', 'none') - : Object.keys(items).sort().map(function(key) { - return [m('span.label.label-xs.label-info', key), ' '] - }), - ' ', - m('a[href="/sessions"]', 'Add/remove sites'), - ]), - ]), - m('.row', Object.keys(items).sort().map(function(key) { - return m('.col-md-3', {key: key, style: { - opacity: items[key].dirty ? 0.5 : 1, - }}, [ - m(window.components.collection_table_narrow, { - key: key, - session: sessions[key], - items: items[key], - }), - ]) - })), + m(CollectionsTable, { + loader: vnode.state.loader, + }), + ], ]) }, } - -function debounce(t, f) { - // Return a new function that waits until t milliseconds have - // passed since it was last called, then calls f with its most - // recent arguments. - var this_was = this - var pending - return function() { - var args = arguments - if (pending) { - console.log("debounce!") - window.clearTimeout(pending) - } - pending = window.setTimeout(function() { - pending = undefined - f.apply(this_was, args) - }, t) - } -}