<html> <body> <?php //ini_set('include_path','.:/data/peartest/share/pear'); //ini_set('include_path','.:/data/pear/cvsroot/pear:/data/pear/cvsroot/php4/pear'); require_once 'HTML/Javascript.php'; require_once 'HTML/Javascript/Convert.php'; // ,array(741,742,743) $test = array("foo1" => "this \\ is a string test &\" and 'nothing' must failed", "foo2" => 2, "foo3" => 3, "foo4" => 4, "foo5" => 5, "foo6" => 6, "foo7" => array(71, 72, 73, "foo74" => array(741, 742, 743))); $js = new HTML_Javascript(); echo $js->startScript(); echo HTML_Javascript_Convert::convertArray($test, 'arTest', true); echo HTML_Javascript::alert('toto'); echo HTML_Javascript::prompt('toto', 'toto'); echo $js->confirm('sure', 'Are you sure?!'); echo $js->popup('win', './test.html', 'window', 200, 200, false); echo $js->popupWrite('win2', 'Foo? Bar!', 'window2222222222222222222222', 200, 200, true); echo $js->endScript(); ?> <script> function interrogate(what) { var output = ''; for (var i in what){ output += i+ " "; } alert(output); } </script>
/** * Converts a PHP array into a JS array * supports of multu-dimensional array. * Keeps keys as they are (associative arrays). * * @access public * @param string $arr the array to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @param int $level Not public, used for recursive calls * @return mixed a PEAR_Error if no script was started * or the converted array */ function convertArray($arr, $varname, $global = false, $level = 0) { $var = ''; if ($global) { $var = 'var '; } if (is_array($arr)) { $length = sizeof($arr); $var .= $varname . ' = Array(' . $length . ')' . HTML_JAVASCRIPT_NL; foreach ($arr as $key => $cell) { $jskey = '"' . $key . '"'; if (is_array($cell)) { $level++; $var .= HTML_Javascript_Convert::convertArray($cell, 'tmp' . $level, $global, $level); $var .= $varname . "[{$jskey}] = tmp{$level}" . HTML_JAVASCRIPT_NL; $var .= "tmp{$level} = null" . HTML_JAVASCRIPT_NL; } else { $value = is_string($cell) ? '"' . HTML_Javascript_Convert::escapeString($cell) . '"' : $cell; $var .= $varname . "[{$jskey}] = {$value}" . HTML_JAVASCRIPT_NL; } } return $var; } else { return HTML_Javascript::raiseError(HTML_JAVASCRIPT_ERROR_INVVAR); } }
/** * Creates a new popup window containing a string. Inside the popup windows * you can access the opener window with the opener var. * * @param string $assign the JS variable to assign the window to * @param string $str the string that will appear in the new window * (HTML tags would be parsed by the browser, of course) * @param string $title the title of the window * @param int $width the width of the window * @param int $height the height of the window * @param mixed $attr see popup() * @param int $top distance from the top (in pixels * @param int $left distance from the left (in pixels) * @see popup() * @return the processed string */ function popupWrite($assign, $str, $title, $width, $height, $attr, $top = 300, $left = 300) { static $cnt_popup; $str = HTML_Javascript_Convert::escapeString($str); $assign = strlen($assign) == 0 ? 'pearpopup' . $cnt_popup++ : $assign; if ($attr) { $attr = array('yes', 'yes', 'yes', 'yes', 'yes', 'yes', $top, $left); } else { $attr = array('no', 'no', 'no', 'no', 'no', 'no', $top, $height); } $windows = $assign . "= window.open(\"\"," . " \"{$title}\"," . " \"width={$width}, height={$height}," . " resizable={$attr['0']}, scrollbars={$attr['1']}," . " menubar={$attr['2']}, toolbar={$attr['3']}," . " status={$attr['4']}, location={$attr['5']}," . " top={$attr['6']}, left={$attr['7']}\")" . HTML_JAVASCRIPT_NL; $windows .= "\n if ({$assign}){\n {$assign}.focus();\n {$assign}.document.open();\n {$assign}.document.write('{$str}');\n {$assign}.document.close();\n if ({$assign}.opener == null) {$assign}.opener = self;\n }\n "; $ret = HTML_Javascript::_out($windows); return $ret; }
<?php /** * A file intended for testing output direction in the HTML_Javascript class */ require_once 'HTML/Javascript.php'; $js = new HTML_Javascript(); $js->setOutputMode(HTML_JAVASCRIPT_OUTPUT_FILE, '/tmp/out.js'); $js->prompt('Foo?', 'foo', 'Bar!'); $js->alert('Muhahaha!');
</head> <body> <h1>HTML_Javascript</h1> <?php /** * HTML_Javascript::Javascript Sample usage * * Show how to use the base of HTML_Javascript * * @author Pierre-Alain Joye <*****@*****.**> * @package HTML_Javascript * @filesource */ /** requires the main class */ require_once 'HTML/Javascript.php'; $htmljs = new HTML_Javascript(); // Starts the JS script echo $htmljs->startScript(); echo $htmljs->writeLine('<h2>document.write</h2>'); // document.write methods /* * Simple usage of write and writeLine * See the confirm, prompt examples to see these methods * usage with JS variables. */ echo $htmljs->writeLine('writeln: Test JS Line 1', false); echo $htmljs->write('write: Test JS Line 2', false); echo $htmljs->write('write: Test JS still Line 2', false); echo $htmljs->writeLine('<h2>Interaction with the users, prompt, alert and confirm</h2>'); echo $htmljs->alert('I will ask you three questions and write back the answers.'); // alert, confirm and prompt methods
protected function _resolve() { $match = $this->_view_manager->head->getElementsBy('tag', 'scripts'); if (empty($match)) { throw new Template_Invalid_Structure_Exception('scripts tag missing in head template'); } $scripts = $match[0]; $required = array('id', 'lat', 'lng'); foreach ($required as $attr) { if ($this->{$attr} === null) { throw new Data_Insufficient_Exception($attr); } } $markers_js = ''; if ($this->marker) { if ($this->mlat === null) { throw new Data_Insufficient_Exception('mlat'); } if ($this->mlng === null) { throw new Data_Insufficient_Exception('mlng'); } $marker_ids = explode(';', $this->marker); $marker_lats = explode(';', $this->mlat); $marker_lngs = explode(';', $this->mlng); $marker_icons = explode(';', $this->micon); if ($this->mtitle) { $marker_titles = explode($this->titles_separator, $this->mtitle); } else { $marker_titles = array(); } if ($this->mopen) { $marker_open = explode(';', $this->mopen); } else { $marker_open = array(); } $mcount = count($marker_ids); if ($mcount > count($marker_lats)) { throw new Data_Insufficient_Exception('mlat'); } if ($mcount > count($marker_lngs)) { throw new Data_Insufficient_Exception('mlng'); } $innerHTML_str = $this->setContentByString ? '.innerHTML' : ''; for ($mi = 0; $mi < $mcount; ++$mi) { if ($marker_ids[$mi] !== '' && $marker_ids[$mi] != '#') { $markers_js .= <<<EOT \t\tvar contentElem{$mi} = document.getElementById("{$marker_ids[$mi]}").cloneNode(true); \t\tvar infowindow{$mi} = new google.maps.InfoWindow({content: contentElem{$mi}{$innerHTML_str}}); EOT; } $markers_js .= <<<EOT \t\tvar markerPos{$mi} = new google.maps.LatLng({$marker_lats[$mi]}, {$marker_lngs[$mi]}); EOT; if (array_key_exists($mi, $marker_icons)) { $marker_icon_js = ", icon: '" . str_replace("'", "\\'", $marker_icons[$mi]) . "'"; } else { $marker_icon_js = ''; } if (array_key_exists($mi, $marker_titles)) { $marker_title_js = ", title: '{$marker_titles[$mi]}'"; } else { $marker_title_js = ''; } $markers_js .= <<<EOT \t\tvar marker{$mi} = new google.maps.Marker({position: markerPos{$mi}, map: map{$marker_icon_js}{$marker_title_js}}); EOT; if ($marker_ids[$mi] !== '' && $marker_ids[$mi] != '#') { $markers_js .= <<<EOT \t\tgoogle.maps.event.addListener(marker{$mi}, 'click', function() { infowindow{$mi}.open(map, marker{$mi}); }); EOT; if (array_key_exists($mi, $marker_open) && $marker_open[$mi]) { $markers_js .= <<<EOT \t\tinfowindow{$mi}.open(map, marker{$mi}); EOT; } } } } if ($this->navigationControl) { $navigationControl_js = <<<EOT \t\t\tnavigationControl: true, EOT; if ($this->navigationControl !== true) { $navigationControl_js .= <<<EOT \t\t\tnavigationControlOptions: {style: google.maps.NavigationControlStyle.{$this->navigationControl}}, EOT; } } else { $navigationControl_js = <<<EOT \t\t\tnavigationControl: false, EOT; } if ($this->mapTypeControl) { $mapTypeControl_js = <<<EOT \t\t\tmapTypeControl: true, EOT; if ($this->mapTypeControl !== true) { $mapTypeControl_js .= <<<EOT \t\t\tmapTypeControlOptions: {style: google.maps.MapTypeControlStyle.{$this->mapTypeControl}}, EOT; } } else { $mapTypeControl_js = <<<EOT \t\t\tmapTypeControl: false, EOT; } if ($this->scaleControl) { $scaleControl_js = <<<EOT \t\t\tscaleControl: true, EOT; } else { $scaleControl_js = <<<EOT \t\t\tscaleControl: false, EOT; } $init_js_src = <<<EOT \tfunction initialize_googlemap_{$this->id}() \t{ \t\tvar myLatlng = new google.maps.LatLng({$this->lat}, {$this->lng}); \t\tvar myOptions = \t\t{ \t\t\tzoom: {$this->zoom}, \t\t\tcenter: myLatlng,{$navigationControl_js}{$mapTypeControl_js}{$scaleControl_js} \t\t\tmapTypeId: google.maps.MapTypeId.{$this->type} \t\t} \t\tvar map = new google.maps.Map(document.getElementById("{$this->id}"), myOptions);{$markers_js} \t} \tgoogle.setOnLoadCallback(initialize_googlemap_{$this->id}); EOT; $init_js = new HTML_Javascript(); $init_js->add(new HTML_Text($init_js_src)); $sensor = $this->sensor ? 'true' : 'false'; $scripts->addUnique(new HTML_JavaScript("http://www.google.com/jsapi?autoload=" . urlencode("{modules:[{name:\"maps\",version:3,other_params:\"sensor={$sensor}\"}]}"))); $scripts->addUnique($init_js); }