Your IP : 3.147.51.72


Current Path : /home/ncdcgo/moodledata/localcache/js/
Upload File :
Current File : /home/ncdcgo/moodledata/localcache/js/fcbe1d97f06c7c0dc056c47afdbae5e137588590

/**
 * Javascript library for enableing a drag and drop upload to courses
 *
 * @package    core
 * @subpackage course
 * @copyright  2012 Davo Smith
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
M.course_dndupload={Y:null,url:M.cfg.wwwroot+'/course/dndupload.php',maxbytes:0,courseid:null,handlers:null,entercount:0,currentsection:null,uploadqueue:null,uploaddialog:!1,lastselected:null,sectiontypename:'li',sectionclasses:['section','main'],pagecontentid:'page',modslistselector:'ul.section',init:function(Y,options){this.Y=Y;if(!this.browser_supported()){return}
this.maxbytes=options.maxbytes;this.courseid=options.courseid;this.handlers=options.handlers;this.uploadqueue=new Array();this.lastselected=new Array();var sectionselector=this.sectiontypename+'.'+this.sectionclasses.join('.');var sections=this.Y.all(sectionselector);if(sections.isEmpty()){return}
sections.each(function(el){this.add_preview_element(el);this.init_events(el)},this);if(options.showstatus){this.add_status_div()}
var self=this;require(['core_courseformat/courseeditor','core_course/events'],function(Editor,CourseEvents){self.courseeditor=Editor.getCurrentCourseEditor();document.querySelector('#'+self.pagecontentid).addEventListener(CourseEvents.sectionRefreshed,self.sectionRefreshed.bind(self))});document.addEventListener('scroll',function(){sections.each(function(el){if(el.hasClass('dndupload-dropzone')){self.rePositionPreviewInfoElement(el)}},this)},!0)},sectionRefreshed:function(event){if(event.detail.newSectionElement===undefined){return}
var element=this.Y.one(event.detail.newSectionElement);this.add_preview_element(element);this.init_events(element)},add_status_div:function(){var Y=this.Y,coursecontents=Y.one('#'+this.pagecontentid),div,handlefile=(this.handlers.filehandlers.length>0),handletext=!1,handlelink=!1,i=0,styletop,styletopunit;if(!coursecontents){return}
div=Y.Node.create('<div id="dndupload-status"></div>').setStyle('opacity','0.0');coursecontents.insert(div,0);for(i=0;i<this.handlers.types.length;i++){switch(this.handlers.types[i].identifier){case 'text':case 'text/html':handletext=!0;break;case 'url':handlelink=!0;break}}
$msgident='dndworking';if(handlefile){$msgident+='file'}
if(handletext){$msgident+='text'}
if(handlelink){$msgident+='link'}
div.setContent(M.util.get_string($msgident,'moodle'));styletop=div.getStyle('top')||'0px';styletopunit=styletop.replace(/^\d+/,'');styletop=parseInt(styletop.replace(/\D*$/,''),10);var fadein=new Y.Anim({node:'#dndupload-status',from:{opacity:0.0,top:(styletop-30).toString()+styletopunit},to:{opacity:1.0,top:styletop.toString()+styletopunit},duration:0.5});var fadeout=new Y.Anim({node:'#dndupload-status',from:{opacity:1.0,top:styletop.toString()+styletopunit},to:{opacity:0.0,top:(styletop-30).toString()+styletopunit},duration:0.5});fadein.run();fadein.on('end',function(e){Y.later(3000,this,function(){fadeout.run()})});fadeout.on('end',function(e){Y.one('#dndupload-status').remove(!0)})},browser_supported:function(){if(typeof FileReader=='undefined'){return!1}
if(typeof FormData=='undefined'){return!1}
return!0},init_events:function(el){this.Y.on('dragenter',this.drag_enter,el,this);this.Y.on('dragleave',this.drag_leave,el,this);this.Y.on('dragover',this.drag_over,el,this);this.Y.on('drop',this.drop,el,this)},get_section:function(el){var sectionclasses=this.sectionclasses;return el.ancestor(function(test){var i;for(i=0;i<sectionclasses.length;i++){if(!test.hasClass(sectionclasses[i])){return!1}
return!0}},!0)},get_section_number:function(section){var sectionid=section.get('id').split('-');if(sectionid.length<2||sectionid[0]!='section'){return!1}
return parseInt(sectionid[1])},types_includes:function(e,type){var i;var types=e._event.dataTransfer.types;type=type.toLowerCase();for(i=0;i<types.length;i++){if(!types.hasOwnProperty(i)){continue}
if(types[i].toLowerCase()===type){return!0}}
return!1},typesIncludesFilesOnly:function(e){return e._event.dataTransfer.types.every(function(currentType){return(currentType.toLowerCase()!='text/uri-list'&&currentType.toLowerCase()!='text/html'&&currentType.toLowerCase()!='text/plain')})},drag_type:function(e){if(e._event.dataTransfer===null){return!1}
if(e._event.dataTransfer.types===null){return!1}
if(e._event.dataTransfer.types.length==0){return!1}
if(this.types_includes(e,'Files')&&this.typesIncludesFilesOnly(e)){if(e.type!='drop'||e._event.dataTransfer.files.length!=0){if(this.handlers.filehandlers.length==0){return!1}
return{realtype:'Files',addmessage:M.util.get_string('addfilehere','moodle'),namemessage:null,type:'Files'}}}
var types=this.handlers.types;for(var i=0;i<types.length;i++){var dttypes=types[i].datatransfertypes;for(var j=0;j<dttypes.length;j++){if(this.types_includes(e,dttypes[j])){return{realtype:dttypes[j],addmessage:types[i].addmessage,namemessage:types[i].namemessage,handlermessage:types[i].handlermessage,type:types[i].identifier,handlers:types[i].handlers}}}}
return!1},check_drag:function(e){var type=this.drag_type(e);if(type){e.stopPropagation();e.preventDefault()}
return type},drag_enter:function(e){if(!(type=this.check_drag(e))){return!1}
var section=this.get_section(e.currentTarget);if(!section){return!1}
if(this.currentsection&&this.currentsection!=section){this.currentsection=section;this.entercount=1}else{this.entercount++;if(this.entercount>2){this.entercount=2;return!1}}
this.showPreviewInfoElement(section);return!1},drag_leave:function(e){if(!this.check_drag(e)){return!1}
this.entercount--;if(this.entercount==1){return!1}
this.entercount=0;this.currentsection=null;this.hide_preview_element();return!1},drag_over:function(e){this.check_drag(e);return!1},drop:function(e){this.hide_preview_element();if(!(type=this.check_drag(e))){return!1}
var section=this.get_section(e.currentTarget);var sectionnumber=this.get_section_number(section);if(type.type=='Files'){var files=e._event.dataTransfer.files;for(var i=0,f;f=files[i];i++){this.handle_file(f,section,sectionnumber)}}else{var contents=e._event.dataTransfer.getData(type.realtype);if(contents){this.handle_item(type,contents,section,sectionnumber)}}
return!1},get_mods_element:function(section){var modsel=section.one(this.modslistselector);if(!modsel){modsel=document.createElement('ul');modsel.className='section img-text';var contentel=section.get('children').pop();var brel=contentel.get('children').pop();contentel.insertBefore(modsel,brel);modsel=this.Y.one(modsel)}
return modsel},add_resource_element:function(name,section,module){var modsel=this.get_mods_element(section);var resel={parent:modsel,li:document.createElement('li'),div:document.createElement('div'),indentdiv:document.createElement('div'),a:document.createElement('a'),icon:document.createElement('img'),namespan:document.createElement('span'),groupingspan:document.createElement('span'),progressouter:document.createElement('span'),progress:document.createElement('span')};resel.li.className='activity '+module+' modtype_'+module;resel.indentdiv.className='mod-indent';resel.li.appendChild(resel.indentdiv);resel.div.className='activityinstance';resel.indentdiv.appendChild(resel.div);resel.a.href='#';resel.div.appendChild(resel.a);resel.icon.src=M.util.image_url('i/ajaxloader');resel.icon.className='activityicon iconlarge';resel.a.appendChild(resel.icon);resel.namespan.className='instancename';resel.namespan.innerHTML=name;resel.a.appendChild(resel.namespan);resel.groupingspan.className='groupinglabel';resel.div.appendChild(resel.groupingspan);resel.progressouter.className='dndupload-progress-outer';resel.progress.className='dndupload-progress-inner';resel.progress.innerHTML='&nbsp;';resel.progressouter.appendChild(resel.progress);resel.div.appendChild(resel.progressouter);modsel.insertBefore(resel.li,modsel.get('children').pop());return resel},hide_preview_element:function(){this.Y.all('.dndupload-preview-wrapper').addClass('dndupload-hidden');this.Y.all('.dndupload-over').removeClass('dndupload-over');this.Y.all('.dndupload-dropzone').removeClass('dndupload-dropzone')},show_preview_element:function(section,type){this.hide_preview_element();var preview=section.one('li.dndupload-preview').removeClass('dndupload-hidden');section.addClass('dndupload-over');var node=preview.one('span').getDOMNode();node.firstChild.nodeValue=type.addmessage},showPreviewInfoElement:function(section){this.hide_preview_element();section.one('.dndupload-preview-wrapper').removeClass('dndupload-hidden');section.addClass('dndupload-dropzone');this.rePositionPreviewInfoElement(section)},add_preview_element:function(section){const modsEl=this.get_mods_element(section);const overlay=document.createElement('div');overlay.className='dndupload-preview-overlay';modsEl.get('parentNode').appendChild(overlay);const preview={wrapper:document.createElement('div'),div:document.createElement('div'),iconSpan:document.createElement('span'),nameSpan:document.createElement('span')};preview.wrapper.className='dndupload-preview-wrapper dndupload-hidden';preview.wrapper.appendChild(preview.div);preview.div.className='dndupload-preview';preview.div.appendChild(document.createTextNode(' '));preview.iconSpan.className='fa fa-arrow-circle-o-down';preview.nameSpan.className='instancename';preview.nameSpan.innerHTML=M.util.get_string('addfilehere','moodle');preview.div.appendChild(preview.iconSpan);preview.div.appendChild(preview.nameSpan);modsEl.get('parentNode').appendChild(preview.wrapper)},rePositionPreviewInfoElement:function(section){const sectionElement=document.getElementById(section.get('id'));const rect=sectionElement.getBoundingClientRect();const sectionHeight=parseInt(window.getComputedStyle(sectionElement).height,10);const sectionOffset=rect.top;const preview=sectionElement.querySelector('.dndupload-preview-wrapper');const previewHeight=parseInt(window.getComputedStyle(preview).height,10)+(2*parseInt(window.getComputedStyle(preview).padding,10));let top,bottom;if(sectionOffset<0){if(sectionHeight+sectionOffset>=previewHeight){let offSetTop=0-sectionOffset;const navBar=document.querySelector('nav.navbar.fixed-top');if(navBar){offSetTop=offSetTop+navBar.offsetHeight}
top=offSetTop+'px';bottom='unset'}else{top='unset';bottom=0}}else{top=0;bottom='unset'}
preview.style.top=top
preview.style.bottom=bottom},handle_file:function(file,section,sectionnumber){var handlers=new Array();var filehandlers=this.handlers.filehandlers;var extension='';var dotpos=file.name.lastIndexOf('.');if(dotpos!=-1){extension=file.name.substr(dotpos+1,file.name.length).toLowerCase()}
for(var i=0;i<filehandlers.length;i++){if(filehandlers[i].extension=='*'||filehandlers[i].extension==extension){handlers.push(filehandlers[i])}}
if(handlers.length==0){return}
if(handlers.length==1){this.upload_file(file,section,sectionnumber,handlers[0].module);return}
this.file_handler_dialog(handlers,extension,file,section,sectionnumber)},file_handler_dialog:function(handlers,extension,file,section,sectionnumber){if(this.uploaddialog){var details=new Object();details.isfile=!0;details.handlers=handlers;details.extension=extension;details.file=file;details.section=section;details.sectionnumber=sectionnumber;this.uploadqueue.push(details);return}
this.uploaddialog=!0;var timestamp=new Date().getTime();var uploadid=Math.round(Math.random()*100000)+'-'+timestamp;var content='';var sel;if(extension in this.lastselected){sel=this.lastselected[extension]}else{sel=handlers[0].module}
content+='<p>'+M.util.get_string('actionchoice','moodle',file.name)+'</p>';content+='<div id="dndupload_handlers'+uploadid+'">';for(var i=0;i<handlers.length;i++){var id='dndupload_handler'+uploadid+handlers[i].module;var checked=(handlers[i].module==sel)?'checked="checked" ':'';content+='<input type="radio" name="handler" value="'+handlers[i].module+'" id="'+id+'" '+checked+'/>';content+=' <label for="'+id+'">';content+=handlers[i].message;content+='</label><br/>'}
content+='</div>';var Y=this.Y;var self=this;var panel=new M.core.dialogue({bodyContent:content,width:'350px',modal:!0,visible:!1,render:!0,align:{node:null,points:[Y.WidgetPositionAlign.CC,Y.WidgetPositionAlign.CC]}});panel.show();panel.after("visibleChange",function(e){if(!panel.get('visible')){panel.destroy(!0);self.check_upload_queue()}});panel.addButton({label:M.util.get_string('upload','moodle'),action:function(e){e.preventDefault();var module=!1;var div=Y.one('#dndupload_handlers'+uploadid);div.all('input').each(function(input){if(input.get('checked')){module=input.get('value')}});if(!module){return}
panel.hide();self.lastselected[extension]=module;self.upload_file(file,section,sectionnumber,module)},section:Y.WidgetStdMod.FOOTER});panel.addButton({label:M.util.get_string('cancel','moodle'),action:function(e){e.preventDefault();panel.hide()},section:Y.WidgetStdMod.FOOTER})},check_upload_queue:function(){this.uploaddialog=!1;if(this.uploadqueue.length==0){return}
var details=this.uploadqueue.shift();if(details.isfile){this.file_handler_dialog(details.handlers,details.extension,details.file,details.section,details.sectionnumber)}else{this.handle_item(details.type,details.contents,details.section,details.sectionnumber)}},upload_file:function(file,section,sectionnumber,module){var xhr=new XMLHttpRequest();var self=this;if(this.maxbytes>0&&file.size>this.maxbytes){new M.core.alert({message:M.util.get_string('namedfiletoolarge','moodle',{filename:file.name})});return}
var resel=this.add_resource_element(file.name,section,module);xhr.upload.addEventListener('progress',function(e){if(e.lengthComputable){var percentage=Math.round((e.loaded*100)/e.total);resel.progress.style.width=percentage+'%'}},!1);xhr.onreadystatechange=function(){if(xhr.readyState==1){this.originalUnloadEvent=window.onbeforeunload;require(['core_form/events'],function(FormEvent){FormEvent.notifyUploadStarted(section.get('id'))})}
if(xhr.readyState==4){if(xhr.status==200){var result=JSON.parse(xhr.responseText);if(result){if(result.error==0){resel.li.outerHTML=result.fullcontent;if(self.Y.UA.gecko>0){resel.li.outerHTML=unescape(resel.li.outerHTML)}
self.add_editing(result.elementid);self.courseeditor.dispatch('cmState',[result.cmid]);require(['core/event','jquery'],function(event,$){event.notifyFilterContentUpdated($(result.fullcontent))})}else{resel.parent.removeChild(resel.li);new M.core.alert({message:result.error})}}}else{new M.core.alert({message:M.util.get_string('servererror','moodle')})}
require(['core_form/events'],function(FormEvent){FormEvent.notifyUploadCompleted(section.get('id'))})}};var formData=new FormData();try{formData.append('repo_upload_file',file)}catch(e){resel.parent.removeChild(resel.li);new M.core.alert({message:M.util.get_string('filereaderror','moodle',file.name)});return}
formData.append('sesskey',M.cfg.sesskey);formData.append('course',this.courseid);formData.append('section',sectionnumber);formData.append('module',module);formData.append('type','Files');var reader=new FileReader();reader.onload=function(){xhr.open("POST",self.url,!0);xhr.send(formData)};reader.onerror=function(){resel.parent.removeChild(resel.li);new M.core.alert({message:M.util.get_string('filereaderror','moodle',file.name)})};if(file.size>0){reader.readAsText(file.slice(0,5))}else{reader.readAsText(file)}},handle_item:function(type,contents,section,sectionnumber){if(type.handlers.length==0){return}
if(type.handlers.length==1&&type.handlers[0].noname){this.upload_item('',type.type,contents,section,sectionnumber,type.handlers[0].module);this.check_upload_queue();return}
if(this.uploaddialog){var details=new Object();details.isfile=!1;details.type=type;details.contents=contents;details.section=section;details.setcionnumber=sectionnumber;this.uploadqueue.push(details);return}
this.uploaddialog=!0;var timestamp=new Date().getTime();var uploadid=Math.round(Math.random()*100000)+'-'+timestamp;var nameid='dndupload_handler_name'+uploadid;var content='';if(type.handlers.length>1){content+='<p>'+type.handlermessage+'</p>';content+='<div id="dndupload_handlers'+uploadid+'">';var sel=type.handlers[0].module;for(var i=0;i<type.handlers.length;i++){var id='dndupload_handler'+uploadid+type.handlers[i].module;var checked=(type.handlers[i].module==sel)?'checked="checked" ':'';content+='<input type="radio" name="handler" value="'+i+'" id="'+id+'" '+checked+'/>';content+=' <label for="'+id+'">';content+=type.handlers[i].message;content+='</label><br/>'}
content+='</div>'}
var disabled=(type.handlers[0].noname)?' disabled = "disabled" ':'';content+='<label for="'+nameid+'">'+type.namemessage+'</label>';content+=' <input type="text" id="'+nameid+'" value="" '+disabled+' />';var Y=this.Y;var self=this;var panel=new M.core.dialogue({bodyContent:content,width:'350px',modal:!0,visible:!0,render:!0,align:{node:null,points:[Y.WidgetPositionAlign.CC,Y.WidgetPositionAlign.CC]}});panel.after("visibleChange",function(e){if(!panel.get('visible')){panel.destroy(!0);self.check_upload_queue()}});var namefield=Y.one('#'+nameid);var submit=function(e){e.preventDefault();var name=Y.Lang.trim(namefield.get('value'));var module=!1;var noname=!1;if(type.handlers.length>1){var div=Y.one('#dndupload_handlers'+uploadid);div.all('input').each(function(input){if(input.get('checked')){var idx=input.get('value');module=type.handlers[idx].module;noname=type.handlers[idx].noname}});if(!module){return}}else{module=type.handlers[0].module;noname=type.handlers[0].noname}
if(name==''&&!noname){return}
if(noname){name=''}
panel.hide();self.upload_item(name,type.type,contents,section,sectionnumber,module)};panel.addButton({label:M.util.get_string('upload','moodle'),action:submit,section:Y.WidgetStdMod.FOOTER,name:'submit'});panel.addButton({label:M.util.get_string('cancel','moodle'),action:function(e){e.preventDefault();panel.hide()},section:Y.WidgetStdMod.FOOTER});var submitbutton=panel.getButton('submit').button;namefield.on('key',submit,'enter');namefield.after('keyup',function(){if(Y.Lang.trim(namefield.get('value'))==''){submitbutton.disable()}else{submitbutton.enable()}});for(i=0;i<type.handlers.length;i++){if(type.handlers[i].noname){Y.one('#dndupload_handler'+uploadid+type.handlers[i].module).on('click',function(e){namefield.set('disabled','disabled');submitbutton.enable()})}else{Y.one('#dndupload_handler'+uploadid+type.handlers[i].module).on('click',function(e){namefield.removeAttribute('disabled');namefield.focus();if(Y.Lang.trim(namefield.get('value'))==''){submitbutton.disable()}})}}
Y.one('#'+nameid).focus()},upload_item:function(name,type,contents,section,sectionnumber,module){var xhr=new XMLHttpRequest();var self=this;var resel=this.add_resource_element(name,section,module);xhr.onreadystatechange=function(){if(xhr.readyState==1){this.originalUnloadEvent=window.onbeforeunload;require(['core_form/events'],function(FormEvent){FormEvent.notifyUploadStarted(section.get('id'))})}
if(xhr.readyState==4){if(xhr.status==200){var result=JSON.parse(xhr.responseText);if(result){if(result.error==0){resel.li.outerHTML=result.fullcontent;if(self.Y.UA.gecko>0){resel.li.outerHTML=unescape(resel.li.outerHTML)}
self.add_editing(result.elementid);self.courseeditor.dispatch('cmState',[result.cmid])}else{resel.parent.removeChild(resel.li);require(['core_form/events'],function(FormEvent){FormEvent.notifyUploadCompleted(section.get('id'))});new M.core.alert({message:result.error})}}}else{require(['core_form/events'],function(FormEvent){FormEvent.notifyUploadCompleted(section.get('id'))});new M.core.alert({message:M.util.get_string('servererror','moodle')})}
require(['core_form/events'],function(FormEvent){FormEvent.notifyUploadCompleted(section.get('id'))})}};var formData=new FormData();formData.append('contents',contents);formData.append('displayname',name);formData.append('sesskey',M.cfg.sesskey);formData.append('course',this.courseid);formData.append('section',sectionnumber);formData.append('type',type);formData.append('module',module);xhr.open("POST",this.url,!0);xhr.send(formData)},add_editing:function(elementid){var node=Y.one('#'+elementid);YUI().use('moodle-course-coursebase',function(Y){Y.log("Invoking setup_for_resource",'debug','coursedndupload');M.course.coursebase.invoke_function('setup_for_resource',node)});if(M.core.actionmenu&&M.core.actionmenu.newDOMNode){M.core.actionmenu.newDOMNode(node)}}}