12519: Simplified code by nesting an additional MergingLoader
[arvados.git] / apps / workbench / app / assets / javascripts / components / collections.js
index 9f7c80489ed2fbc9b730483c361a1425e74df8c5..3d424911890df56742ade47ce883b6f30e35b35f 100644 (file)
@@ -5,7 +5,7 @@
 window.CollectionsTable = {
     maybeLoadMore: function(dom) {
         var loader = this.loader
-        if (loader.done || loader.loading)
+        if (loader.state != loader.READY)
             // Can't start getting more items anyway: no point in
             // checking anything else.
             return
@@ -36,6 +36,7 @@ window.CollectionsTable = {
         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'),
@@ -44,30 +45,36 @@ window.CollectionsTable = {
                 m('th', 'last modified'),
             ])),
             m('tbody', [
-                vnode.attrs.loader.items() && vnode.attrs.loader.items().map(function(item) {
+                loader.items().map(function(item) {
                     return m('tr', [
-                        m('td', m('a.btn.btn-xs.btn-default', {href: item.session.baseURL.replace('://', '://workbench.')+'collections/'+item.uuid}, 'Show')),
+                        m('td', [
+                            item.workbenchBaseURL() &&
+                                m('a.btn.btn-xs.btn-default', {
+                                    title: 'Show '+item.objectType.description,
+                                    href: item.workbenchBaseURL()+'/'+item.objectType.wb_path+'/'+item.uuid,
+                                }, item.objectType.label),
+                        ]),
                         m('td.arvados-uuid', item.uuid),
                         m('td', item.name || '(unnamed)'),
                         m('td', m(LocalizedDateTime, {parse: item.modified_at})),
                     ])
                 }),
             ]),
-            m('tfoot', m('tr', [
-                vnode.attrs.loader.done ? null : m('th[colspan=4]', m('button.btn.btn-xs', {
-                    className: vnode.attrs.loader.loading ? 'btn-default' : 'btn-primary',
+            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: vnode.attrs.loader.loading,
+                    disabled: loader.state == loader.LOADING,
                     onclick: function() {
-                        vnode.attrs.loader.loadMore()
+                        loader.loadMore()
                         return false
                     },
-                }, vnode.attrs.loader.loading ? '(loading)' : 'Load more')),
+                }, loader.state == loader.LOADING ? '(loading)' : 'Load more')),
             ])),
         ])
     },
@@ -88,23 +95,57 @@ window.CollectionsSearch = {
             vnode.state.loader = new MergingLoader({
                 children: Object.keys(sessions).map(function(key) {
                     var session = sessions[key]
-                    return new MultipageLoader({
+                    var workbenchBaseURL = function() {
+                        return vnode.state.sessionDB.workbenchBaseURL(session)
+                    }
+                    var searchable_objects = [
+                        {
+                            wb_path: 'groups',
+                            api_path: 'arvados/v1/groups',
+                            filters: [['group_class', '=', 'project']],
+                            label: 'P',
+                            description: 'Project',
+                        },
+                        {
+                            wb_path: 'collections',
+                            api_path: 'arvados/v1/collections',
+                            filters: [],
+                            label: 'C',
+                            description: 'Collection',
+                        },
+                    ]
+                    return new MergingLoader({
                         sessionKey: key,
-                        loadFunc: function(filters) {
-                            if (q)
-                                filters.push(['any', '@@', q+':*'])
-                            return vnode.state.sessionDB.request(session, 'arvados/v1/collections', {
-                                data: {
-                                    filters: JSON.stringify(filters),
-                                    count: 'none',
+                        // For every session, search for every object type
+                        children: searchable_objects.map(function(obj_type){
+                            return new MultipageLoader({
+                                sessionKey: key,
+                                objectKind: obj_type.label,
+                                loadFunc: function(filters) {
+                                    var tsquery = to_tsquery(q)
+                                    if (tsquery) {
+                                        filters = filters.slice(0)
+                                        filters.push(['any', '@@', tsquery])
+                                    }
+                                    // Apply additional type dependant filters, if any.
+                                    for (var f of obj_type.filters) {
+                                        filters.push(f)
+                                    }
+                                    return vnode.state.sessionDB.request(session, obj_type.api_path, {
+                                        data: {
+                                            filters: JSON.stringify(filters),
+                                            count: 'none',
+                                        },
+                                    }).then(function(resp) {
+                                        resp.items.map(function(item) {
+                                            item.workbenchBaseURL = workbenchBaseURL
+                                            item.objectType = obj_type
+                                        })
+                                        return resp
+                                    })
                                 },
-                            }).then(function(resp) {
-                                resp.items.map(function(item) {
-                                    item.session = session
-                                })
-                                return resp
                             })
-                        },
+                        })
                     })
                 })
             })
@@ -115,14 +156,14 @@ window.CollectionsSearch = {
         return m('form', {
             onsubmit: function() {
                 vnode.state.searchActive(vnode.state.searchEntered())
-                vnode.state.forgetSavedState = true
+                vnode.state.forgetSavedHeight = true
                 return false
             },
         }, [
             m(SaveUIState, {
                 defaultState: '',
                 currentState: vnode.state.searchActive,
-                forgetSavedState: vnode.state.forgetSavedState,
+                forgetSavedHeight: vnode.state.forgetSavedHeight,
                 saveBodyHeight: true,
             }),
             vnode.state.loader && [
@@ -144,7 +185,7 @@ window.CollectionsSearch = {
                             ? m('span.label.label-xs.label-danger', 'none')
                             : vnode.state.loader.children.map(function(child) {
                                 return [m('span.label.label-xs', {
-                                    className: child.items() ? 'label-success' : 'label-warning',
+                                    className: child.state == child.LOADING ? 'label-warning' : 'label-success',
                                 }, child.sessionKey), ' ']
                             }),
                         ' ',