source: main/trunk/greenstone3/web/interfaces/default/js/documentbasket/documentbasket.js@ 33041

Last change on this file since 33041 was 33041, checked in by kjdon, 5 years ago

tomcat 8 strict on unsafe chars. Use the new makeURLComponentSafe for berry baskets and document baskets etc to escape the hhf param ( no idea what this param does..)

  • Property svn:executable set to *
File size: 11.2 KB
Line 
1//used to stored the current items in document basket, including collection name, document id and dodument title
2//in the format of collection:id:[title], it is updated when the page is loaded (reloaded) or the user adds
3// new items in the berry basket (see documentBasketDDPlayer.js). It is used to show the contents of the berry basket.
4
5var show = false;
6var docList = new Array();
7
8var oldHeight=120;
9var oldWidth=140;
10var oldBg;
11
12var dd = new ygDDOnTop('documentbasket');
13dd.setHandleElId('documenthandle');
14
15var dmcheckout = function(){
16
17 if ( document.getElementById('documentbasket') == null ) return;
18
19 var request_type = "GET";
20 var url = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=GetDocuments&o=XML&hhf="+makeURLComponentSafe("[{\"name\":\"Cache-Control\", \"value\":\"no-cache\"}]")+"&c=";
21
22 var responseSuccess = function(o){
23 //alert(o.responseText);
24 var response = o.responseXML;
25 var size = response.getElementsByTagName('size').item(0).getAttribute('value');
26
27 var items = response.getElementsByTagName('item');
28
29 docList = new Array();
30 for (var i=0;i < items.length ; i++ )
31 {
32 // remove document images from the document basket if it already contains this item
33 var itemID = "documentBasketBook" + items[i].getAttribute('collection') + ":" + items[i].getAttribute('name');
34 var item = YAHOO.util.Dom.get(itemID);
35 if (item != null){
36 var parent = item.parentNode;
37 parent.removeChild(item);
38 }
39 docList[docList.length] = items[i];
40 }
41
42 //add document image into the document basket
43 updateDocumentImages();
44
45 // set document images visible
46 // they are set to be invisible to prevent flickering when first loaded (see documentbasket.css)
47
48 var docs = document.getElementsByTagName('img');
49 new YAHOO.util.DDTarget('documentbasket','documentbox');
50
51 for (var j=0; j < docs.length; j++){
52 var doc = docs[j];
53 var parent = doc.parentNode;
54
55 if (doc.id.indexOf("documentBasketBook") != -1){
56 doc.style.visibility = "visible";
57 new documentBasketDDPlayer(doc.id,'documentbox',docList);
58 }
59 }
60 //updateDocumentCount();
61 correctDocumentExpandCollapseButtons();
62 }
63
64 var responseFailure = function(o){
65 alert("CHECKOUT FAILED");
66 }
67
68 var callback = {
69 success: responseSuccess,
70 failure: responseFailure
71 }
72
73 YAHOO.util.Connect.asyncRequest(request_type , url , callback);
74}
75
76function clearBasket()
77{
78 var delurlPath = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=ClearDocuments&c=&o=XML&hhf="+makeURLComponentSafe("[{\"name\":\"Cache-Control\", \"value\":\"no-cache\"}]")+"&s1.c=" + gs.cgiParams.c;
79 var callback =
80 {
81 success:function(){console.log("Successfully removed document");},
82 failed:function(){console.log("Failed to remove document " + docList[i].id + " from the document basket");}
83 }
84 YAHOO.util.Connect.asyncRequest("GET", delurlPath, callback);
85 docList = new Array();
86 updateDocumentImages();
87}
88
89function updateDocumentImages(){
90
91 var docs = YAHOO.util.Dom.get('documentpages');
92 if ( docs == null ) return;
93
94 while(docs.hasChildNodes())
95 {
96 docs.removeChild(docs.firstChild);
97 }
98
99 for (var i = 0; i < docList.length && i < 12; i++ ){
100 var img = document.createElement('img');
101 img.src = gs.imageURLs.pageIcon;
102 docs.appendChild(img);
103 }
104
105 //show the help message if there are no docs
106 if ( docList.length == 0 ) {
107 var docHelpMsg = document.createElement('span');
108 docs.appendChild(docHelpMsg);
109 docHelpMsg.appendChild(document.createTextNode('Drop your document/pages here'));
110 docHelpMsg.setAttribute('id','docHelpMsg');
111 }
112
113 //show the expand buttons
114 correctDocumentExpandCollapseButtons();
115
116}
117
118function correctDocumentExpandCollapseButtons() {
119 var dbecl = YAHOO.util.Dom.get('documentBasketExpandCollapseLinks');
120 if ( dbecl == null ) return;
121 if ( docList.length > 0 ) {
122 YAHOO.util.Dom.get('documentBasketExpandCollapseLinks').style.display='';
123 } else {
124 YAHOO.util.Dom.get('documentBasketExpandCollapseLinks').style.display='none';
125 }
126}
127
128function updateDocumentCount() {
129 var docs = YAHOO.util.Dom.get('documentpages');
130 var documentCount = YAHOO.util.Dom.get('documentCount');
131 if ( docs == null || documentCount == null ) return;
132 if ( docList.length == 0 ) {
133 documentCount.innerHTML = "empty";
134 } else {
135 documentCount.innerHTML = docList.length;
136 }
137}
138
139function documentBoxUpdate(e) {
140//alert( arguments.callee );
141 var target = e.target;
142 if ((target.id.toLowerCase() == 'documentbasket' || target.id.toLowerCase() == 'documentpages') && !show){
143 showDocumentBox();
144 show = true;
145 }else{
146 if (target.id.toLowerCase() == 'hideview' && show){
147 hideDocumentBox();
148 show = false;
149 }
150 }
151
152 if (target.className == 'pick'){
153 addDocument(target);
154 }
155}
156
157function showDocumentBox() {
158//alert( arguments.callee );
159 var documentBasket= YAHOO.util.Dom.get('documentbasket');
160 var documentHandle = YAHOO.util.Dom.get('documenthandle');
161 var documentPages = YAHOO.util.Dom.get('documentpages');
162 var width = 500;
163 var height = 40;
164 var i=0;
165
166 //remove berryImages in the berry basket
167 while (documentPages.hasChildNodes()) {
168 documentPages.removeChild(documentPages.firstChild);
169 }
170 while (documentHandle.hasChildNodes()) {
171 documentHandle.removeChild(documentHandle.firstChild);
172 }
173
174 var div = document.createElement('div');
175 documentPages.appendChild(div);
176 var list = document.createElement('ol');
177 div.appendChild(list);
178
179 list.id = 'doclist';
180
181 //put the document pages in
182 for (i; i < docList.length; i++){
183 var documentItem = docList[i];
184 var documentElement = document.createElement('li');
185 list.appendChild(documentElement);
186 var title = documentItem.getAttribute('title');
187 var root_title = documentItem.getAttribute('root_title');
188 //var id = berryItem.getAttribute('collection')+":"+berryItem.getAttribute('name');
189
190 if (root_title != ""){
191 root_title +=":";
192 }
193
194 title = root_title+title;
195 if (title.length > 50){
196 title = title.substring(0,20)+" ... "+title.substr(title.length-35,35);
197 }
198
199 documentElement.setAttribute("class","documentitem");
200 documentElement.setAttribute("title",title);
201 documentElement.innerHTML = title;
202 height +=40;
203 }
204
205 oldHeight = documentBasket.style.height;
206 oldWidth = documentBasket.style.width;
207 oldBg = documentBasket.style.background;
208
209 documentBasket.style.background ='url("interfaces/default/images/kete2.png") 0 0 repeat';
210 documentBasket.style.cursor = "default";
211 documentBasket.className = "show";
212
213 //put the full view link in
214 var fullView = document.createElement('a');
215 documentPages.appendChild(fullView);
216
217 //Find the collection in the cgi parameters
218 var url = window.location.href;
219 var colstart = url.indexOf("&c=");
220 var collectionName = "";
221 if (colstart != -1)
222 {
223 var colend = url.indexOf("&", (colstart + 1));
224 if (colend == -1)
225 {
226 colend = url.length - 1;
227 }
228 collectionName = url.substring(colstart + 3, colend);
229 }
230
231 fullView.appendChild(document.createTextNode('Full View »'));
232 fullView.setAttribute("href", gs.xsltParams.library_name + "?a=g&sa=documentbasket&c=&s=DisplayDocumentList&rt=r&p.c=" + collectionName);
233 fullView.setAttribute("id","documentpagesFullViewLink");
234
235 //toggle expand/collapse links
236 var expandLink = YAHOO.util.Dom.get('documentBasketExpandLink');
237 var collapseLink = YAHOO.util.Dom.get('documentBasketCollapseLink');
238 if ( expandLink != null && collapseLink != null ) {
239 expandLink.style.display = 'none';
240 collapseLink.style.display = '';
241 }
242
243}
244
245function hideDocumentBox() {
246//alert( arguments.callee );
247 var i = 0;
248 var documentBasket = YAHOO.util.Dom.get('documentbasket');
249 var documentHandle = YAHOO.util.Dom.get('documenthandle');
250 var documentPages = YAHOO.util.Dom.get('documentpages');
251 documentBasket.style.height = oldHeight;
252 documentBasket.style.width = oldWidth;
253 documentBasket.style.background = oldBg;
254 documentBasket.style.cursor = "pointer";
255 documentBasket.className = "hide";
256
257 while (documentPages.hasChildNodes()) {
258 documentPages.removeChild(documentPages.firstChild);
259 }
260
261 while (documentHandle.hasChildNodes()) {
262 documentHandle.removeChild(documentHandle.firstChild);
263 }
264
265 updateDocumentImages();
266 //updateDocumentCount();
267
268 //toggle expand/collapse links
269 var expandLink = YAHOO.util.Dom.get('documentBasketExpandLink');
270 var collapseLink = YAHOO.util.Dom.get('documentBasketCollapseLink');
271 if ( expandLink != null && collapseLink != null ) {
272 expandLink.style.display = '';
273 collapseLink.style.display = 'none';
274 }
275
276}
277
278
279function addDocument(el){
280 var addurl = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=AddDocument&c=&s1.id=2&o=XML&hhf="+makeURLComponentSafe("[{\"name\":\"Cache-Control\", \"value\":\"no-cache\"}]")+"&s1.item=" + el.id;
281
282 var addSuccess = function(o){
283 var result = o.responseXML;
284 var items = result.getElementsByTagName('item');
285
286 if (items.length < 0){
287
288 var item = items[0];
289 var documentbasket = YAHOO.util.Dom.get('documentbasket');
290 var documentPages = YAHOO.util.Dom.get('documentpages');
291 docList[docList.length]= item;
292 var parent =el.parentNode;
293 if (parent == null) return;
294 parent.removeChild(el);
295 var itemID = item.getAttribute('collection')+":"+item.getAttribute('name');
296 //added
297 if (parent !=null && parent.id == "documentberries"){
298
299 var root = YAHOO.util.Dom.get(itemID+":root");
300 var section = YAHOO.util.Dom.get(itemID+":section");
301
302 if(root!=null && root.id.indexOf(itemID) !=-1){
303 parent.removeChild(root);
304 }
305
306 if(section!=null && section.id.indexOf(itemID) !=-1){
307 parent.removeChild(section);
308 }
309
310 }
311
312 if (!YAHOO.util.Dom.get('hideview') && docList.length < 13){
313
314 while (documentpages.hasChildNodes()) {
315 documentpages.removeChild(documentpages.firstChild);
316 }
317
318 for (var i =0; i < docList.length; i++ ){
319 var img = document.createElement('img');
320 img.src = gs.imageURLs.pageIcon;
321 documentpages.appendChild(img);
322 }
323
324 } else {
325
326 if (YAHOO.util.Dom.get('hideview')){
327 var documentBasket = YAHOO.util.Dom.get('documentbasket');
328 var documentPages = YAHOO.util.Dom.get('documentpages');
329 var doclist = YAHOO.util.Dom.get('doclist');
330 var tid = el.id;
331 var documentItem;
332 var documentElement = document.createElement('li');
333
334 for (var i in docList){
335 documentItem = docList[i];
336 var id = documentItem.getAttribute('collection')+":"+documentItem.getAttribute('name');
337
338 if (id == tid){
339 var title = documentItem.getAttribute('title');
340 var root_title = documentItem.getAttribute('root_title');
341 if (root_title != ""){
342 root_title +=":";
343 }
344
345 title = root_title+title;
346 if (title.length > 50){
347 title = title.substring(0,20)+" ... "+title.substr(title.length-35,35);
348 }
349
350 documentElement.appendChild(document.createTextNode(title));
351 documentElement.setAttribute("class","documentitem");
352 doclist.appendChild(documentElement);
353 var heightStr = documentBasket.style.height+"";
354 var height =parseInt(heightStr.substring(0,heightStr.length-2)) +18;
355 documentBasket.style.height = height;
356 documentPages.style.height = height;
357 break;
358 }
359
360 }
361
362 }
363
364 }
365 }
366 }
367
368 var addFailure = function(o){
369 }
370
371 var addcallback = {
372 success:addSuccess,
373 failure:addFailure
374 }
375
376 YAHOO.util.Connect.asyncRequest(request_type , addurl , addcallback);
377}
378
379YAHOO.util.Event.addListener(window, 'load', dmcheckout);
380//YAHOO.util.Event.addListener(window, 'click', documentBoxUpdate);
381
Note: See TracBrowser for help on using the repository browser.