From 99eaca9d3d9cd2b42fd8586bbb65dec125b12379 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Fri, 15 Jan 2010 07:48:38 +0000 Subject: [PATCH] "MDL-21170, major update to filepicker, complete rewrote filepicker javascript code to use YUI3, need furthur polish" --- lang/en_utf8/repository.php | 1 + lib/ajax/ajaxlib.php | 22 +- lib/editor/tinymce/extra/tinymce_utils.js | 27 +- lib/filelib.php | 11 +- lib/form/editor.php | 27 +- lib/form/filemanager.js | 17 + lib/form/filemanager.php | 60 +- lib/form/filepicker.js | 33 +- lib/form/filepicker.php | 30 +- lib/javascript-static.js | 5 +- repository/boxnet/repository.class.php | 2 +- repository/filepicker.js | 1086 ++++++++++++++ repository/flickr_public/repository.class.php | 61 +- repository/lib.php | 272 ++-- repository/repository.js | 1 - repository/repository.src.js | 1314 ----------------- repository/repository_ajax.php | 23 +- 17 files changed, 1378 insertions(+), 1614 deletions(-) create mode 100644 repository/filepicker.js delete mode 100644 repository/repository.js delete mode 100644 repository/repository.src.js diff --git a/lang/en_utf8/repository.php b/lang/en_utf8/repository.php index a754e4824b1..404e6adc2fb 100644 --- a/lang/en_utf8/repository.php +++ b/lang/en_utf8/repository.php @@ -117,6 +117,7 @@ $string['submit'] = 'Submit'; $string['sync'] = 'Sync'; $string['title'] = 'Choose a file...'; $string['thumbview'] = 'View as icons'; +$string['iconview'] = 'View as icons'; $string['typenotvisible'] = 'Type not visible'; $string['updown'] = 'Display order'; $string['upload'] = 'Upload this file'; diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index d5a38653319..ac886d69d87 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -166,6 +166,8 @@ class page_requirements_manager { $this->yui2_lib('dom'); // needs to be migrated to YUI3 before we release 2.0 $this->yui2_lib('container'); // needs to be migrated to YUI3 before we release 2.0 $this->yui2_lib('connection'); // needs to be migrated to YUI3 before we release 2.0 + // File Picker use this module loading YUI2 widgets + $this->yui2_lib('yuiloader'); // needs to be migrated to YUI3 before we release 2.0 $this->string_for_js('confirmation', 'admin'); $this->string_for_js('cancel', 'moodle'); @@ -603,14 +605,22 @@ class page_requirements_manager { $ondomreadyjs = $this->get_javascript_code(self::WHEN_ON_DOM_READY, ' '); + $pathtofilepicker = $CFG->httpswwwroot.'/repository/filepicker.js'; + $js .= <<options['CURLOPT_FOLLOWLOCATION'] = 1; + //$this->options['CURLOPT_FOLLOWLOCATION'] = 1; $this->options['CURLOPT_MAXREDIRS'] = 10; $this->options['CURLOPT_ENCODING'] = ''; // TRUE to return the transfer as a string of the return @@ -2764,7 +2764,7 @@ class curl_cache { * @copyright 2009 Dongsheng Cai * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class file_type_to_ext { +class filetype_parser { /** * Check file_types.mm file, setup variables * @@ -2826,7 +2826,10 @@ class file_type_to_ext { * @param array $types * @return mixed */ - public function get_file_ext($types) { + public function get_extensions($types) { + if (!is_array($types)) { + $types = array($types); + } $this->result = array(); if ((is_array($types) && in_array('*', $types)) || $types == '*' || empty($types)) { @@ -2848,7 +2851,7 @@ class file_type_to_ext { } } } else { - exit('Failed to open test.xml.'); + exit('Failed to open file'); } return $this->result; } diff --git a/lib/form/editor.php b/lib/form/editor.php index 98000aa6dee..e5b59e4abc9 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -18,7 +18,8 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null); function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) { - global $CFG; + global $CFG, $PAGE; + $PAGE->requires->js('lib/form/filepicker.js'); $options = (array)$options; foreach ($options as $name=>$value) { @@ -35,7 +36,6 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $this->_options['trusted'] = trusttext_trusted($this->_options['context']); parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); - repository_head_setup(); editors_head_setup(); } @@ -114,6 +114,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { function toHtml() { global $CFG, $COURSE, $PAGE; + require_once($CFG->dirroot.'/repository/lib.php'); if ($this->_flagFrozen) { return $this->getFrozenHtml(); @@ -177,13 +178,25 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $str .= 'Error'; // TODO: localise, fix styles, etc. $str .= ''; - require_once($CFG->dirroot.'/repository/lib.php'); $client_id = uniqid(); - $repojs = repository_get_client($ctx, $client_id, array('image', 'video', 'media'), (FILE_EXTERNAL | FILE_INTERNAL)); - $str .= $repojs; - $str .= $PAGE->requires->js_function_call('id2_add_clientid', array($id, $client_id))->asap(); - $str .= $PAGE->requires->js_function_call('id2_add_itemid', array($id, $draftitemid))->asap(); + $args = new stdclass; + // need these three to filter repositories list + $args->accepted_types = array('image', 'video', 'media'); + $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL); + $args->context = $ctx; + $args->env = 'filepicker'; + + $options = initialise_filepicker($args); + + $options->client_id = $client_id; + $options->maxbytes = $this->_options['maxbytes']; + $options->maxfiles = 1; + $options->env = 'editor'; + + //$PAGE->requires->js_function_call('id2_add_clientid', array($id, $client_id))->on_dom_ready(); + //$PAGE->requires->js_function_call('id2_add_itemid', array($id, $draftitemid))->on_dom_ready(); + $PAGE->requires->js_function_call('editor_init_filepicker', array($id, $options))->on_dom_ready(); if ($editor->supports_repositories()) { $str .= $PAGE->requires->js_function_call('destroy_item', array("{$id}_filemanager"))->asap(); diff --git a/lib/form/filemanager.js b/lib/form/filemanager.js index d62752e6358..c2f73e90e86 100644 --- a/lib/form/filemanager.js +++ b/lib/form/filemanager.js @@ -88,6 +88,23 @@ function filemanager_callback(params) { } } +var fm_filepickers = {}; +function fp_filepicker_callback(params) { + var html = ''+params['title']+''; + document.getElementById('file_info_'+params['client_id']).innerHTML = html; +} + +function fm_init_filepicker(id, options) { + Y.use("filepicker", function (Y) { + options.formcallback = filemanager_callback; + if (!pickers[options.client_id]) { + fm_filepickers[options.client_id] = new Y.filepicker(options); + } + Y.one('#'+id).on('click', function(e, client_id) { + fm_filepickers[options.client_id].show(); + }, this, options.client_id); + }); +} /** * Setup options to launch file picker. * Fired by add file button. diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index 5bb90bf99c5..92735d0b0dc 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -27,19 +27,14 @@ global $CFG; require_once('HTML/QuickForm/element.php'); require_once($CFG->dirroot.'/lib/filelib.php'); +require_once("$CFG->dirroot/repository/lib.php"); class MoodleQuickForm_filemanager extends HTML_QuickForm_element { public $_helpbutton = ''; - protected $_options = array('mainfile'=>'', 'subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>-1, 'filetypes'=>'*', 'returntypes'=>FILE_INTERNAL); + protected $_options = array('mainfile'=>'', 'subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL); function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) { global $CFG, $PAGE; - require_once("$CFG->dirroot/repository/lib.php"); - - // has to require these js files before head - $PAGE->requires->yui2_lib('menu'); - $PAGE->requires->yui2_lib('connection'); - $PAGE->requires->yui2_lib('json'); $options = (array)$options; foreach ($options as $name=>$value) { @@ -51,8 +46,8 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']); } parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); - - repository_head_setup(); + $PAGE->requires->js('repository/filepicker.js'); + $PAGE->requires->js('lib/form/filemanager.js'); } function setName($name) { @@ -130,12 +125,14 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $subdirs = $this->_options['subdirs']; $maxbytes = $this->_options['maxbytes']; $draftitemid = $this->getValue(); + $accepted_types = $this->_options['accepted_types']; // language strings $straddfile = get_string('add', 'repository') . '...'; $strmakedir = get_string('makeafolder', 'moodle'); $strdownload = get_string('downloadfolder', 'repository'); + $PAGE->requires->string_for_js('loading', 'repository'); $PAGE->requires->string_for_js('nomorefiles', 'repository'); $PAGE->requires->string_for_js('confirmdeletefile', 'repository'); @@ -168,17 +165,22 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $draftareainfo = file_get_draft_area_info($draftitemid); $filecount = $draftareainfo['filecount']; - - if ($COURSE->id == SITEID) { - $context = get_context_instance(CONTEXT_SYSTEM); - } else { - $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); - } - $client_id = uniqid(); + $args = new stdclass; + // need these three to filter repositories list + $args->accepted_types = $accepted_types; + $args->return_types = $this->_options['return_types']; + $args->context = $PAGE->context; + $args->env = 'filemanager'; + + $filepicker_options = initialise_filepicker($args); + + $filepicker_options->client_id = $client_id; + $filepicker_options->maxbytes = $this->_options['maxbytes']; + $filepicker_options->maxfiles = $this->_options['maxfiles']; + // Generate file picker - $repojs = repository_get_client($context, $client_id, $this->_options['filetypes'], $this->_options['returntypes']); $result = new stdclass; $options = file_get_draft_area_files($draftitemid); @@ -192,11 +194,9 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $options->target = $id; $html = $this->_getTabs(); - $html .= $repojs; - if (empty($CFG->filemanagerjsloaded)) { - $PAGE->requires->js('lib/form/filemanager.js'); - $CFG->filemanagerjsloaded = true; + if (empty($CFG->filemanagertemplateloaded)) { + $CFG->filemanagertemplateloaded = true; // print html template $html .= <<