Skip to content

Commit

Permalink
MDL-25937 Forms Library: Filepicker will respect disable if
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Nov 3, 2011
1 parent b2595cb commit a5b30b0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
19 changes: 17 additions & 2 deletions lib/form/filepicker.js
@@ -1,19 +1,34 @@


M.form_filepicker = {}; M.form_filepicker = {};

M.form_filepicker.Y = null;
M.form_filepicker.instances = [];


M.form_filepicker.callback = function(params) { M.form_filepicker.callback = function(params) {
var html = '<a href="'+params['url']+'">'+params['file']+'</a>'; var html = '<a href="'+params['url']+'">'+params['file']+'</a>';
document.getElementById('file_info_'+params['client_id']).innerHTML = html; document.getElementById('file_info_'+params['client_id']).innerHTML = html;
//When file is added then set status of global variable to true
var elementname = M.core_filepicker.instances[params['client_id']].options.elementname;
M.form_filepicker.instances[elementname].fileadded = true;
//generate event to indicate changes which will be used by disable if or validation code
M.form_filepicker.Y.one('#id_'+elementname).simulate('change');
}; };


/** /**
* This fucntion is called for each file picker on page. * This fucntion is called for each file picker on page.
*/ */
M.form_filepicker.init = function(Y, options) { M.form_filepicker.init = function(Y, options) {
//Keep reference of YUI, so that it can be used in callback.
M.form_filepicker.Y = Y;

//For client side validation, initialize file status for this filepicker
M.form_filepicker.instances[options.elementname] = {};
M.form_filepicker.instances[options.elementname].fileadded = false;

//Set filepicker callback
options.formcallback = M.form_filepicker.callback; options.formcallback = M.form_filepicker.callback;

if (!M.core_filepicker.instances[options.client_id]) { if (!M.core_filepicker.instances[options.client_id]) {
M.core_filepicker.init(Y, options); M.core_filepicker.init(Y, options);
} }
Y.on('click', function(e, client_id) { Y.on('click', function(e, client_id) {
e.preventDefault(); e.preventDefault();
Expand Down
3 changes: 2 additions & 1 deletion lib/form/filepicker.php
Expand Up @@ -79,6 +79,7 @@ function toHtml() {
$args->maxbytes = $this->_options['maxbytes']; $args->maxbytes = $this->_options['maxbytes'];
$args->context = $PAGE->context; $args->context = $PAGE->context;
$args->buttonname = $elname.'choose'; $args->buttonname = $elname.'choose';
$args->elementname = $elname;


$html = $this->_getTabs(); $html = $this->_getTabs();
$fp = new file_picker($args); $fp = new file_picker($args);
Expand All @@ -87,7 +88,7 @@ function toHtml() {
$html .= $OUTPUT->render($fp); $html .= $OUTPUT->render($fp);
$html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>'; $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';


$module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker')); $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate'));
$PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module); $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);


$nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array( $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
Expand Down
24 changes: 22 additions & 2 deletions lib/form/form.js
Expand Up @@ -277,7 +277,17 @@ M.form.initFormDependencies = function(Y, formid, dependencies) {
} else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) { } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
return; return;
} }
lock = lock || this.get('value') == value; //check for filepicker status
if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
var elementname = this.getAttribute('name');
if (elementname && M.form_filepicker.instances[elementname].fileadded) {
lock = false;
} else {
lock = true;
}
} else {
lock = lock || this.get('value') == value;
}
}); });
return { return {
lock : lock, lock : lock,
Expand All @@ -298,7 +308,17 @@ M.form.initFormDependencies = function(Y, formid, dependencies) {
} else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) { } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
return; return;
} }
lock = lock || this.get('value') != value; //check for filepicker status
if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
var elementname = this.getAttribute('name');
if (elementname && M.form_filepicker.instances[elementname].fileadded) {
lock = true;
} else {
lock = false;
}
} else {
lock = lock || this.get('value') != value;
}
}); });
return { return {
lock : lock, lock : lock,
Expand Down
2 changes: 1 addition & 1 deletion lib/formslib.php
Expand Up @@ -2340,7 +2340,7 @@ function finishForm(&$form){
if (!$form->isFrozen()) { if (!$form->isFrozen()) {
$args = $form->getLockOptionObject(); $args = $form->getLockOptionObject();
if (count($args[1]) > 0) { if (count($args[1]) > 0) {
$PAGE->requires->js_init_call('M.form.initFormDependencies', $args, false, moodleform::get_js_module()); $PAGE->requires->js_init_call('M.form.initFormDependencies', $args, true, moodleform::get_js_module());
} }
} }
} }
Expand Down

0 comments on commit a5b30b0

Please sign in to comment.