7768 |
07 Feb 20 |
nicklas |
/* $Id $ |
7768 |
07 Feb 20 |
nicklas |
2 |
------------------------------------------------------------------ |
7768 |
07 Feb 20 |
nicklas |
Copyright (C) 2020 Nicklas Nordborg |
7768 |
07 Feb 20 |
nicklas |
4 |
|
7768 |
07 Feb 20 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
7768 |
07 Feb 20 |
nicklas |
Available at http://base.thep.lu.se/ |
7768 |
07 Feb 20 |
nicklas |
7 |
|
7768 |
07 Feb 20 |
nicklas |
BASE is free software; you can redistribute it and/or |
7768 |
07 Feb 20 |
nicklas |
modify it under the terms of the GNU General Public License |
7768 |
07 Feb 20 |
nicklas |
as published by the Free Software Foundation; either version 3 |
7768 |
07 Feb 20 |
nicklas |
of the License, or (at your option) any later version. |
7768 |
07 Feb 20 |
nicklas |
12 |
|
7768 |
07 Feb 20 |
nicklas |
BASE is distributed in the hope that it will be useful, |
7768 |
07 Feb 20 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
7768 |
07 Feb 20 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7768 |
07 Feb 20 |
nicklas |
GNU General Public License for more details. |
7768 |
07 Feb 20 |
nicklas |
17 |
|
7768 |
07 Feb 20 |
nicklas |
You should have received a copy of the GNU General Public License |
7768 |
07 Feb 20 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
7768 |
07 Feb 20 |
nicklas |
20 |
------------------------------------------------------------------ |
7768 |
07 Feb 20 |
nicklas |
21 |
|
7840 |
31 Aug 20 |
nicklas |
JavaScript functions for lazy-loading of information from parent/child |
7840 |
31 Aug 20 |
nicklas |
items in tables. |
7768 |
07 Feb 20 |
nicklas |
24 |
|
7768 |
07 Feb 20 |
nicklas |
@author Nicklas |
7768 |
07 Feb 20 |
nicklas |
@version 3.16 |
7768 |
07 Feb 20 |
nicklas |
27 |
*/ |
7768 |
07 Feb 20 |
nicklas |
'use strict'; |
7768 |
07 Feb 20 |
nicklas |
29 |
|
7768 |
07 Feb 20 |
nicklas |
var Lazy = function() |
7768 |
07 Feb 20 |
nicklas |
31 |
{ |
7768 |
07 Feb 20 |
nicklas |
var lazy = {}; |
7768 |
07 Feb 20 |
nicklas |
var ITEMS_IN_FIRST_BATCH = 3; // To make the page appear more responsive |
7768 |
07 Feb 20 |
nicklas |
var MAX_ITEMS_IN_BATCH = 10; |
7768 |
07 Feb 20 |
nicklas |
35 |
|
7768 |
07 Feb 20 |
nicklas |
var lazyElements; |
7768 |
07 Feb 20 |
nicklas |
var lazyNo = 0; |
7768 |
07 Feb 20 |
nicklas |
var itemType; |
7768 |
07 Feb 20 |
nicklas |
var subContext; |
7768 |
07 Feb 20 |
nicklas |
40 |
|
7840 |
31 Aug 20 |
nicklas |
lazy.initLazyItems = function() |
7768 |
07 Feb 20 |
nicklas |
42 |
{ |
8083 |
20 Oct 22 |
nicklas |
lazyElements = document.querySelectorAll('.lazy-item'); |
7768 |
07 Feb 20 |
nicklas |
if (lazyElements.length > 0) |
7768 |
07 Feb 20 |
nicklas |
45 |
{ |
7768 |
07 Feb 20 |
nicklas |
var list = document.getElementsByClassName('itemlist')[0]; |
7768 |
07 Feb 20 |
nicklas |
itemType = Data.get(list, 'item-type'); |
7768 |
07 Feb 20 |
nicklas |
subContext = Data.get(list, 'subcontext'); |
7768 |
07 Feb 20 |
nicklas |
lazy.loadNextBatch(ITEMS_IN_FIRST_BATCH); |
7768 |
07 Feb 20 |
nicklas |
50 |
} |
7768 |
07 Feb 20 |
nicklas |
51 |
} |
7768 |
07 Feb 20 |
nicklas |
52 |
|
7768 |
07 Feb 20 |
nicklas |
lazy.loadNextBatch = function(itemsInBatch) |
7768 |
07 Feb 20 |
nicklas |
54 |
{ |
7768 |
07 Feb 20 |
nicklas |
var lazyItems = []; |
7778 |
28 Feb 20 |
nicklas |
while (lazyNo < lazyElements.length) |
7768 |
07 Feb 20 |
nicklas |
57 |
{ |
7768 |
07 Feb 20 |
nicklas |
var e = lazyElements[lazyNo]; |
7778 |
28 Feb 20 |
nicklas |
var colIndex = Data.int(e, 'index'); |
7778 |
28 Feb 20 |
nicklas |
if (colIndex == 0) |
7768 |
07 Feb 20 |
nicklas |
61 |
{ |
7778 |
28 Feb 20 |
nicklas |
// Collect 'item-id' from the first column until we have 'itemsInBatch' items |
7778 |
28 Feb 20 |
nicklas |
if (lazyItems.length == itemsInBatch) break; |
7768 |
07 Feb 20 |
nicklas |
lazyItems[lazyItems.length] = Data.get(e, 'item-id'); |
7768 |
07 Feb 20 |
nicklas |
65 |
} |
7778 |
28 Feb 20 |
nicklas |
Doc.removeClass(e, 'not-loaded'); |
7778 |
28 Feb 20 |
nicklas |
Doc.addClass(e, 'loading'); |
7778 |
28 Feb 20 |
nicklas |
lazyNo++; |
7768 |
07 Feb 20 |
nicklas |
69 |
} |
7768 |
07 Feb 20 |
nicklas |
70 |
|
7768 |
07 Feb 20 |
nicklas |
// Submit request to load the items |
7768 |
07 Feb 20 |
nicklas |
if (lazyItems.length > 0) |
7768 |
07 Feb 20 |
nicklas |
73 |
{ |
7768 |
07 Feb 20 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
7768 |
07 Feb 20 |
nicklas |
var url = App.getRoot() + 'common/columns/ajax.jsp?ID='+App.getSessionId(); |
7840 |
31 Aug 20 |
nicklas |
url += '&cmd=GetLazyItemColumns'; |
7768 |
07 Feb 20 |
nicklas |
url += '&itemType='+encodeURIComponent(itemType); |
7768 |
07 Feb 20 |
nicklas |
if (subContext) url += '&subcontext='+encodeURIComponent(subContext); |
7768 |
07 Feb 20 |
nicklas |
url += '&items='+lazyItems.join(','); |
8083 |
20 Oct 22 |
nicklas |
url += '&remaining='+(lazyElements.length-lazyNo); |
7768 |
07 Feb 20 |
nicklas |
request.open("GET", url, true); |
7768 |
07 Feb 20 |
nicklas |
request.send(null); |
7768 |
07 Feb 20 |
nicklas |
Ajax.setReadyStateHandler(request, lazy.lazyInfoLoaded, lazy.lazyInfoLoaded); |
7768 |
07 Feb 20 |
nicklas |
84 |
} |
7768 |
07 Feb 20 |
nicklas |
85 |
} |
7768 |
07 Feb 20 |
nicklas |
86 |
|
7768 |
07 Feb 20 |
nicklas |
lazy.lazyInfoLoaded = function(request) |
7768 |
07 Feb 20 |
nicklas |
88 |
{ |
7768 |
07 Feb 20 |
nicklas |
var response; |
7768 |
07 Feb 20 |
nicklas |
try |
7768 |
07 Feb 20 |
nicklas |
91 |
{ |
7768 |
07 Feb 20 |
nicklas |
response = JSON.parse(request.responseText); |
7768 |
07 Feb 20 |
nicklas |
if (response.status != 'ok') |
7768 |
07 Feb 20 |
nicklas |
94 |
{ |
7768 |
07 Feb 20 |
nicklas |
App.debug(request.responseText); |
7768 |
07 Feb 20 |
nicklas |
return; |
7768 |
07 Feb 20 |
nicklas |
97 |
} |
7768 |
07 Feb 20 |
nicklas |
98 |
} |
7768 |
07 Feb 20 |
nicklas |
catch (e) |
7768 |
07 Feb 20 |
nicklas |
100 |
{ |
7768 |
07 Feb 20 |
nicklas |
App.debug(e); |
7768 |
07 Feb 20 |
nicklas |
App.debug(request.responseText); |
7768 |
07 Feb 20 |
nicklas |
return; |
7768 |
07 Feb 20 |
nicklas |
104 |
} |
7768 |
07 Feb 20 |
nicklas |
105 |
|
7768 |
07 Feb 20 |
nicklas |
// Simply inject the returned HTML in the expected place |
7768 |
07 Feb 20 |
nicklas |
for (var i = 0; i < response.items.length; i++) |
7768 |
07 Feb 20 |
nicklas |
108 |
{ |
7768 |
07 Feb 20 |
nicklas |
var item = response.items[i]; |
7768 |
07 Feb 20 |
nicklas |
var itemId = item.id; |
7768 |
07 Feb 20 |
nicklas |
for (var col = 0; col < item.data.length; col++) |
7768 |
07 Feb 20 |
nicklas |
112 |
{ |
8083 |
20 Oct 22 |
nicklas |
var e = Doc.element('lazy-item-'+col+'-'+itemId).parentNode; |
7768 |
07 Feb 20 |
nicklas |
if (e) |
7768 |
07 Feb 20 |
nicklas |
115 |
{ |
7768 |
07 Feb 20 |
nicklas |
e.innerHTML = item.data[col]; |
7769 |
07 Feb 20 |
nicklas |
Doc.autoInitElements(e); |
7768 |
07 Feb 20 |
nicklas |
118 |
} |
7768 |
07 Feb 20 |
nicklas |
119 |
} |
7768 |
07 Feb 20 |
nicklas |
120 |
} |
7768 |
07 Feb 20 |
nicklas |
121 |
|
7768 |
07 Feb 20 |
nicklas |
lazy.loadNextBatch(MAX_ITEMS_IN_BATCH); |
7768 |
07 Feb 20 |
nicklas |
123 |
} |
7768 |
07 Feb 20 |
nicklas |
124 |
|
7768 |
07 Feb 20 |
nicklas |
return lazy; |
7768 |
07 Feb 20 |
nicklas |
126 |
}(); |
7768 |
07 Feb 20 |
nicklas |
127 |
|
7982 |
14 Jun 21 |
nicklas |
Doc.addFinalizer(Lazy.initLazyItems); |
7768 |
07 Feb 20 |
nicklas |
129 |
|