MDL-29766 Add drag and drop upload to filemanager / filepicker elements
[moodle.git] / lib / form / filepicker.js
CommitLineData
4c508047
PS
1
2M.form_filepicker = {};
63d5c4ac
RT
3M.form_filepicker.Y = null;
4M.form_filepicker.instances = [];
4c508047
PS
5
6M.form_filepicker.callback = function(params) {
0c4edaa2 7 var html = '<a href="'+params['url']+'">'+params['file']+'</a>';
dd070162 8 document.getElementById('file_info_'+params['client_id']).innerHTML = html;
63d5c4ac
RT
9 //When file is added then set status of global variable to true
10 var elementname = M.core_filepicker.instances[params['client_id']].options.elementname;
11 M.form_filepicker.instances[elementname].fileadded = true;
12 //generate event to indicate changes which will be used by disable if or validation code
13 M.form_filepicker.Y.one('#id_'+elementname).simulate('change');
4c508047
PS
14};
15
16/**
17 * This fucntion is called for each file picker on page.
18 */
19M.form_filepicker.init = function(Y, options) {
63d5c4ac
RT
20 //Keep reference of YUI, so that it can be used in callback.
21 M.form_filepicker.Y = Y;
22
23 //For client side validation, initialize file status for this filepicker
24 M.form_filepicker.instances[options.elementname] = {};
25 M.form_filepicker.instances[options.elementname].fileadded = false;
26
27 //Set filepicker callback
4c508047 28 options.formcallback = M.form_filepicker.callback;
63d5c4ac 29
4c508047 30 if (!M.core_filepicker.instances[options.client_id]) {
63d5c4ac 31 M.core_filepicker.init(Y, options);
4c508047
PS
32 }
33 Y.on('click', function(e, client_id) {
34 e.preventDefault();
35 M.core_filepicker.instances[client_id].show();
36 }, '#filepicker-button-'+options.client_id, null, options.client_id);
dd070162 37
4c508047
PS
38 var item = document.getElementById('nonjs-filepicker-'+options.client_id);
39 if (item) {
40 item.parentNode.removeChild(item);
41 }
42 item = document.getElementById('filepicker-wrapper-'+options.client_id);
43 if (item) {
44 item.style.display = '';
45 }
f08fac7c
DS
46
47 var dndoptions = {
48 clientid: options.client_id,
49 acceptedtypes: options.accepted_types,
50 maxfiles: -1,
51 maxbytes: options.maxbytes,
52 itemid: options.itemid,
53 repositories: options.repositories,
54 formcallback: options.formcallback,
55 containerprefix: '#file_info_',
56 };
57 M.form_dndupload.init(Y, dndoptions);
4c508047 58};