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