summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2005-08-31 18:37:30 +0000
committerDries Buytaert <dries@buytaert.net>2005-08-31 18:37:30 +0000
commite03ce2f99670b75a7f3e0709dd8705a2a3f4625e (patch)
treea7678fa8a55ccd2927cbbc8d0e30bbacf630ad3d /includes
parent3029da00d62c20a4d97d668931bf9f0c918d1b09 (diff)
downloadbrdo-e03ce2f99670b75a7f3e0709dd8705a2a3f4625e.tar.gz
brdo-e03ce2f99670b75a7f3e0709dd8705a2a3f4625e.tar.bz2
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note: * The feature degrades back to the current behaviour without JS. * If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset. * Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use. * I included some minor improvements to the Drupal JS API and code. * I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS. * I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
Diffstat (limited to 'includes')
-rw-r--r--includes/common.inc55
1 files changed, 52 insertions, 3 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 7b6fe29da..2a098aa7c 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -1461,6 +1461,10 @@ function form_file($title, $name, $size, $description = NULL, $required = FALSE)
* The internal name used to refer to the field.
* @param $value
* The stored data.
+ * @param $edit
+ * The array name to prefix to the $name.
+ * @param $attributes
+ * An array of HTML attributes for the input tag.
* @return
* A themed HTML string representing the hidden field.
*
@@ -1468,8 +1472,8 @@ function form_file($title, $name, $size, $description = NULL, $required = FALSE)
* but be sure to validate the data on the receiving page as it is possible for
* an attacker to change the value before it is submitted.
*/
-function form_hidden($name, $value, $edit = 'edit') {
- return '<input type="hidden" name="'. $edit .'['. $name .']" value="'. check_plain($value) ."\" />\n";
+function form_hidden($name, $value, $edit = 'edit', $attributes = NULL) {
+ return '<input type="hidden" name="'. $edit .'['. $name .']" id="'. form_clean_id($edit .'-'. $name) .'" value="'. check_plain($value) .'"'. drupal_attributes($attributes) ." />\n";
}
/**
@@ -1488,7 +1492,7 @@ function form_hidden($name, $value, $edit = 'edit') {
* A themed HTML string representing the button.
*/
function form_button($value, $name = 'op', $type = 'submit', $attributes = NULL) {
- return '<input type="'. $type .'" class="form-'. $type .'" name="'. $name .'" value="'. check_plain($value) .'" '. drupal_attributes($attributes) ." />\n";
+ return '<input type="'. $type .'" class="form-'. $type .'" name="'. $name .'" id="'. form_clean_id($name) .'" value="'. check_plain($value) .'" '. drupal_attributes($attributes) ." />\n";
}
/**
@@ -1806,6 +1810,51 @@ function drupal_add_js($file) {
}
/**
+ * Generates a Javascript call, while importing the arguments as is.
+ * PHP arrays are turned into JS objects to preserve keys. This means the array
+ * keys must conform to JS's member naming rules.
+ *
+ * @param $function
+ * The name of the function to call.
+ * @param $arguments
+ * An array of arguments.
+ */
+function drupal_call_js($function) {
+ $arguments = func_get_args();
+ array_shift($arguments);
+ $args = array();
+ foreach ($arguments as $arg) {
+ $args[] = drupal_to_js($arg);
+ }
+ $output = '<script type="text/javascript">'. $function .'('. implode(', ', $args) .');</script>';
+ return $output;
+}
+
+/**
+ * Converts a PHP variable into its Javascript equivalent.
+ */
+function drupal_to_js($var) {
+ switch (gettype($var)) {
+ case 'boolean':
+ case 'integer':
+ case 'double':
+ return $var;
+ case 'resource':
+ case 'string':
+ return '"'. str_replace(array("\r", "\n"), array('\r', '\n'), addslashes($var)) .'"';
+ case 'array':
+ case 'object':
+ $output = array();
+ foreach ($var as $k => $v) {
+ $output[] = $k .': '. drupal_to_js($v);
+ }
+ return '{ '. implode(', ', $output) .' }';
+ default:
+ return 'null';
+ }
+}
+
+/**
* Implode a PHP array into a string that can be decoded by the autocomplete JS routines.
*
* Items are separated by double pipes. Each item consists of a key-value pair