From 2ce08f1b320b0cbb8d6942595cba76487aaec564 Mon Sep 17 00:00:00 2001
From: Aaron Wells <aaronw@catalyst.net.nz>
Date: Fri, 28 Jan 2011 11:36:37 +1300
Subject: [PATCH] lib/form: Altering Javascript so that "disabledIf" will disable the "Choose" button for a filepicker

---
 lib/form/filepicker.php  |    3 ++-
 lib/form/form.js         |   13 +++++++++++++
 lib/outputcomponents.php |    1 +
 lib/outputrenderers.php  |    7 ++++++-
 4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/lib/form/filepicker.php b/lib/form/filepicker.php
index caa2009..c5af6e1 100644
--- a/lib/form/filepicker.php
+++ b/lib/form/filepicker.php
@@ -77,13 +77,14 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
         $args->itemid = $draftitemid;
         $args->maxbytes = $this->_options['maxbytes'];
         $args->context = $PAGE->context;
+        $args->buttonname = $elname.'choose';
 
         $html = $this->_getTabs();
         $fp = new file_picker($args);
         $options = $fp->options;
         $options->context = $PAGE->context;
         $html .= $OUTPUT->render($fp);
-        $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" />';
+        $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'));
         $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
diff --git a/lib/form/form.js b/lib/form/form.js
index 42b9316..140a475 100644
--- a/lib/form/form.js
+++ b/lib/form/form.js
@@ -198,12 +198,25 @@ M.form.initFormDependencies = function(Y, formid, dependencies) {
              */
             _disableElement : function(name, disabled) {
                 var els = this.elementsByName(name);
+                var form = this;
                 els.each(function(){
                     if (disabled) {
                         this.setAttribute('disabled', 'disabled');
                     } else {
                         this.removeAttribute('disabled');
                     }
+
+                    // Extra code to disable a filepicker
+                    if (this.getAttribute('class') == 'filepickerhidden'){
+                        var pickerbuttons = form.elementsByName(name + 'choose');
+                        pickerbuttons.each(function(){
+                            if (disabled){
+                                this.setAttribute('disabled','disabled');
+                            } else {
+                                this.removeAttribute('disabled');
+                            }
+                        });
+                    }
                 })
             },
             /**
diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php
index 4cbd05c..d84e2dd 100644
--- a/lib/outputcomponents.php
+++ b/lib/outputcomponents.php
@@ -57,6 +57,7 @@ class file_picker implements renderable {
             'itemid' => 0,
             'maxbytes'=>-1,
             'maxfiles'=>1,
+            'buttonname'=>false,
         );
         foreach ($defaults as $key=>$value) {
             if (empty($options->$key)) {
diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php
index 39a0ac5..339f458 100644
--- a/lib/outputrenderers.php
+++ b/lib/outputrenderers.php
@@ -1967,13 +1967,18 @@ class core_renderer extends renderer_base {
         } else {
             $maxsize = get_string('maxfilesize', 'moodle', display_size($size));
         }
+        if ($options->buttonname){
+            $buttonname = ' name="' . $options->buttonname . '"';
+        } else {
+            $buttonname = '';
+        }
         $html = <<<EOD
 <div class="filemanager-loading mdl-align" id='filepicker-loading-{$client_id}'>
 $icon_progress
 </div>
 <div id="filepicker-wrapper-{$client_id}" class="mdl-left" style="display:none">
     <div>
-        <input type="button" id="filepicker-button-{$client_id}" value="{$straddfile}" />
+        <input type="button" id="filepicker-button-{$client_id}" value="{$straddfile}"{$buttonname}/>
         <span> $maxsize </span>
     </div>
 EOD;
-- 
1.7.1

