2010-03-01 14 views
1

J'ai une fonction JavaScript qui dessine un dialogue. Je voudrais qu'il renvoie la valeur spécifiée par l'utilisateur. Le problème est que la boîte de dialogue est fermée lorsqu'un utilisateur clique sur deux boutons auxquels sont affectés des événements onClick. La seule façon que je connaisse pour saisir ces événements est de leur assigner des fonctions, ce qui signifie qu'un retour provoque le retour de la fonction assignée, et non ma fonction inputDialog. Je suis sûr que je fais juste ça d'une manière stupide. Au cas où vous vous poseriez la question, ce script utilise l'API ExtendScript d'Adobe pour étendre After Effects.Retour de dialogue JavaScript

Voici le code:

function inputDialog (queryString, title){ 
    // Create a window of type dialog. 
    var dia = new Window("dialog", title, [100,100,330,200]); // bounds = [left, top, right, bottom] 
    this.windowRef = dia; 

    // Add the components, a label, two buttons and input 
    dia.label = dia.add("statictext", [20, 10, 210, 30]); 
    dia.label.text = queryString; 
    dia.input = dia.add("edittext", [20, 30, 210, 50]); 
    dia.input.textselection = "New Selection"; 
    dia.input.active = true; 
    dia.okBtn = dia.add("button", [20,65,105,85], "OK"); 
    dia.cancelBtn = dia.add("button", [120, 65, 210, 85], "Cancel"); 


    // Register event listeners that define the button behavior 

    //user clicked OK 
    dia.okBtn.onClick = function() { 
     if(dia.input.text != "") { //check that the text input wasn't empty 
      var result = dia.input.text; 
      dia.close(); //close the window 
      if(debug) alert(result); 
      return result; 
     } else { //the text box is blank 
      alert("Please enter a value."); //don't close the window, ask the user to enter something 
     } 
    }; 

    //user clicked Cancel 
    dia.cancelBtn.onClick = function() { 
     dia.close(); 
     if(debug) alert("dialog cancelled"); 
     return false; 
    }; 

    // Display the window 
    dia.show(); 

} 

Répondre

0

Je suis venu avec une solution. C'est vraiment moche, mais ça va me dépasser pour l'instant ... Quelqu'un a une meilleure solution?

var ret = null; 

    // Register event listeners that define the button behavior 
    dia.okBtn.onClick = function() { 
     if(dia.input.text != "") { //check that the text input wasn't empty 
      var result = dia.input.text; 
      dia.close(); //close the window 
      if(debug) alert(result); 
      ret = result; 
      return result; 
     } else { //the text box is blank 
      alert("Please enter a value."); //don't close the window, ask the user to enter something 
     } 
    }; 

    dia.cancelBtn.onClick = function() { //user cancelled action 
     dia.close(); 
     ret = false; 
     return false; 
    }; 

    // Display the window 
    dia.show(); 

    while(ret == null){}; 
    return ret;