]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19797 Added callback support to the confirm_dialog function, and to the add_confi...
authornicolasconnault <nicolasconnault>
Thu, 27 Aug 2009 10:12:44 +0000 (10:12 +0000)
committernicolasconnault <nicolasconnault>
Thu, 27 Aug 2009 10:12:44 +0000 (10:12 +0000)
lib/javascript-static.js
lib/outputcomponents.php

index 39584bcf58b41ba5af438a67819700979bbd2bbe..4ba42d1d43bc441b0a4cff26d0129c6361cbc6d2 100644 (file)
@@ -208,7 +208,7 @@ function lockoptionsallsetup(formid) {
 /**
  * Helper function mainly for drop-down menus' onchange events,
  * submits the form designated by args.id. If args.selectid is also
- * given, it only submits the form if the selected <option> is not 
+ * given, it only submits the form if the selected <option> is not
  * the first one (usually the "Choose..." option)
  * Example usage of the html_select component with this function:
  * <pre>
@@ -1215,6 +1215,7 @@ function init_help_icons() {
 function confirm_dialog(event, args) {
     var message = args.message;
     var target = this;
+    target.args = args;
     YAHOO.util.Event.preventDefault(event);
 
     var simpledialog = new YAHOO.widget.SimpleDialog('confirmdialog',
@@ -1230,13 +1231,19 @@ function confirm_dialog(event, args) {
     simpledialog.setBody(message);
     simpledialog.cfg.setProperty('icon', YAHOO.widget.SimpleDialog.ICON_WARN);
 
-    var handle_cancel = function() {
+    this.handle_cancel = function() {
         this.hide();
     };
 
-    var handle_yes = function() {
+    this.handle_yes = function() {
         this.hide();
-        
+
+        if (target.args.callback) {
+            // args comes from PHP, so callback will be a string, needs to be evaluated by JS
+            var callback = eval('('+target.args.callback+')');
+            callback.apply(this);
+        }
+
         if (target.tagName.toLowerCase() == 'a') {
             window.location = target.href;
         } else if (target.tagName.toLowerCase() == 'input') {
@@ -1252,11 +1259,17 @@ function confirm_dialog(event, args) {
         }
     };
 
-    var buttons = [ { text: mstr.moodle.cancel, handler: handle_cancel, isDefault: true },
-                    { text: mstr.moodle.yes, handler: handle_yes } ];
+    var buttons = [ { text: mstr.moodle.cancel, handler: this.handle_cancel, isDefault: true },
+                    { text: mstr.moodle.yes, handler: this.handle_yes } ];
 
     simpledialog.cfg.queueProperty('buttons', buttons);
 
     simpledialog.render(document.body);
     simpledialog.show();
+    return simpledialog;
+}
+
+function dialog_callback() {
+    console.debug(this);
+    console.debug(this.args);
 }
index d5f6f42f12856464f29116abf298d42bffd84672..d3acbf4b332b1d7efdae6f5f8e76c91e6db0c608 100644 (file)
@@ -193,10 +193,12 @@ class moodle_html_component {
      * Shortcut for adding a JS confirm dialog when the component is clicked.
      * The message must be a yes/no question.
      * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
+     * @param string $callback The name of a JS function whose scope will be set to the simpleDialog object and have this 
+     *    function's arguments set as this.args.
      * @return void
      */
-    public function add_confirm_action($message) {
-        $this->add_action(new component_action('click', 'confirm_dialog', array('message' => $message)));
+    public function add_confirm_action($message, $callback=null) {
+        $this->add_action(new component_action('click', 'confirm_dialog', array('message' => $message, 'callback' => $callback)));
     }
 
     /**