~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/tutorial/tutorial.js

  • Committer: mattgiuca
  • Date: 2008-03-15 09:00:15 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:703
tutorial: (Python + Javascript)
    Added JS function "set_saved_status", which can activate and deactivate
    the save button.
    Modifying the text field now creates a Save timer which takes 10 seconds
    to elapse, then auto-saves the field.
    The save button is only enabled if the text has been modified since
    saving.

Show diffs side-by-side

added added

removed removed

Lines of Context:
88
88
 */
89
89
function saveexercise(exerciseid, filename)
90
90
{
 
91
    set_saved_status(exerciseid, filename, "Saving...");
91
92
    /* Get the source code the student is submitting */
92
93
    var exercisediv = document.getElementById(exerciseid);
93
94
    var exercisebox = exercisediv.getElementsByTagName("textarea")[0];
101
102
    var callback = function(xhr)
102
103
        {
103
104
            // XXX Maybe check to see if this worked?
 
105
            set_saved_status(exerciseid, filename, "Saved");
104
106
        }
105
107
    ajax_call(callback, "tutorialservice", "", args, "POST",
106
108
        "multipart/form-data");
107
109
}
108
110
 
 
111
/* savetimers is a dict mapping exerciseIDs to timer IDs.
 
112
 * Its members indicate all exercises that have been modified but not saved.
 
113
 */
 
114
savetimers = {}
 
115
 
 
116
/** Changes whether an exercise is considered "saved" or not.
 
117
 * stat is a string which specifies the status, and also the button text.
 
118
 * If stat == "Save", then it indicates it is NOT saved. This will
 
119
 * enable the "Save" button, and set a timer going which will auto-save
 
120
 * after a set period of time (eg. 30 seconds).
 
121
 * Any other value will disable the "Save" button and disable the timer.
 
122
 * stat should be "Saving..." when the save request is issued, and "Saved"
 
123
 * when the response comes back.
 
124
 */
 
125
function set_saved_status(exerciseid, filename, stat)
 
126
{
 
127
    var timername = "savetimer_" + exerciseid;
 
128
    var button = document.getElementById("savebutton_" + exerciseid);
 
129
    var is_saved = stat != "Save";
 
130
    button.value = stat;
 
131
 
 
132
    /* Disable the timer, if it exists */
 
133
    if (typeof(savetimers[timername]) != "undefined")
 
134
    {
 
135
        clearTimeout(savetimers[timername]);
 
136
        savetimers[timername] = undefined;
 
137
    }
 
138
 
 
139
    if (is_saved)
 
140
    {
 
141
        /* Disable the button */
 
142
        button.disabled = true;
 
143
    }
 
144
    else
 
145
    {
 
146
        /* Enable the button */
 
147
        button.disabled = false;
 
148
        /* Create a timer which will auto-save when it expires */
 
149
        var save_string = "saveexercise(" + repr(exerciseid) + ", "
 
150
            + repr(filename) + ")"
 
151
        savetimers[timername] = setTimeout(save_string, 10000);
 
152
    }
 
153
}
 
154
 
109
155
/** Given a exercise div, return the testoutput div which is its child.
110
156
 * (The div which is its child whose class is "testoutput".
111
157
 */