added 'dbg' url param to turn on debugging, in particular this enables request debugging
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / cn_browse.js
1 var cnBrowseCurrent;
2 var cnBrowseTopCn;
3 var cnBrowseTopId;
4 var cnBrowseBottomCn;
5 var cnBrowseBottomId;
6 var cnBrowseDepth;
7 var cnBrowseCache = {};
8 var cnBrowseShowNext = false;
9 var cnBrowseShowPrev = false;
10 var MAX_CN = 9;
11
12 function cnBrowseGo(cn, depth) { 
13         if(depth == null) depth = getDepth();
14         cnBrowseDepth = depth;
15         cnBrowseCurrent = cn;
16         var req = new Request( FETCH_CNBROWSE_TARGET, 
17                 'org_unit', getLocation(), 
18                 'depth', cnBrowseDepth, 
19                 'label', cnBrowseCurrent,
20                 'page_size', MAX_CN );
21         req.callback( cnBrowseDraw );
22         req.send();
23 }
24
25 function cnBrowseNext() {
26         cnBrowseShowNext = true;
27         if( cnBrowseCache.next )  /* if we have it, show it */
28                 cnBrowseClearNext(cnBrowseCache.next);
29 }
30
31 function cnBrowsePrev() {
32         cnBrowseShowPrev = true;
33         if( cnBrowseCache.prev ) 
34                 cnBrowseClearPrev(cnBrowseCache.prev);
35 }
36
37 function cnBrowseGrabNext() {
38         var req = new Request( FETCH_CNBROWSE_NEXT, 
39                 'org_unit', getLocation(), 
40                 'depth', cnBrowseDepth,
41                 'label', cnBrowseBottomCn, 
42                 'boundry_id', cnBrowseBottomId,
43                 'page_size', MAX_CN );
44         req.callback( cnBrowseCacheMe );
45         req.request.next = true;
46         req.send();
47 }
48
49
50 function cnBrowseGrabPrev() {
51         var req = new Request( FETCH_CNBROWSE_PREV,
52                 'org_unit', getLocation(), 
53                 'depth', cnBrowseDepth,
54                 'label', cnBrowseTopCn, 
55                 'boundry_id', cnBrowseTopId,
56                 'page_size', MAX_CN );
57         req.callback( cnBrowseCacheMe );
58         req.request.prev = true;
59         req.send();
60 }
61
62 function cnBrowseClearNext(list) {
63         cnBrowseCache.next = null; 
64         cnBrowseShowNext = false;
65         _cnBrowseDraw(list);
66 }
67
68 function cnBrowseClearPrev(list) {
69         cnBrowseCache.prev = null; 
70         cnBrowseShowPrev = false;
71         _cnBrowseDraw(list);
72 }
73
74 /* cache next and previous calls unless they are 
75 needed immediately */
76 function cnBrowseCacheMe(r) {
77         var list = r.getResultObject();
78         if( r.next ) {
79                 cnBrowseCache.next = list;
80                 if( cnBrowseShowNext ) {
81                         cnBrowseClearNext(list);
82                 } 
83
84         } else if( r.prev ) {
85                 cnBrowseCache.prev = list;
86                 if( cnBrowseShowPrev )  {
87                         cnBrowseClearPrev(list);
88                 } 
89         }
90 }
91
92
93 function cnBrowseDraw( r ) {
94         var list = r.getResultObject();
95         _cnBrowseDraw(list);
96 }
97
98
99 var cnTbody;
100 var cnRowT;
101 var cnTdT;
102 function _cnBrowseDraw( list ) {
103
104         if(!cnTbody) {
105                 cnTbody = $('cn_tbody');
106                 cnRowT = $('cn_browse_row');
107                 cnTdT = cnRowT.removeChild($('cn_browse_td'));
108                 cnTbody.removeChild(cnRowT);
109         }
110         removeChildren(cnTbody);
111
112         var counter = 1;
113         var currentRow = cnRowT.cloneNode(true);
114         cnTbody.appendChild(currentRow);
115
116         for( var idx in list ) {
117                 
118
119                 var currentTd = cnTdT.cloneNode(true);
120                 currentRow.appendChild(currentTd);
121
122                 var td = cnTdT.cloneNode(true);
123                 var label       = list[idx][0];
124                 var lib         = list[idx][1];
125                 var record      = list[idx][2];
126                 var id          = list[idx][3];
127
128                 /*
129                 if( label != cnBrowseCurrent ) {
130                         removeCSSClass( td, 'cn_browse_home_cn' );
131                 }
132                 */
133
134                 if( idx == 0 ) { cnBrowseTopCn = label; cnBrowseTopId = id; } 
135                 cnBrowseBottomCn = label;
136                 cnBrowseBottomId = id;
137
138                 var cn_td                       = $n(currentTd, 'cn_browse_cn');
139                 var lib_td                      = $n(currentTd, 'cn_browse_lib');
140                 var title_td            = $n(currentTd, 'cn_browse_title');
141                 var author_td           = $n(currentTd, 'cn_browse_author');
142                 var pic_td                      = $n(currentTd, 'cn_browse_pic');
143
144                 cn_td.appendChild(text(label));
145                 lib_td.appendChild(text(findOrgUnit(lib).name()));
146
147                 var req = new Request( FETCH_RMODS, record );
148                 req.request.title_td            = title_td;
149                 req.request.author_td   = author_td;
150                 req.request.pic_td              = pic_td;
151                 req.callback( cnBrowseDrawTitle );
152                 req.send();
153
154         
155                 if( counter++ % 3 == 0 ) {
156                         counter = 1;
157                         currentRow = cnRowT.cloneNode(true);
158                         cnTbody.appendChild(currentRow);
159                 }
160         }
161         cnBrowseGrabNext();
162         cnBrowseGrabPrev();
163 }
164
165 function cnBrowseDrawTitle(r) {
166         var mods = r.getResultObject();
167         buildTitleDetailLink(mods, r.title_td); 
168         buildSearchLink(STYPE_AUTHOR, mods.author(), r.author_td);
169         r.pic_td.setAttribute("src", buildISBNSrc(cleanISBN(mods.isbn())));
170
171         var args = {};
172         args.page = RDETAIL;
173         args[PARAM_OFFSET] = 0;
174         args[PARAM_RID] = mods.doc_id();
175         args[PARAM_MRID] = 0;
176         r.pic_td.parentNode.setAttribute("href", buildOPACLink(args));
177 }
178