📄 ajax.php
字号:
$options . ");"); }/** * Creates an Ajax-updateable DIV element * * @param string $id options for javascript * @return string HTML code */ function div($id, $options = array()) { if (env('HTTP_X_UPDATE') != null) { $this->Javascript->enabled = false; $divs = explode(' ', env('HTTP_X_UPDATE')); if (in_array($id, $divs)) { @ob_end_clean(); ob_start(); return ''; } } $attr = $this->_parseAttributes(array_merge($options, array('id' => $id))); return $this->output(sprintf($this->Html->tags['blockstart'], $attr)); }/** * Closes an Ajax-updateable DIV element * * @param string $id The DOM ID of the element * @return string HTML code */ function divEnd($id) { if (env('HTTP_X_UPDATE') != null) { $divs = explode(' ', env('HTTP_X_UPDATE')); if (in_array($id, $divs)) { $this->__ajaxBuffer[$id] = ob_get_contents(); ob_end_clean(); ob_start(); return ''; } } return $this->output($this->Html->tags['blockend']); }/** * Detects Ajax requests * * @return boolean True if the current request is a Prototype Ajax update call */ function isAjax() { return (isset($this->params['isAjax']) && $this->params['isAjax'] === true); }/** * Creates a draggable element. For a reference on the options for this function, * check out http://wiki.script.aculo.us/scriptaculous/show/Draggable * * @param unknown_type $id * @param array $options * @return unknown */ function drag($id, $options = array()) { $var = ''; if (isset($options['var'])) { $var = 'var ' . $options['var'] . ' = '; unset($options['var']); } $options = $this->_buildOptions($this->_optionsToString($options, array('handle', 'constraint')), $this->dragOptions); return $this->Javascript->codeBlock("{$var}new Draggable('$id', " .$options . ");"); }/** * For a reference on the options for this function, check out * http://wiki.script.aculo.us/scriptaculous/show/Droppables.add * * @param unknown_type $id * @param array $options * @return string */ function drop($id, $options = array()) { $optionsString = array('overlap', 'hoverclass'); if (!isset($options['accept']) || !is_array($options['accept'])) { $optionsString[] = 'accept'; } else if (isset($options['accept'])) { $options['accept'] = $this->Javascript->object($options['accept']); } $options = $this->_buildOptions($this->_optionsToString($options, $optionsString), $this->dropOptions); return $this->Javascript->codeBlock("Droppables.add('{$id}', {$options});"); }/** * Make an element with the given $id droppable, and trigger an Ajax call when a draggable is * dropped on it. * * For a reference on the options for this function, check out * http://wiki.script.aculo.us/scriptaculous/show/Droppables.add * * @param string $id * @param array $options * @param array $ajaxOptions * @return string JavaScript block to create a droppable element */ function dropRemote($id, $options = array(), $ajaxOptions = array()) { $options['onDrop'] = "function(element, droppable, event) {" . $this->remoteFunction($ajaxOptions) . "}"; $optionsString = array('overlap', 'hoverclass'); if (!isset($options['accept']) || !is_array($options['accept'])) { $optionsString[] = 'accept'; } else if (isset($options['accept'])) { $options['accept'] = $this->Javascript->object($options['accept']); } $options = $this->_buildOptions($this->_optionsToString($options, $optionsString), $this->dropOptions); return $this->Javascript->codeBlock("Droppables.add('{$id}', {$options});"); }/** * Makes a slider control. * * @param string $id DOM ID of slider handle * @param string $trackId DOM ID of slider track * @param array $options Array of options to control the slider * @link http://wiki.script.aculo.us/scriptaculous/show/Slider */ function slider($id, $trackId, $options = array()) { if (isset($options['var'])) { $var = 'var ' . $options['var'] . ' = '; unset($options['var']); } else { $var = 'var ' . $id . ' = '; } $options = $this->_optionsToString($options, array('axis', 'handleImage', 'handleDisabled')); $callbacks = array('change', 'slide'); foreach($callbacks as $callback) { if (isset($options[$callback])) { $options['on' . ucfirst($callback)] = 'function(value) {' . $options[$callback] . '}'; unset($options[$callback]); } } if (isset($options['values']) && is_array($options['values'])) { $options['values'] = $this->Javascript->object($options['values']); } $options = $this->_buildOptions($options, $this->sliderOptions); return $this->Javascript->codeBlock("{$var}new Control.Slider('$id', '$trackId', $options);"); }/** * Makes an Ajax In Place editor control. * * @param string $id DOM ID of input element * @param string $url Postback URL of saved data * @param array $options Array of options to control the editor, including ajaxOptions (see link). * @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor */ function editor($id, $url, $options = array()) { $url = $this->url($url); $options['ajaxOptions'] = $this->__optionsForAjax($options); foreach ($this->ajaxOptions as $opt) { if (isset($options[$opt])) { unset($options[$opt]); } } if (isset($options['callback'])) { $options['callback'] = 'function(form, value) {' . $options['callback'] . '}'; } $type = 'InPlaceEditor'; if (isset($options['collection']) && is_array($options['collection'])) { $options['collection'] = $this->Javascript->object($options['collection']); $type = 'InPlaceCollectionEditor'; } $var = ''; if (isset($options['var'])) { $var = 'var ' . $options['var'] . ' = '; unset($options['var']); } $options = $this->_optionsToString($options, array('okText', 'cancelText', 'savingText', 'formId', 'externalControl', 'highlightcolor', 'highlightendcolor', 'savingClassName', 'formClassName', 'loadTextURL', 'loadingText', 'clickToEditText', 'okButton', 'cancelLink')); $options = $this->_buildOptions($options, $this->editorOptions); return $this->Javascript->codeBlock("{$var}new Ajax.{$type}('{$id}', '{$url}', {$options});"); }/** * Makes a list or group of floated objects sortable. * * @param string $id DOM ID of parent * @param array $options Array of options to control sort.http://wiki.script.aculo.us/scriptaculous/show/Sortable.create * @link http://wiki.script.aculo.us/scriptaculous/show/Sortable.create */ function sortable($id, $options = array()) { if (!empty($options['url'])) { if (empty($options['with'])) { $options['with'] = "Sortable.serialize('$id')"; } $options['onUpdate'] = 'function(sortable) {' . $this->remoteFunction($options) . '}'; } $block = true; if (isset($options['block'])) { $block = $options['block']; unset($options['block']); } $strings = array('tag', 'constraint', 'only', 'handle', 'hoverclass', 'tree', 'treeTag', 'update', 'overlap'); if (isset($options['scroll']) && $options['scroll'] != 'window' && strpos($options['scroll'], '$(') !== 0) { $strings[] = 'scroll'; } $options = $this->_optionsToString($options, $strings); $options = array_merge($options, $this->_buildCallbacks($options)); $options = $this->_buildOptions($options, $this->sortOptions); $result = "Sortable.create('$id', $options);"; if (!$block) { return $result; } return $this->Javascript->codeBlock($result); }/** * Private helper function for Javascript. * * @param array $options Set of options * @access private */ function __optionsForAjax($options) { if (isset($options['indicator'])) { if (isset($options['loading'])) { if (!empty($options['loading']) && substr(trim($options['loading']), -1, 1) != ';') { $options['loading'] .= '; '; } $options['loading'] .= "Element.show('{$options['indicator']}');"; } else { $options['loading'] = "Element.show('{$options['indicator']}');"; } if (isset($options['complete'])) { if (!empty($options['complete']) && substr(trim($options['complete']), -1, 1) != ';') { $options['complete'] .= '; '; } $options['complete'] .= "Element.hide('{$options['indicator']}');"; } else { $options['complete'] = "Element.hide('{$options['indicator']}');"; } unset($options['indicator']); } $jsOptions = array_merge( array('asynchronous' => 'true', 'evalScripts' => 'true'), $this->_buildCallbacks($options) ); $options = $this->_optionsToString($options, array('contentType', 'encoding', 'fallback', 'method', 'postBody', 'update', 'url')); $jsOptions = array_merge($jsOptions, array_intersect_key($options, array_flip(array('contentType', 'encoding', 'method', 'postBody')))); foreach ($options as $key => $value) { switch($key) { case 'type': $jsOptions['asynchronous'] = ife(($value == 'synchronous'), 'false', 'true'); break; case 'evalScripts': $jsOptions['evalScripts'] = ife($value, 'true', 'false'); break; case 'position': $jsOptions['insertion'] = "Insertion." . Inflector::camelize($options['position']); break; case 'with': $jsOptions['parameters'] = $options['with']; break; case 'form': $jsOptions['parameters'] = 'Form.serialize(this)'; break; case 'requestHeaders': $keys = array(); foreach ($value as $key => $val) { $keys[] = "'" . $key . "'"; $keys[] = "'" . $val . "'"; } $jsOptions['requestHeaders'] = '[' . join(', ', $keys) . ']'; break; } } return $this->_buildOptions($jsOptions, $this->ajaxOptions); }/** * Private Method to return a string of html options * option data as a JavaScript options hash. * * @param array $options Options in the shape of keys and values * @param array $extra Array of legal keys in this options context * @return array Array of html options * @access private */ function __getHtmlOptions($options, $extra = array()) { foreach (array_merge($this->ajaxOptions, $this->callbacks, $extra) as $key) { if (isset($options[$key])) { unset($options[$key]); } } return $options; }/** * Returns a string of JavaScript with the given option data as a JavaScript options hash. * * @param array $options Options in the shape of keys and values * @param array $acceptable Array of legal keys in this options context * @return string String of Javascript array definition */ function _buildOptions($options, $acceptable) { if (is_array($options)) { $out = array(); foreach ($options as $k => $v) { if (in_array($k, $acceptable)) { if ($v === true) { $v = 'true'; } elseif ($v === false) { $v = 'false'; } $out[] = "$k:$v"; } } $out = join(', ', $out); $out = '{' . $out . '}'; return $out; } else { return false; } }/** * Return JavaScript text for an observer... * * @param string $klass Name of JavaScript class * @param string $name * @param array $options Ajax options * @return string Formatted JavaScript */ function _buildObserver($klass, $name, $options = null) { if (!isset($options['with']) && isset($options['update'])) { $options['with'] = 'value'; } $callback = $this->remoteFunction($options); $javascript = "new $klass('$name', "; $javascript .= (!isset($options['frequency']) || intval($options['frequency']) == 0 ? '' : $options['frequency'] . ', ' ) . "function(element, value) {"; $javascript .= "$callback})"; return $javascript; }/** * Return Javascript text for callbacks. * * @param array $options Option array where a callback is specified * @return array Options with their callbacks properly set * @access protected */ function _buildCallbacks($options) { $callbacks = array(); foreach ($this->callbacks as $callback) { if (isset($options[$callback])) { $name = 'on' . ucfirst($callback); $code = $options[$callback]; switch($name) { case 'onComplete': $callbacks[$name] = "function(request, json) {" . $code . "}"; break; case 'onCreate': $callbacks[$name] = "function(request, xhr) {" . $code . "}"; break; case 'onException': $callbacks[$name] = "function(request, exception) {" . $code . "}"; break; default: $callbacks[$name] = "function(request) {" . $code . "}"; break; } if (isset($options['bind'])) { if ((is_array($options['bind']) && in_array($callback, $options['bind'])) || (is_string($options['bind']) && strpos($options['bind'], $callback) !== false)) { $callbacks[$name] .= ".bind(this)"; } } } } return $callbacks; }/** * Returns a string of JavaScript with a string representation of given options array. * * @param array $options Ajax options array * @param array $stringOpts Options as strings in an array * @access private * @return array */ function _optionsToString($options, $stringOpts = array()) { foreach ($stringOpts as $option) { if (isset($options[$option]) && !empty($options[$option]) && is_string($options[$option]) && $options[$option][0] != "'") { if ($options[$option] === true || $options[$option] === 'true') { $options[$option] = 'true'; } elseif ($options[$option] === false || $options[$option] === 'false') { $options[$option] = 'false'; } else { $options[$option] = "'{$options[$option]}'"; } } } return $options; }/** * Executed after a view has rendered, used to include bufferred code * blocks. * * @access public */ function afterRender() { if (env('HTTP_X_UPDATE') != null && !empty($this->__ajaxBuffer)) { @ob_end_clean(); $data = array(); $divs = explode(' ', env('HTTP_X_UPDATE')); $keys = array_keys($this->__ajaxBuffer); if (count($divs) == 1 && in_array($divs[0], $keys)) { e($this->__ajaxBuffer[$divs[0]]); } else { foreach ($this->__ajaxBuffer as $key => $val) { if (in_array($key, $divs)) { $data[] = $key . ':"' . rawurlencode($val) . '"'; } } $out = 'var __ajaxUpdater__ = {' . join(", \n", $data) . '};' . "\n"; $out .= 'for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string" && $(n)) Element.update($(n), unescape(decodeURIComponent(__ajaxUpdater__[n]))); }'; e($this->Javascript->codeBlock($out, false)); } $scripts = $this->Javascript->getCache(); if (!empty($scripts)) { e($this->Javascript->codeBlock($scripts, false)); } $this->_stop(); } }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -