"REPOSITORY/MDL-13766, 1. support help option 2. make breadcrumb clickable"
[moodle.git] / repository / javascript.php
1 <?php // $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 //                                                                       //
4 //       Don't modify this file unless you know how it works             //
5 //                                                                       //
6 ///////////////////////////////////////////////////////////////////////////
8 require_once(dirname(dirname(__FILE__)).'/config.php');
9 $yui     = optional_param('yui', 0, PARAM_RAW);              // page or path
10 if (!empty($yui)) {
11     repository_get_yui();
12 }
13 function repository_get_yui() {
14     global $CFG;
16     $lifetime = '86400';
18     @header('Content-type: text/javascript'); 
19     @header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
20     @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .'GMT');
21     @header('Cache-control: max-age='.$lifetime);
22     @header('Pragma: ');
24     $jslist = array(
25         'yahoo-dom-event/yahoo-dom-event.js',
26         'element/element-beta-min.js',
27         'treeview/treeview-min.js',
28         'dragdrop/dragdrop-min.js',
29         'container/container-min.js',
30         'resize/resize-min.js',
31         'layout/layout-min.js',
32         'connection/connection-min.js',
33         'json/json-min.js',
34         'button/button-min.js',
35         'selector/selector-beta-min.js'
36         );
37     foreach ($jslist as $js) {
38         echo "/* Included from lib/yui/$js */\n";
39         readfile($CFG->dirroot.'/lib/yui/'.$js);
40         echo "\n\n";
41     }
42     exit();
43 }
44 /**
45  * Return javascript to create file picker to browse repositories
46  * @global object $CFG
47  * @global object $USER
48  * @param object $context the context
49  * @return array
50  */
51 function repository_get_client($context, $accepted_filetypes = '*', $returnvalue = '*') {
52     global $CFG, $USER;
53     $suffix = uniqid();
54     $sesskey = sesskey();
55     // language string
56     $stradd       = get_string('add', 'repository');
57     $strback      = get_string('back', 'repository');
58     $strcancel    = get_string('cancel');
59     $strclose     = get_string('close', 'repository');
60     $strccache    = get_string('cleancache', 'repository');
61     $strcopying   = get_string('copying', 'repository');
62     $strdownbtn   = get_string('getfile', 'repository');
63     $strdownload  = get_string('downloadsucc', 'repository');
64     $strdate      = get_string('date', 'repository').': ';
65     $strerror     = get_string('error', 'repository');
66     $strfilenotnull = get_string('filenotnull', 'repository');
67     $strfederatedsearch = get_string('federatedsearch', 'repository');
68     $strhelp      = get_string('help');
69     $strrefresh   = get_string('refresh', 'repository');
70     $strinvalidjson = get_string('invalidjson', 'repository');
71     $strlistview  = get_string('listview', 'repository');
72     $strlogin     = get_string('login', 'repository');
73     $strlogout    = get_string('logout', 'repository');
74     $strloading   = get_string('loading', 'repository');
75     $strthumbview = get_string('thumbview', 'repository');
76     $strtitle     = get_string('title', 'repository');
77     $strnoresult  = get_string('noresult', 'repository');
78     $strmgr       = get_string('manageurl', 'repository');
79     $strnoenter   = get_string('noenter', 'repository');
80     $strsave      = get_string('save', 'repository');
81     $strsaveas    = get_string('saveas', 'repository').': ';
82     $strsaved     = get_string('saved', 'repository');
83     $strsaving    = get_string('saving', 'repository');
84     $strsize      = get_string('size', 'repository').': ';
85     $strsync      = get_string('sync', 'repository');
86     $strsearch    = get_string('search', 'repository');
87     $strsearching = get_string('searching', 'repository');
88     $strsubmit    = get_string('submit', 'repository');
89     $strpreview   = get_string('preview', 'repository');
90     $strpopup     = get_string('popup', 'repository');
91     $strupload    = get_string('upload', 'repository').'...';
92     $struploading = get_string('uploading', 'repository');
93     $css = '';
94     if (!isset($CFG->repo_yui_loaded)) {
95         $css .= <<<EOD
96 <style type="text/css">
97 @import "$CFG->httpswwwroot/lib/yui/resize/assets/skins/sam/resize.css";
98 @import "$CFG->httpswwwroot/lib/yui/container/assets/skins/sam/container.css";
99 @import "$CFG->httpswwwroot/lib/yui/layout/assets/skins/sam/layout.css";
100 @import "$CFG->httpswwwroot/lib/yui/button/assets/skins/sam/button.css";
101 @import "$CFG->httpswwwroot/lib/yui/assets/skins/sam/treeview.css";
102 </style>
103 <style type="text/css">
104 .file-picker{font-size:12px;}
105 .file-picker strong{background:#FFFFCC}
106 .file-picker a{color: #336699}
107 .file-picker a:hover{background:#003366;color:white}
108 .fp-panel{padding:0;margin:0; text-align:left;}
109 .fp-login-form{text-align:center}
110 .fp-searchbar{float:right}
111 .fp-viewbar{width:300px;float:left}
112 .fp-toolbar{padding: .8em;background: #FFFFCC;color:white;text-align:center}
113 .fp-toolbar a{padding: 0 .5em}
114 .fp-list{list-style-type:none;padding:0;float:left;width:100%;margin:0;}
115 .fp-list li{border-bottom:1px dotted gray;margin-bottom: 1em;}
116 .fp-repo-name{display:block;padding: .5em;margin-bottom: .5em}
117 .fp-pathbar{margin: .4em;border-bottom: 1px dotted gray;}
118 .fp-pathbar a{padding: .4em;}
119 .fp-rename-form{text-align:center}
120 .fp-rename-form p{margin: 1em;}
121 .fp-upload-form{margin: 2em 0;text-align:center}
122 .fp-upload-btn a{cursor: default;background: white;border:1px solid gray;color:black;padding: .5em}
123 .fp-upload-btn a:hover {background: grey;color:white}
124 .fp-paging{margin:1em .5em; clear:both;text-align:center;line-height: 2.5em;}
125 .fp-paging a{padding: .5em;border: 1px solid #CCC}
126 .fp-paging a.cur_page{border: 1px solid blue}
127 .fp-popup{text-align:center}
128 .fp-grid{float:left;text-align:center;}
129 .fp-grid div{overflow: hidden}
130 .fp-grid p{margin:0;padding:0;background: #FFFFCC}
131 .fp-grid .label{height:48px;text-align:center}
132 .fp-grid span{color:gray}
133 </style>
135 <!--[if IE 6]>
136     <style type="text/css">
137     /* Fix for IE6 */
138     .yui-skin-sam .yui-panel .hd{
140     }
141     </style>
142 <![endif]-->
143 EOD;
145         $js = '<script type="text/javascript" src="'.$CFG->httpswwwroot.'/repository/javascript.php?yui=1"></script>';
146         $CFG->repo_yui_loaded = true;
147     } else {
148         $js = '';
149     }
151     $js .= <<<EOD
152 <script type="text/javascript">
153 //<![CDATA[
154 //
155 var mdl_in_array = function(el, arr) {
156     for(var i = 0, l = arr.length; i < l; i++) {
157         if(arr[i] == el) {
158             return true;
159         }
160     }
161     return false;
164 var active_instance = null;
165 function repository_callback(id) {
166     active_instance.req(id, '');
168 var repository_client_$suffix = (function() {
169 // private static field
170 var dver = '1.0';
171 // private static methods
172 function version() {
173     alert(dver);
175 function _client() {
176 // public varible
177 this.name = 'repository_client_$suffix';
178 // private varible
179 var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, layout = null, resize = null;
180 var IE_QUIRKS = (YAHOO.env.ua.ie && document.compatMode == "BackCompat");
181 var IE_SYNC = (YAHOO.env.ua.ie == 6 || (YAHOO.env.ua.ie == 7 && IE_QUIRKS));
182 var PANEL_BODY_PADDING = (10*2);
183 var btn_list = {label: '$strlistview', value: 'l', checked: true, onclick: {fn: _client.viewlist}};
184 var btn_thumb = {label: '$strthumbview', value: 't', onclick: {fn: _client.viewthumb}};
185 var repo_list = null;
186 var resize = null;
187 var filepicker = new YAHOO.widget.Panel('file-picker-$suffix', {
188     draggable: true,
189     close: true,
190     underlay: 'none',
191     zindex: 666666,
192     xy: [50, Dom.getDocumentScrollTop()+20]
193 });
194 // construct code section
196     filepicker.setHeader('$strtitle');
197     filepicker.setBody('<div id="layout-$suffix"></div>');
198     filepicker.beforeRenderEvent.subscribe(function() {
199         Event.onAvailable('layout-$suffix', function() {
200             layout = new YAHOO.widget.Layout('layout-$suffix', {
201                 height: 480, width: 700,
202                 units: [
203                 {position: 'top', height: 32, resize: false,
204                 body:'<div class="yui-buttongroup fp-viewbar" id="repo-viewbar-$suffix"></div><div class="fp-searchbar" id="search-div-$suffix"></div>', gutter: '2'},
205                 {position: 'left', width: 200, resize: true, scroll:true,
206                 body:'<ul class="fp-list" id="repo-list-$suffix"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
207                 {position: 'center', body: '<div class="fp-panel" id="panel-$suffix"></div>',
208                 scroll: true, gutter: '0 2 0 0' }
209                 ]
210             });
211             layout.render();
212         });
213     });
214     resize = new YAHOO.util.Resize('file-picker-$suffix', {
215         handles: ['br'],
216         autoRatio: true,
217         status: true,
218         minWidth: 680,
219         minHeight: 400
220     });
221     if(YAHOO.env.ua.ie == 6){
222         var fp_title = document.getElementById('file-picker-$suffix');
223         fp_title.style.width = '680px';
224     }
225     resize.on('resize', function(args) {
226         var panelHeight = args.height;
227         var headerHeight = this.header.offsetHeight; // Content + Padding + Border
228         var bodyHeight = (panelHeight - headerHeight);
229         var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING;
230         Dom.setStyle(this.body, 'height', bodyContentHeight + 'px');
231         if(YAHOO.env.ua.ie == 6){
232             var fp_title = document.getElementById('file-picker-$suffix');
233             fp_title.style.width = args.width;
234         }
235         if (IE_SYNC) {
236             this.sizeUnderlay();
237             this.syncIframe();
238         }
239         layout.set('height', bodyContentHeight);
240         layout.set('width', (args.width - PANEL_BODY_PADDING));
241         layout.resize();
243     }, filepicker, true);
244     filepicker.update_instances = function(){
245         _client.print_instances();
246     }
247     _client.viewbar = new YAHOO.widget.ButtonGroup({
248         id: 'btngroup-$suffix',
249         name: 'buttons',
250         disabled: true,
251         container: 'repo-viewbar-$suffix'
252     });
254 // public method
255 this.show = function() {
256     filepicker.update_instances();
257     filepicker.show();
259 this.hide = function() {
260     filepicker.hide();
262 this.create_picker = function() {
263     // display UI
264     filepicker.render();
265     _client.viewbar.addButtons([btn_thumb, btn_list]);
266     // init repository list
267     repo_list = new YAHOO.util.Element('repo-list-$suffix');
268     repo_list.on('contentReady', function(e) {
269         var searchbar = new YAHOO.util.Element('search-div-$suffix');
270         searchbar.get('element').innerHTML = '<input id="search-input-$suffix" /><button id="search-btn-$suffix">$strfederatedsearch</button>';
271         var btn_search = new YAHOO.util.Element('search-btn-$suffix');
272         var input_keyword = new YAHOO.util.Element('search-input-$suffix');
273         btn_search.fnSearch = function(e) {
274             var el = new YAHOO.util.Element('search-input-$suffix')
275             var keyword = el.get('value');
276             var params = [];
277             params['s'] = keyword;
278             params['env']=_client.env;
279             params['action']='gsearch';
280             params['accepted_types'] = _client.accepted_types;
281             params['sesskey']='$sesskey';
282             params['ctx_id']=$context->id;
283             _client.loading('load');
284             var trans = YAHOO.util.Connect.asyncRequest('POST',
285                 '$CFG->httpswwwroot/repository/ws.php?action=gsearch', this.global_search_cb, _client.postdata(params));
286         }
287         btn_search.global_search_cb={
288             success: function(o) {
289                 var panel = new YAHOO.util.Element('panel-$suffix');
290                 if(!o.responseText) {
291                     panel.get('element').innerHTML = '$strnoresult';
292                     return;
293                 }
294                 try {
295                     var data = YAHOO.lang.JSON.parse(o.responseText);
296                 } catch(e) {
297                     alert('$strinvalidjson - |global_search_cb| -'+_client.stripHTML(o.responseText));
298                     return;
299                 }
300                 _client.ds={};
301                 if(!data.list || data.list.length<1){
302                     panel.get('element').innerHTML = '$strnoresult';
303                     return;
304                 }
305                 _client.ds.list = data.list;
306                 if(_client.viewmode) {
307                     _client.viewlist();
308                 } else {
309                     _client.viewthumb();
310                 }
311                 var el = new YAHOO.util.Element('search-input-$suffix')
312                 el.set('value', '');
313             }
314         }
315         btn_search.on('contentReady', function() {
316             btn_search.on('click', this.fnSearch, this.input_keyword);
317         });
318         input_keyword.on('contentReady', function() {
319             var scope = document.getElementById('search-input-$suffix');
320             var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){this.fnSearch()},scope:btn_search, correctScope: true});
321             k1.enable();
322         });
323         _client.print_instances();
324     });
328 // public static varible
329 _client.repos = [];
330 _client.repositoryid = 0;
331 // _client.ds save all data received from server side
332 _client.ds = null;
333 _client.viewmode = 0;
334 _client.viewbar = null;
335 _client.print_instances = function() {
336     var container = new YAHOO.util.Element('repo-list-$suffix');
337     container.set('innerHTML', '');
338     for(var i in _client.repos) {
339         var repo = _client.repos[i];
340         var support = false;
341         if(repository_client_$suffix.env=='editor' && _client.accepted_types != '*'){
342             if(repo.supported_types!='*'){
343                 for (var j in repo.supported_types){
344                     if(mdl_in_array(repo.supported_types[j], _client.accepted_types)){
345                         support = true;
346                     }
347                 }
348             }
349         }else{
350             support = true;
351         }
352         if(repo.supported_types == '*' || support){
353             var li = document.createElement('li');
354             li.id = 'repo-$suffix-'+repo.id;
355             var icon = document.createElement('img');
356             icon.src = repo.icon;
357             icon.width = '16';
358             icon.height = '16';
359             var link = document.createElement('a');
360             link.href = '###';
361             link.id = 'repo-call-$suffix-'+repo.id;
362             link.appendChild(icon);
363             link.className = 'fp-repo-name';
364             link.onclick = function() {
365                 // highlight active repo
366                 for(var cc in _client.repos){
367                     var tmp_id = 'repo-call-$suffix-'+ _client.repos[cc].id;
368                     var el = document.getElementById(tmp_id);
369                     if(el){
370                         el.style.background = 'transparent';
371                     }
372                 }
373                 this.style.background = '#CCC';
374                 var re = /repo-call-$suffix-(\d+)/i;
375                 var id = this.id.match(re);
376                 repository_client_$suffix.req(id[1], '');
377             }
378             link.innerHTML += ' '+repo.name;
379             li.appendChild(link);
380             container.appendChild(li);
381             repo = null;
382         }
383     }
385 _client.stripHTML = function(str){
386     var re= /<\S[^><]*>/g
387     var ret = str.replace(re, "")
388     return ret;
391 // public static mehtod
392 _client.postdata = function(obj) {
393     var str = '';
394     for(k in obj) {
395         if(obj[k] instanceof Array) {
396             for(i in obj[k]) {
397                 str += (encodeURIComponent(k) +'[]='+encodeURIComponent(obj[k][i]));
398                 str += '&';
399             }
400         } else {
401             str += encodeURIComponent(k) +'='+encodeURIComponent(obj[k]);
402             str += '&';
403         }
404     }
405     return str;
407 _client.loading = function(type, name) {
408     var panel = new YAHOO.util.Element('panel-$suffix');
409     panel.get('element').innerHTML = '';
410     var content = document.createElement('div');
411     content.style.textAlign='center';
412     var para = document.createElement('P');
413     var img = document.createElement('IMG');
414     if(type=='load') {
415         img.src = '$CFG->pixpath/i/loading.gif';
416         para.innerHTML = '$strloading';
417     }else{
418         img.src = '$CFG->pixpath/i/progressbar.gif';
419         para.innerHTML = '$strcopying <strong>'+name+'</strong>';
420     }
421     content.appendChild(para);
422     content.appendChild(img);
423     //content.innerHTML = '';
424     panel.get('element').appendChild(content);
426 _client.rename = function(oldname, url, icon, repo_id) {
427     var thumbnail = document.getElementById('fp-grid-panel-$suffix');
428     if(thumbnail){
429         thumbnail.style.display = 'none';
430     }
431     var header = document.getElementById('fp-header-$suffix');
432     header.style.display = 'none';
433     var footer = document.getElementById('fp-footer-$suffix');
434     footer.style.display = 'none';
435     var panel = new YAHOO.util.Element('panel-$suffix');
436     var html = '<div class="fp-rename-form">';
437     _client.repositoryid=repo_id;
438     html += '<p><img src="'+icon+'" /></p>';
439     html += '<p><label for="newname-$suffix">$strsaveas</label>';
440     html += '<input type="text" id="newname-$suffix" value="'+oldname+'" /></p>';
441     /**
442     html += '<p><label for="syncfile-$suffix">$strsync</label> ';
443     html += '<input type="checkbox" id="syncfile-$suffix" /></p>';
444     */
445     html += '<p><input type="hidden" id="fileurl-$suffix" value="'+url+'" />';
446     html += '<input type="button" onclick="repository_client_$suffix.download()" value="$strdownbtn" />';
447     html += '<input type="button" onclick="repository_client_$suffix.viewfiles()" value="$strcancel" /></p>';
448     html += '</div>';
449     panel.get('element').innerHTML += html;
450     var tree = document.getElementById('treediv-$suffix');
451     if(tree){
452         tree.style.display = 'none';
453     }
455 _client.popup = function(url) {
456     active_instance = repository_client_$suffix;
457     _client.win = window.open(url,'repo_auth', 'location=0,status=0,scrollbars=0,width=500,height=300');
458     return false;
460 _client.print_login = function() {
461     var panel = new YAHOO.util.Element('panel-$suffix');
462     var data = _client.ds.login;
463     var str = '<div class="fp-login-form">';
464     var has_pop = false;
465     for(var k in data) {
466         if(data[k].type=='popup') {
467             str += '<p class="fp-popup">$strpopup</p>';
468             str += '<p class="fp-popup"><button onclick="repository_client_$suffix.popup(\''+data[k].url+'\')">$strlogin</button>';
469             str += '</p>';
470             has_pop = true;
471         }else if(data[k].type=='textarea') {
472             str += '<p><textarea id="'+data[k].id+'" name="'+data[k].name+'"></textarea></p>';
473         }else{
474             str += '<p>';
475             var label_id = '';
476             var field_id = '';
477             var field_value = '';
478             if(data[k].id) {
479                 label_id = ' for="'+data[k].id+'"';
480                 field_id = ' id="'+data[k].id+'"';
481             }
482             if (data[k].label) {
483                 str += '<label'+label_id+'>'+data[k].label+'</label>&nbsp;';
484             }
485             if(data[k].value) {
486                 field_value = ' value="'+data[k].value+'"';
487             }
488             str += '<input type="'+data[k].type+'"'+' name="'+data[k].name+'"'+field_id+field_value+' />';
489             str += '</p>';
490         }
491     }
492     if(!has_pop) {
493         str += '<p><input type="button" onclick="repository_client_$suffix.login()" value="$strsubmit" /></p>';
494     }
495     str += '</div>';
496     panel.get('element').innerHTML = str;
499 _client.viewfiles = function() {
500     if(_client.viewmode) {
501         _client.viewlist();
502     } else {
503         _client.viewthumb();
504     }
506 _client.print_msg = function(msg) {
507     _client.print_header();
508     var panel = new YAHOO.util.Element('panel-$suffix');
509     panel.get('element').innerHTML += msg;
510     _client.print_footer();
512 _client.print_header = function() {
513     var panel = new YAHOO.util.Element('panel-$suffix');
514     var str = '<div id="fp-header-$suffix">';
515     str += '<div class="fp-toolbar" id="repo-tb-$suffix"></div>';
516     if(_client.ds.pages < 8){
517         str += _client.makepage('header');
518     }
519     str += '</div>';
520     panel.set('innerHTML', str);
521     _client.makepath();
523 _client.print_footer = function() {
524     var panel = document.getElementById('panel-$suffix');
525     var footer = document.createElement('DIV');
526     footer.id = 'fp-footer-$suffix';
527     footer.innerHTML += _client.uploadcontrol();
528     footer.innerHTML += _client.makepage('footer');
529     panel.appendChild(footer);
530     // add repository manage buttons here
531     var oDiv = document.getElementById('repo-tb-$suffix');
532     if(!_client.ds.nosearch) {
533         var search = document.createElement('A');
534         search.href = '###';
535         search.innerHTML = '<img src="$CFG->pixpath/a/search.png" /> $strsearch';
536         oDiv.appendChild(search);
537         search.onclick = function() {
538             repository_client_$suffix.search_form(repository_client_$suffix.repositoryid);
539         }
540     }
541     // weather we use cache for this instance, this button will reload listing anyway
542     if(!_client.ds.norefresh) {
543         var ccache = document.createElement('A');
544         ccache.href = '###';
545         ccache.innerHTML = '<img src="$CFG->pixpath/a/refresh.png" /> $strrefresh';
546         oDiv.appendChild(ccache);
547         ccache.onclick = function() {
548             var params = [];
549             params['env']=_client.env;
550             params['sesskey']='$sesskey';
551             params['ctx_id']=$context->id;
552             params['repo_id']=repository_client_$suffix.repositoryid;
553             _client.loading('load');
554             var trans = YAHOO.util.Connect.asyncRequest('POST',
555                     '$CFG->httpswwwroot/repository/ws.php?action=ccache', repository_client_$suffix.req_cb, _client.postdata(params));
556         }
557     }
558     if(_client.ds.manage) {
559         var mgr = document.createElement('A');
560         mgr.innerHTML = '<img src="$CFG->pixpath/a/setting.png" /> $strmgr';
561         mgr.href = _client.ds.manage;
562         mgr.target = "_blank";
563         oDiv.appendChild(mgr);
564     }
565     if(!_client.ds.nologin) {
566         var logout = document.createElement('A');
567         logout.href = '###';
568         logout.innerHTML = '<img src="$CFG->pixpath/a/logout.png" /> $strlogout';
569         oDiv.appendChild(logout);
570         logout.onclick = function() {
571             repository_client_$suffix.logout(repository_client_$suffix.repositoryid, 1);
572         }
573     }
574     if(_client.ds.help) {
575         var help = document.createElement('A');
576         help.href = _client.ds.help;
577         help.target = "_blank";
578         help.innerHTML = '<img src="$CFG->pixpath/a/help.png" /> $strhelp';
579         oDiv.appendChild(help);
580     }
582 _client.viewthumb = function(ds) {
583     _client.viewmode = 0;
584     _client.viewbar.check(0);
585     var container = document.getElementById('panel-$suffix');
586     var panel = document.createElement('DIV');
587     panel.id = 'fp-grid-panel-$suffix';
588     var list = null;
589     if(arguments.length == 1) {
590         list = ds;
591     } else {
592         // from button
593         list = _client.ds.list;
594     }
595     _client.print_header();
596     var count = 0;
597     for(k in list) {
598         // the container
599         var el = document.createElement('div');
600         el.className='fp-grid';
601         // the file name
602         var title = document.createElement('div');
603         title.id = 'grid-title-'+String(count);
604         title.className = 'label';
605         if (list[k].shorttitle) {
606             list[k].title = list[k].shorttitle;
607         }
608         title.innerHTML += '<a href="###"><span>'+list[k].title+"</span></a>";
609         if(list[k].thumbnail_width){
610             el.style.width = list[k].thumbnail_width+'px';
611             title.style.width = (list[k].thumbnail_width-10)+'px';
612         } else {
613             el.style.width = title.style.width = '80px';
614         }
615         var frame = document.createElement('DIV');
616         frame.style.textAlign='center';
617         if(list[k].thumbnail_height){
618             frame.style.height = list[k].thumbnail_height+'px';
619         }
620         var img = document.createElement('img');
621         img.src = list[k].thumbnail;
622         if(list[k].thumbnail_alt) {
623             img.alt = list[k].thumbnail_alt;
624         }
625         if(list[k].thumbnail_title) {
626             img.title = list[k].thumbnail_title;
627         }
628         var link = document.createElement('A');
629         link.href='###';
630         link.id = 'img-id-'+String(count);
631         if(list[k].url) {
632             el.innerHTML += '<p><a target="_blank" href="'+list[k].url+'">$strpreview</a></p>';
633         }
634         link.appendChild(img);
635         frame.appendChild(link);
636         el.appendChild(frame);
637         el.appendChild(title);
638         
639         panel.appendChild(el);
640         if(list[k].children) {
641             var folder = new YAHOO.util.Element(link.id);
642             folder.ds = list[k].children;
643             folder.path = list[k].path;
644             var el_title = new YAHOO.util.Element(title.id);
645             folder.on('contentReady', function() {
646                 this.on('click', function() {
647                     if(_client.ds.dynload) {
648                         var params = [];
649                         params['p'] = this.path;
650                         params['env'] = _client.env;
651                         params['repo_id'] = _client.repositoryid;
652                         params['ctx_id'] = $context->id;
653                         params['sesskey']= '$sesskey';
654                         params['accepted_types'] = _client.accepted_types;
655                         _client.loading('load');
656                         var trans = YAHOO.util.Connect.asyncRequest('POST',
657                                 '$CFG->httpswwwroot/repository/ws.php?action=list', _client.req_cb, _client.postdata(params));
658                     }else{
659                         _client.viewthumb(this.ds);
660                     }
661                 });
662             });
663             el_title.on('contentReady', function() {
664                 this.on('click', function(){
665                     folder.fireEvent('click');
666                 });
667             });    
668         } else {
669             var el_title = new YAHOO.util.Element(title.id);
670             var file = new YAHOO.util.Element(link.id);
671             el_title.title = file.title = list[k].title;
672             el_title.value = file.value = list[k].source;
673             el_title.icon = file.icon  = list[k].thumbnail;
674             if(list[k].repo_id) {
675                 el_title.repo_id = file.repo_id = list[k].repo_id;
676             }else{
677                 el_title.repo_id = file.repo_id = _client.repositoryid;
678             }
679             file.on('contentReady', function() {
680                 this.on('click', function() {
681                     repository_client_$suffix.rename(this.title, this.value, this.icon, this.repo_id);
682                 });
683             });
684             el_title.on('contentReady', function() {
685                 this.on('click', function() {
686                     repository_client_$suffix.rename(this.title, this.value, this.icon, this.repo_id);
687                 });
688             });
689         }
690         count++;
691     }
692     container.appendChild(panel);
693     _client.print_footer();
695 _client.buildtree = function(node, level) {
696     if(node.children) {
697         node.title = '<i><u>'+node.title+'</u></i>';
698     }
699     var info = {
700         label:node.title,
701         title:"$strdate"+node.date+' $strsize'+node.size,
702         filename:node.title,
703         value:node.source,
704         icon:node.thumbnail,
705         path:node.path
706     };
707     var tmpNode = new YAHOO.widget.TextNode(info, level, false);
708     var tooltip = new YAHOO.widget.Tooltip(tmpNode.labelElId, {
709         context:tmpNode.labelElId, text:info.title});
710     if(node.repo_id) {
711         tmpNode.repo_id=node.repo_id;
712     }else{
713         tmpNode.repo_id=_client.repositoryid;
714     }
715     if(node.children) {
716         if(node.expanded) {
717             tmpNode.expand();
718         }
719         tmpNode.isLeaf = false;
720         if (node.path) {
721             tmpNode.path = node.path;
722         } else {
723             tmpNode.path = '';
724         }
725         for(var c in node.children) {
726             _client.buildtree(node.children[c], tmpNode);
727         }
728     } else {
729         tmpNode.isLeaf = true;
730     }
732 _client.dynload = function (node, fnLoadComplete) {
733     var callback = {
734         success: function(o) {
735              try {
736                  var json = YAHOO.lang.JSON.parse(o.responseText);
737              } catch(e) {
738                  alert('$strinvalidjson - |dynload| -'+_client.stripHTML(o.responseText));
739                  return;
740              }
741              for(k in json.list) {
742                  _client.buildtree(json.list[k], node);
743              }
744              o.argument.fnLoadComplete();
745         },
746         failure:function(oResponse) {
747             alert('$strerror - |dynload| -');
748             oResponse.argument.fnLoadComplete();
749         },
750         argument:{"node":node, "fnLoadComplete": fnLoadComplete}
751     }
752     var params = [];
753     params['p']=node.path;
754     params['env']=_client.env;
755     params['sesskey']='$sesskey';
756     params['ctx_id']=$context->id;
757     params['repo_id']=_client.repositoryid;
758     params['accepted_types'] = _client.accepted_types;
759     var trans = YAHOO.util.Connect.asyncRequest('POST',
760             '$CFG->httpswwwroot/repository/ws.php?action=list', callback, _client.postdata(params));
762 _client.viewiframe = function() {
763     var panel = new YAHOO.util.Element('panel-$suffix');
764     panel.get('element').innerHTML = "<iframe frameborder=\"0\" width=\"98%\" height=\"400px\" src=\""+_client.ds.iframe+"\" />";
766 _client.viewlist = function() {
767     _client.viewmode = 1;
768     var panel = new YAHOO.util.Element('panel-$suffix');
769     _client.viewbar.check(1);
770     list = _client.ds.list;
771     _client.print_header();
772     panel.get('element').innerHTML += '<div id="treediv-$suffix"></div>';
773     var tree = new YAHOO.widget.TreeView('treediv-$suffix');
774     if(_client.ds.dynload) {
775         tree.setDynamicLoad(_client.dynload, 1);
776     } else {
777     }
778     for(k in list) {
779         _client.buildtree(list[k], tree.getRoot());
780     }
781     tree.draw();
782     tree.subscribe('clickEvent', function(e){
783         if(e.node.isLeaf){
784             repository_client_$suffix.rename(e.node.data.filename, e.node.data.value, e.node.data.icon, e.node.repo_id);
785         }
786     });
787     _client.print_footer();
789 _client.upload = function() {
790     var u = _client.ds.upload;
791     var aform = document.getElementById(u.id);
792     var parent = document.getElementById(u.id+'_div');
793     var d = document.getElementById(_client.ds.upload.id+'-file');
794     if(d.value!='' && d.value!=null) {
795         var container = document.createElement('DIV');
796         container.id = u.id+'_loading';
797         container.style.textAlign='center';
798         var img = document.createElement('IMG');
799         img.src = '$CFG->pixpath/i/progressbar.gif';
800         var para = document.createElement('p');
801         para.innerHTML = '$struploading';
802         container.appendChild(para);
803         container.appendChild(img);
804         parent.appendChild(container);
805         YAHOO.util.Connect.setForm(aform, true, true);
806         var trans = YAHOO.util.Connect.asyncRequest('POST',
807                 '$CFG->httpswwwroot/repository/ws.php?action=upload&sesskey=$sesskey&ctx_id=$context->id&repo_id='
808                 +_client.repositoryid,
809                 _client.upload_cb);
810     }else{
811         alert('$strfilenotnull');
812     }
814 _client.upload_cb = {
815 upload: function(o) {
816         try {
817             var ret = YAHOO.lang.JSON.parse(o.responseText);
818         } catch(e) {
819             alert('$strinvalidjson - |upload| -'+_client.stripHTML(o.responseText));
820         }
821         if(ret && ret.e) {
822             var panel = new YAHOO.util.Element('panel-$suffix');
823             panel.get('element').innerHTML = ret.e;
824             return;
825         }
826         if(ret) {
827             alert('$strsaved');
828             repository_client_$suffix.end(ret);
829         }
830     }
832 _client.uploadcontrol = function() {
833     var str = '';
834     if(_client.ds.upload) {
835         str += '<div id="'+_client.ds.upload.id+'_div" class="fp-upload-form">';
836         str += '<form id="'+_client.ds.upload.id+'" onsubmit="return false">';
837         str += '<label for="'+_client.ds.upload.id+'-file">'+_client.ds.upload.label+': </label>';
838         str += '<input type="file" id="'+_client.ds.upload.id+'-file" name="repo_upload_file" />';
839         str += '<p class="fp-upload-btn"><a href="###" onclick="return repository_client_$suffix.upload();">$strupload</a></p>';
840         str += '</form>';
841         str += '</div>';
842     }
843     return str;
845 _client.paging_node = function(type, page) {
846     if (page == _client.ds.page) {
847         str = '<a class="cur_page" onclick="repository_client_$suffix.'+type+'('+_client.repositoryid+', '+page+', '+page+')" href="###">';
848     } else {
849         str = '<a onclick="repository_client_$suffix.'+type+'('+_client.repositoryid+', '+page+', '+page+')" href="###">';
850     }
851     return str;
853 _client.makepage = function(id) {
854     var str = '';
855     if(_client.ds.pages) {
856         str += '<div class="fp-paging" id="paging-'+id+'-$suffix">';
857         if(!_client.ds.search_result){
858             var action = 'req';
859         } else {
860             var action = 'search_paging';
861         }
862         str += _client.paging_node(action, 1)+'1</a>';
864         if (_client.ds.page+2>=_client.ds.pages) {
865             var max = _client.ds.pages;
866         } else {
867             var max = _client.ds.page+2;
868         }
869         if (_client.ds.page-2 >= 3) {
870             str += ' ... ';
871             for(var i = _client.ds.page-2; i < max; i++) {
872                 str += _client.paging_node(action, i);
873                 str += String(i);
874                 str += '</a> ';
875             }
876         } else {
877             for(var i = 2; i < max; i++) {
878                 str += _client.paging_node(action, i);
879                 str += String(i);
880                 str += '</a> ';
881             }
882         }
883         if (max==_client.ds.pages) {
884             str += _client.paging_node(action, _client.ds.pages)+_client.ds.pages+'</a>';
885         } else {
886             str += _client.paging_node(action, max)+max+'</a>';
887             str += ' ... '+_client.paging_node(action, _client.ds.pages)+_client.ds.pages+'</a>';
888         }
889         str += '</div>';
890     }
891     return str;
893 _client.search_paging = function(id, path, page) {
894     _client.viewbar.set('disabled', false);
895     _client.loading('load');
896     _client.repositoryid = id;
897     var params = [];
898     params['p'] = path;
899     params['page'] = page;
900     params['env']=_client.env;
901     params['accepted_types'] = _client.accepted_types;
902     params['action']='search';
903     params['search_paging']='true';
904     params['sesskey']='$sesskey';
905     params['ctx_id']=$context->id;
906     params['repo_id']=id;
907     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action='+action, _client.req_cb, _client.postdata(params));
909 _client.makepath = function() {
910     if(_client.viewmode == 1) {
911         return;
912     }
913     var panel = new YAHOO.util.Element('panel-$suffix');
914     var p = _client.ds.path;
915     if(p && p.length!=0) {
916         var oDiv = document.createElement('DIV');
917         oDiv.id = "path-$suffix";
918         oDiv.className = "fp-pathbar";
919         panel.get('element').appendChild(oDiv);
920         for(var i = 0; i < _client.ds.path.length; i++) {
921             var link = document.createElement('A');
922             link.href = "###";
923             link.innerHTML = _client.ds.path[i].name;
924             link.id = 'path-'+i+'-el';
925             var sep = document.createElement('SPAN');
926             sep.innerHTML = '/';
927             oDiv.appendChild(link);
928             oDiv.appendChild(sep);
929             var el = new YAHOO.util.Element(link.id);
930             el.id = _client.repositoryid;
931             el.path = _client.ds.path[i].path;
932             el.on('contentReady', function() {
933                 var path_link = document.getElementById(link.id);
934                 path_link.id = this.id;
935                 path_link.path = this.path
936                 path_link.onclick = function() {
937                     repository_client_$suffix.req(this.id, this.path);
938                 }
939             });
940         }
941     }
943 // send download request
944 _client.download = function() {
945     var title = document.getElementById('newname-$suffix').value;
946     var file = document.getElementById('fileurl-$suffix').value;
947     _client.loading('download', title);
948     var params = [];
949     if(_client.itemid){
950         params['itemid']=_client.itemid;
951     }
952     params['env']=_client.env;
953     params['file']=file;
954     params['title']=title;
955     params['sesskey']='$sesskey';
956     params['ctx_id']=$context->id;
957     params['repo_id']=_client.repositoryid;
958     var trans = YAHOO.util.Connect.asyncRequest('POST',
959             '$CFG->httpswwwroot/repository/ws.php?action=download', _client.download_cb, _client.postdata(params));
961 // send login request
962 _client.login = function() {
963     var params = [];
964     var data = _client.ds.login;
965     for (var k in data) {
966         if(data[k].type!='popup') {
967             var el = document.getElementsByName(data[k].name)[0];
968             params[data[k].name] = '';
969             if(el.type == 'checkbox') {
970                 params[data[k].name] = el.checked;
971             } else {
972                 params[data[k].name] = el.value;
973             }
974         }
975     }
976     params['env'] = _client.env;
977     params['repo_id'] = _client.repositoryid;
978     params['ctx_id'] = $context->id;
979     params['sesskey']= '$sesskey';
980     params['accepted_types'] = _client.accepted_types;
981     _client.loading('load');
982     var trans = YAHOO.util.Connect.asyncRequest('POST',
983             '$CFG->httpswwwroot/repository/ws.php?action=sign', _client.req_cb, _client.postdata(params));
985 _client.end = function(obj) {
986     if(_client.env=='filepicker') {
987         _client.target.value = obj['id'];
988     }else if(_client.env=='editor'){
989         _client.target.value = obj['url'];
990         _client.target.onchange();
991     }
992     _client.formcallback(obj);
993     _client.instance.hide();
994     _client.viewfiles();
996 _client.hide = function() {
997     _client.instance.hide();
998     _client.viewfiles();
1000 // request file list or login
1001 _client.req = function(id, path, page) {
1002     _client.viewbar.set('disabled', false);
1003     _client.loading('load');
1004     _client.repositoryid = id;
1005     var params = [];
1006     params['p'] = path;
1007     params['env']=_client.env;
1008     params['sesskey']='$sesskey';
1009     params['ctx_id']=$context->id;
1010     params['repo_id']=id;
1011     if (page) {
1012         params['page']=page;
1013     }
1014     params['accepted_types'] = _client.accepted_types;
1015     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action=list', _client.req_cb, _client.postdata(params));
1017 _client.logout = function(id) {
1018     _client.repositoryid = id;
1019     var params = [];
1020     params['repo_id'] = id;
1021     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action=logout', _client.req_cb, _client.postdata(params));
1023 _client.search_form_cb = {
1024 success: function(o) {
1025      var el = document.getElementById('fp-search-dlg');
1026      var _r = repository_client_$suffix;
1027      if(el) {
1028          el.innerHTML = '';
1029      } else {
1030          var el = document.createElement('DIV');
1031          el.id = 'fp-search-dlg';
1032      }
1033      var div1 = document.createElement('DIV');
1034      div1.className = 'hd';
1035      div1.innerHTML = "$strsearching\"" + _r.repos[_r.repositoryid].name + '"';
1036      var div2 = document.createElement('DIV');
1037      div2.className = 'bd';
1038      var sform = document.createElement('FORM');
1039      sform.method = 'POST';
1040      sform.id = "fp-search-form";
1041      sform.action = '$CFG->httpswwwroot/repository/ws.php?action=search';
1042      sform.innerHTML = o.responseText;
1043      div2.appendChild(sform);
1044      el.appendChild(div1);
1045      el.appendChild(div2);
1046      document.body.appendChild(el);
1047      var dlg = new YAHOO.widget.Dialog("fp-search-dlg",{
1048         postmethod: 'async',
1049         draggable: true,
1050         width : "30em",
1051         fixedcenter : true,
1052         zindex: 766667,
1053         visible : false,
1054         constraintoviewport : true,
1055         buttons : [
1056             { text:"$strsubmit",handler: function() {
1057                 _client.viewbar.set('disabled', false); _client.loading('load');
1058                 YAHOO.util.Connect.setForm('fp-search-form', false, false);
1059                 this.cancel();
1060                 var trans = YAHOO.util.Connect.asyncRequest('POST',
1061                     '$CFG->httpswwwroot/repository/ws.php?action=search&env='+_client.env, _client.req_cb);
1062                 },isDefault:true},
1063             {text:"$strcancel",handler:function() {this.cancel()}}
1064         ]
1065     });
1066     dlg.render();
1067     dlg.show();
1070 _client.search_form = function(id) {
1071     var params = [];
1072     params['env']=_client.env;
1073     params['sesskey']='$sesskey';
1074     params['ctx_id']=$context->id;
1075     params['repo_id']=id;
1076     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action=searchform', _client.search_form_cb, _client.postdata(params));
1078 _client.req_cb = {
1079 success: function(o) {
1080      var panel = new YAHOO.util.Element('panel-$suffix');
1081      try {
1082          var data = YAHOO.lang.JSON.parse(o.responseText);
1083      } catch(e) {
1084          alert('$strinvalidjson - |req_cb| -'+_client.stripHTML(o.responseText));
1085      };
1086      if(data && data.e) {
1087          panel.get('element').innerHTML = data.e;
1088          return;
1089      }
1090      _client.ds = data;
1091      if(!data) {
1092          return;
1093      }else if(data.msg){
1094          _client.print_msg(data.msg);
1095      }else if(data.iframe) {
1096          _client.viewiframe();
1097      }else if(data.login) {
1098          _client.print_login();
1099      }else if(data.list) {
1100          if(_client.viewmode) {
1101              _client.viewlist();
1102          } else {
1103              _client.viewthumb();
1104          }
1105      }
1108 _client.download_cb = {
1109 success: function(o) {
1110      var panel = new YAHOO.util.Element('panel-$suffix');
1111      try {
1112          var data = YAHOO.lang.JSON.parse(o.responseText);
1113      } catch(e) {
1114          alert('$strinvalidjson - |download_cb| -'+_client.stripHTML(o.responseText));
1115      }
1116      if(data && data.e) {
1117          panel.get('element').innerHTML = data.e;
1118          return;
1119      }
1120      if(data) {
1121          repository_client_$suffix.end(data);
1122      }
1126 return _client;
1127 })();
1128 EOD;
1130 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
1131 if (is_array($accepted_filetypes) && in_array('*', $accepted_filetypes)) {
1132     $accepted_filetypes = '*';
1134 $repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returnvalue);
1135 $js .= "\r\n".'repository_client_'.$suffix.'.repos=[];'."\r\n";
1136 foreach ($repos as $repo) {
1137     $info = $repo->ajax_info();
1138     $js .= "\r\n";
1139     $js .= 'repository_client_'.$suffix.'.repos['.$info->id.']='.json_encode($repo->ajax_info()).';'."\n";
1141 $js .= "\r\n";
1143 $ft = new file_type_to_ext();
1144 $image_file_ext = json_encode($ft->get_file_ext(array('image')));
1145 $video_file_ext = json_encode($ft->get_file_ext(array('video')));
1146 $accepted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
1147 $js .= <<<EOD
1148 function openpicker_$suffix(params) {
1149     if(params.filetype) {
1150         if(params.filetype == 'image') {
1151             repository_client_$suffix.accepted_types = $image_file_ext;
1152         } else if(params.filetype == 'video' || params.filetype== 'media') {
1153             repository_client_$suffix.accepted_types = $video_file_ext;
1154         } else if(params.filetype == 'file') {
1155             repository_client_$suffix.accepted_types = '*';
1156         }
1157     } else {
1158         repository_client_$suffix.accepted_types = $accepted_file_ext;
1159     }
1160     if(!repository_client_$suffix.instance) {
1161         repository_client_$suffix.env = params.env;
1162         repository_client_$suffix.target = params.target;
1163         if(params.itemid){
1164             repository_client_$suffix.itemid = params.itemid;
1165         } else if(tinyMCE && id2itemid[tinyMCE.selectedInstance.editorId]){
1166             repository_client_$suffix.itemid = id2itemid[tinyMCE.selectedInstance.editorId];
1167         }
1168         repository_client_$suffix.instance = new repository_client_$suffix();
1169         repository_client_$suffix.instance.create_picker();
1170         if(params.callback) {
1171             repository_client_$suffix.formcallback = params.callback;
1172         } else {
1173             repository_client_$suffix.formcallback = function() {};
1174         }
1175     } else {
1176         repository_client_$suffix.target = params.target;
1177         repository_client_$suffix.instance.show();
1178     }
1180 //]]>
1181 </script>
1182 EOD;
1183 return array('css'=>$css, 'js'=>$js, 'suffix'=>$suffix);