diff options
author | 2012-03-30 17:07:02 -0700 | |
---|---|---|
committer | 2012-03-30 17:07:02 -0700 | |
commit | f6ab6622aab00fe7c2f4c3dc41f786ebbe0f0d73 (patch) | |
tree | 870111038542cd27153e1396ebdc063573249689 /exampleData/trac-new-ticket/New Ticket_files/auto_preview.js |
initial revision
Diffstat (limited to 'exampleData/trac-new-ticket/New Ticket_files/auto_preview.js')
-rw-r--r-- | exampleData/trac-new-ticket/New Ticket_files/auto_preview.js | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/exampleData/trac-new-ticket/New Ticket_files/auto_preview.js b/exampleData/trac-new-ticket/New Ticket_files/auto_preview.js new file mode 100644 index 0000000..5b511dd --- /dev/null +++ b/exampleData/trac-new-ticket/New Ticket_files/auto_preview.js @@ -0,0 +1,156 @@ +// Automatic form submission and preview through XHR + +(function($) { + // Enable automatic submission of forms. + // + // This method can be applied to a single form, where it enables + // auto-submission on all the editable elements that it contains. + // It can also be applied on a list of elements, in which case it + // enables auto-submission only for these elements. + // + // Arguments: + // - `args`: additional form data to be passed with the XHR. + // - `update`: the function that is called with the submission reply. It + // is called with the request data and the reply. + // - `busy`: an object or jQuery selector to be shown while requesting an + // update. + $.fn.autoSubmit = function(args, update, busy) { + if (this.length == 0 || auto_preview_timeout <= 0) + return this; + if (this[0].nodeName == 'FORM') { + var form = this; + var inputs = this.find("textarea, select, :text, :checkbox, :radio"); + } else { + var form = this.closest('form'); + var inputs = this; + } + var timeout = auto_preview_timeout * 1000; + var timer = null; + var updating = false; + var queued = false; + + // Return true iff the values have changed + function values_changed(new_values) { + if (values.length != new_values.length) + return true; + for (var i in values) { + var value = values[i], new_value = new_values[i]; + if ((value.name != new_value.name) || (value.value != new_value.value)) + return true; + } + return false; + } + + // Request a preview through XHR + function request() { + if (!updating) { + var new_values = form.serializeArray(); + if (values_changed(new_values)) { + values = new_values; + updating = true; + if (busy != undefined) + $(busy).show(); + + // Construct request data + var data = values.slice(0); + for (var key in args) + data.push({name: key, value: args[key]}); + + $.ajax({ + type: form.attr('method'), url: form.attr('action'), + data: data, traditional: true, dataType: "html", + success: function(reply) { + if (queued) + timer = setTimeout(request, timeout); + updating = false; + queued = false; + if (busy != undefined) + $(busy).hide(); + update(data, reply); + }, + error: function(req, err, exc) { + updating = false; + queued = false; + if (busy != undefined) + $(busy).hide(); + } + }); + } + } + } + + // Trigger a request after the given timeout + function trigger() { + if (!updating) { + if (timer) + clearTimeout(timer); + timer = setTimeout(request, timeout); + } else { + queued = true; + } + return true; + } + + var values = form.serializeArray(); + return inputs.each(function() { + $(this).keydown(trigger).keypress(trigger).change(trigger).blur(trigger); + }); + }; + + // Enable automatic previewing to <textarea> elements. + // + // Arguments: + // - `href`: URL to be called for fetching the preview data. + // - `args`: arguments to be passed with the XHR. + // - `update`: the function that is called with the preview results. It + // is called with the textarea, the text that was rendered and + // the rendered text. + $.fn.autoPreview = function(href, args, update) { + if (auto_preview_timeout <= 0) + return this; + var timeout = auto_preview_timeout * 1000; + return this.each(function() { + var timer = null; + var updating = false; + var textarea = this; + var data = {}; + for (var key in args) + data[key] = args[key]; + data["__FORM_TOKEN"] = form_token; + data["text"] = textarea.value; + + // Request a preview through XHR + function request() { + var text = textarea.value; + if (!updating && (text != data["text"])) { + updating = true; + data["text"] = text; + $.ajax({ + type: "POST", url: href, data: data, dataType: "html", + success: function(data) { + updating = false; + update(textarea, text, data); + if (textarea.value != text) + timer = setTimeout(request, timeout); + }, + error: function(req, err, exc) { + updating = false; + } + }); + } + } + + // Trigger a request after the given timeout + function trigger() { + if (!updating) { + if (timer) + clearTimeout(timer); + timer = setTimeout(request, timeout); + } + return true; + } + + $(this).keydown(trigger).keypress(trigger).blur(trigger); + }); + }; +})(jQuery); |