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