1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
<?php
/**
* TClientScriptManager class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Revision: $ $Date: $
* @package System.Web.UI
*/
Prado::using('System.Web.Javascripts.*');
/**
* TClientScriptManager class
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System.Web.UI
* @since 3.0
*/
class TClientScriptManager extends TComponent
{
const SCRIPT_DIR='Web/Javascripts/js';
//const POSTBACK_FUNC='Prado.doPostBack';
private $_page;
private $_hiddenFields=array();
private $_beginScripts=array();
private $_endScripts=array();
private $_scriptFiles=array();
//private $_headScriptFiles=array();
//private $_headScripts=array();
private $_styleSheetFiles=array();
private $_styleSheets=array();
private $_client;
/*private $_onSubmitStatements=array();
private $_arrayDeclares=array();
private $_expandoAttributes=array();
private $_postBackScriptRegistered=false;
private $_focusScriptRegistered=false;
private $_scrollScriptRegistered=false;
*/
private $_publishedScriptFiles=array();
public function __construct(TPage $owner)
{
$this->_page=$owner;
$this->_client = new TClientScript($this);
}
public function registerPostBackControl($control,$namespace='Prado.WebUI')
{
$options = $this->getPostBackOptions($control);
$type = get_class($control);
$namespace = empty($namespace) ? "window" : $namespace;
$code = "new {$namespace}.{$type}($options);";
$this->registerEndScript(sprintf('%08X', crc32($code)), $code);
$this->registerHiddenField(TPage::FIELD_POSTBACK_TARGET,'');
$this->registerHiddenField(TPage::FIELD_POSTBACK_PARAMETER,'');
$this->registerClientScript('prado');
}
protected function getPostBackOptions($control)
{
$postback = $control->getPostBackOptions();
if(!isset($postback['ID']))
$postback['ID'] = $control->getClientID();
if(!isset($postback['FormID']))
$postback['FormID'] = $this->_page->getForm()->getClientID();
$options = new TJavascriptSerializer($postback);
return $options->toJavascript();
}
/**
* Register a default button to panel. When the $panel is in focus and
* the 'enter' key is pressed, the $button will be clicked.
* @param TControl panel to register the default button action
* @param TControl button to trigger a postback
*/
public function registerDefaultButton($panel, $button)
{
$serializer = new TJavascriptSerializer(
$this->getDefaultButtonOptions($panel, $button));
$options = $serializer->toJavascript();
$code = "new Prado.WebUI.DefaultButton($options);";
$scripts = $this->_page->getClientScript();
$scripts->registerEndScript("prado:".$panel->getClientID(), $code);
}
/**
* @return array default button options.
*/
protected function getDefaultButtonOptions($panel, $button)
{
$options['Panel'] = $panel->getClientID();
$options['Target'] = $button->getClientID();
$options['Event'] = 'click';
return $options;
}
/**
* Register client scripts.
*/
public function registerClientScript($script)
{
static $scripts = array();
$scripts = array_unique(array_merge($scripts,
TClientScript::getScripts($script)));
$this->publishClientScriptAssets($scripts);
//create the client script url
$url = $this->publishClientScriptCompressorAsset();
$url .= '?js='.implode(',', $scripts);
if(Prado::getApplication()->getMode() == TApplication::STATE_DEBUG)
$url .= '&__nocache';
$this->registerScriptFile('prado:gzipscripts', $url);
}
/**
* Publish each individual javascript file.
*/
protected function publishClientScriptAssets($scripts)
{
foreach($scripts as $lib)
{
if(!isset($this->_publishedScriptFiles[$lib]))
{
$base = Prado::getFrameworkPath();
$clientScripts = self::SCRIPT_DIR;
$file = "{$base}/{$clientScripts}/{$lib}.js";
Prado::getApplication()->getAssetManager()->publishFilePath($file);
$this->_publishedScriptFiles[$lib] = true;
}
}
}
/**
* @return string URL of the compressor asset script.
*/
protected function publishClientScriptCompressorAsset()
{
$scriptFile = 'clientscripts.php';
if(isset($this->_publishedScriptFiles[$scriptFile]))
return $this->_publishedScriptFiles[$scriptFile];
else
{
$base = Prado::getFrameworkPath();
$clientScripts = self::SCRIPT_DIR;
$file = "{$base}/{$clientScripts}/{$scriptFile}";
$url= Prado::getApplication()->getAssetManager()->publishFilePath($file);
$this->_publishedScriptFiles[$scriptFile] = $url;
return $url;
}
}
/* protected function registerPostBackScript()
{
if(!$this->_postBackScriptRegistered)
{
$this->_postBackScriptRegistered=true;
$this->registerHiddenField(TPage::FIELD_POSTBACK_TARGET,'');
$this->registerHiddenField(TPage::FIELD_POSTBACK_PARAMETER,'');
$this->registerPradoScript('prado');
}
}
public function registerFocusScript($target)
{
if(!$this->_focusScriptRegistered)
{
$this->_focusScriptRegistered=true;
$this->registerPradoScript('prado');
$this->registerEndScript('prado:focus','Prado.Focus.setFocus("'.THttpUtility::quoteJavaScriptString($target).'");');
}
}
public function registerScrollScript($x,$y)
{
if(!$this->_scrollScriptRegistered)
{
$this->_scrollScriptRegistered=true;
$this->registerHiddenField(TPage::FIELD_SCROLL_X,$x);
$this->registerHiddenField(TPage::FIELD_SCROLL_Y,$y);
// TBD, need scroll.js
}
}
public function registerDefaultButtonScript($source, $target)
{
$this->registerPradoScript('prado');
$button = $target->getClientID();
$panel = $source->getClientID();
return "Event.observe('{$panel}', 'keyup', Prado.Button.fireButton.bindEvent($('{$panel}'), '$button'));";
}
public function registerValidationScript()
{
}*/
public function isHiddenFieldRegistered($key)
{
return isset($this->_hiddenFields[$key]);
}
public function isScriptRegistered($key)
{
return isset($this->_scripts[$key]);
}
public function isScriptFileRegistered($key)
{
return isset($this->_scriptFiles[$key]);
}
public function isBeginScriptRegistered($key)
{
return isset($this->_beginScripts[$key]);
}
public function isEndScriptRegistered($key)
{
return isset($this->_endScripts[$key]);
}
/* public function isHeadScriptFileRegistered($key)
{
return isset($this->_headScriptFiles[$key]);
}
public function isHeadScriptRegistered($key)
{
return isset($this->_headScripts[$key]);
}
*/
public function isStyleSheetFileRegistered($key)
{
return isset($this->_styleSheetFiles[$key]);
}
public function isStyleSheetRegistered($key)
{
return isset($this->_styleSheets[$key]);
}
/* public function isOnSubmitStatementRegistered($key)
{
return isset($this->_onSubmitStatements[$key]);
}
public function registerArrayDeclaration($name,$value)
{
$this->_arrayDeclares[$name][]=$value;
}
*/
public function registerScriptFile($key,$url)
{
$this->_scriptFiles[$key]=$url;
}
public function registerHiddenField($name,$value)
{
// if the named hidden field exists and has a value null, it means the hidden field is rendered already
if(!isset($this->_hiddenFields[$name]) || $this->_hiddenFields[$name]!==null)
$this->_hiddenFields[$name]=$value;
}
/* public function registerOnSubmitStatement($key,$script)
{
$this->_onSubmitStatements[$key]=$script;
}
*/
public function registerBeginScript($key,$script)
{
$this->_beginScripts[$key]=$script;
}
public function registerEndScript($key,$script)
{
$this->_endScripts[$key]=$script;
}
/* public function registerHeadScriptFile($key,$url)
{
$this->_headScriptFiles[$key]=$url;
}
public function registerHeadScript($key,$script)
{
$this->_headScripts[$key]=$script;
}
*/
public function registerStyleSheetFile($key,$url)
{
$this->_styleSheetFiles[$key]=$url;
}
public function registerStyleSheet($key,$css)
{
$this->_styleSheets[$key]=$css;
}
/* public function registerExpandoAttribute($controlID,$name,$value)
{
$this->_expandoAttributes[$controlID][$name]=$value;
}
public function renderArrayDeclarations($writer)
{
if(count($this->_arrayDeclares))
{
$str="<script type=\"text/javascript\">\n//<![CDATA[\n";
foreach($this->_arrayDeclares as $name=>$array)
$str.="var $name=new Array(".implode(',',$array).");\n";
$str.="\n//]]>\n</script>\n";
$writer->write($str);
}
}
*/
public function renderScriptFiles($writer)
{
$str='';
foreach($this->_scriptFiles as $include)
$str.="<script type=\"text/javascript\" src=\"".THttpUtility::htmlEncode($include)."\"></script>\n";
$writer->write($str);
}
/* public function renderOnSubmitStatements($writer)
{
// ???
}
*/
public function renderBeginScripts($writer)
{
if(count($this->_beginScripts))
$writer->write("<script type=\"text/javascript\">\n//<![CDATA[\n".implode("\n",$this->_beginScripts)."\n//]]>\n</script>\n");
}
public function renderEndScripts($writer)
{
if(count($this->_endScripts))
$writer->write("<script type=\"text/javascript\">\n//<![CDATA[\n".implode("\n",$this->_endScripts)."\n//]]>\n</script>\n");
}
public function renderHiddenFields($writer)
{
$str='';
foreach($this->_hiddenFields as $name=>$value)
{
if($value!==null)
{
$value=THttpUtility::htmlEncode($value);
$str.="<input type=\"hidden\" name=\"$name\" id=\"$name\" value=\"$value\" />\n";
// set hidden field value to null to indicate this field is rendered
// Note, hidden field rendering is invoked twice (at the beginning and ending of TForm)
$this->_hiddenFields[$name]=null;
}
}
if($str!=='')
$writer->write("<div>\n".$str."</div>\n");
}
/* public function renderExpandoAttributes($writer)
{
if(count($this->_expandoAttributes))
{
$str="<script type=\"text/javascript\">\n//<![CDATA[\n";
foreach($this->_expandoAttributes as $controlID=>$attrs)
{
$str.="var $controlID = document.all ? document.all[\"$controlID\"] : document.getElementById(\"$controlID\");\n";
foreach($attrs as $name=>$value)
{
if($value===null)
$str.="{$key}[\"$name\"]=null;\n";
else
$str.="{$key}[\"$name\"]=\"$value\";\n";
}
}
$str.="\n//]]>\n</script>\n";
$writer->write($str);
}
}
*/
/* public function renderHeadScriptFiles($writer)
{
$str='';
foreach($this->_headScriptFiles as $url)
$str.="<script type=\"text/javascript\" src=\"".THttpUtility::htmlEncode($url)."\"></script>\n";
$writer->write($str);
}
public function renderHeadScripts($writer)
{
if(count($this->_headScripts))
$writer->write("<script type=\"text/javascript\">\n//<![CDATA[\n".implode("\n",$this->_headScripts)."\n//]]>\n</script>\n");
}
*/
public function renderJavascriptBlock($code)
{
return "<script type=\"text/javascript\">\n/*<![CDATA[*/\n{$code}\n/*]]>*/\n</script>";
}
public function renderStyleSheetFiles($writer)
{
$str='';
foreach($this->_styleSheetFiles as $url)
{
$str.="<link rel=\"stylesheet\" type=\"text/css\" href=\"".THttpUtility::htmlEncode($url)."\" />\n";
}
$writer->write($str);
}
public function renderStyleSheets($writer)
{
if(count($this->_styleSheets))
$writer->write("<style type=\"text/css\">\n".implode("\n",$this->_styleSheets)."\n</style>\n");
}
public function getHasHiddenFields()
{
return count($this->_hiddenFields)>0;
}
/* public function getHasSubmitStatements()
{
return count($this->_onSubmitStatements)>0;
}
*/
/* public function registerClientEvent($control, $event, $code)
{
if(empty($code)) return;
$this->registerPradoScript("prado");
$script= "Event.observe('{$control->ClientID}', '{$event}', function(e){ {$code} });";
$key = "prado:{$control->ClientID}:{$event}";
$this->registerEndScript($key, $script);
}
*/
/*
private void EnsureEventValidationFieldLoaded();
internal string GetEventValidationFieldValue();
public string GetWebResourceUrl(Type type, string resourceName);
public void RegisterClientScriptResource(Type type, string resourceName);
internal void RegisterDefaultButtonScript(Control button, $writer, bool useAddAttribute);
public function SaveEventValidationField();
public void ValidateEvent(string uniqueId, string argument);
public function getCallbackEventReference()
*/
}
/**
* PradoClientScript class.
*
* Resolves Prado client script dependencies. e.g. TPradoClientScript::getScripts("dom");
*
* - <b>base</b> basic javascript utilities, e.g. $()
* - <b>dom</b> DOM and Form functions, e.g. $F(inputID) to retrive form input values.
* - <b>effects</b> Effects such as fade, shake, move
* - <b>controls</b> Prado client-side components, e.g. Slider, AJAX components
* - <b>validator</b> Prado client-side validators.
* - <b>ajax</b> Prado AJAX library including Prototype's AJAX and JSON.
*
* Dependencies for each library are automatically resolved.
*
* Namespace: System.Web.UI
*
* @author Wei Zhuo<weizhuo[at]gmail[dot]com>
* @version $Revision: 1.1 $ $Date: 2005/11/06 23:02:33 $
* @package System.Web.UI
*/
class TPradoClientScript
{
/**
* Client-side javascript library dependencies
* @var array
*/
protected static $_dependencies = array(
'prado' => array('prado'),
'effects' => array('prado', 'effects'),
'ajax' => array('prado', 'effects', 'ajax'),
'validator' => array('prado', 'validator'),
'logger' => array('prado', 'logger'),
'datepicker' => array('prado', 'datepicker'),
'rico' => array('prado', 'effects', 'ajax', 'rico'),
'colorpicker' => array('prado', 'colorpicker')
);
/**
* Resolve dependencies for the given library name(s).
* @param string|array name(s) of the library to be loaded.
* @return array list of library file names (w/o extension) for the specified library name(s).
*/
public function getScripts($scripts)
{
$files = array();
if(!is_array($scripts)) $scripts = array($scripts);
foreach($scripts as $script)
{
if(isset(self::$_dependencies[$script]))
$files = array_merge($files, self::$_dependencies[$script]);
$files[] = $script;
}
$files = array_unique($files);
return $files;
}
/**
* TODO: clean up
*
public function getPostBackEventReference($control,$parameter='',$options=null,$javascriptPrefix=true)
{
if(!$options || (!$options->getPerformValidation() && !$options->getTrackFocus() && $options->getClientSubmit() && $options->getActionUrl()==''))
{
$this->registerPostBackScript();
if(($form=$this->_page->getForm())!==null)
$formID=$form->getClientID();
else
throw new TConfigurationException('clientscriptmanager_form_required');
$postback=self::POSTBACK_FUNC.'(\''.$formID.'\',\''.$control->getUniqueID().'\',\''.THttpUtility::quoteJavaScriptString($parameter).'\')';
if($options && $options->getAutoPostBack())
$postback='setTimeout(\''.THttpUtility::quoteJavaScriptString($postback).'\',0)';
return $javascriptPrefix?'javascript:'.$postback:$postback;
}
$opt='';
$flag=false;
if($options->getPerformValidation())
{
$flag=true;
$this->registerValidationScript();
$opt.=',true,';
}
else
$opt.=',false,';
if($options->getValidationGroup()!=='')
{
$flag=true;
$opt.='"'.$options->getValidationGroup().'",';
}
else
$opt.='\'\',';
if($options->getActionUrl()!=='')
{
$flag=true;
$this->_page->setCrossPagePostBack(true);
$opt.='"'.$options->getActionUrl().'",';
}
else
$opt.='null,';
if($options->getTrackFocus())
{
$flag=true;
$this->registerFocusScript();
$opt.='true,';
}
else
$opt.='false,';
if($options->getClientSubmit())
{
$flag=true;
$opt.='true';
}
else
$opt.='false';
if(!$flag)
return '';
$this->registerPostBackScript();
if(($form=$this->_page->getForm())!==null)
$formID=$form->getClientID();
else
throw new TConfigurationException('clientscriptmanager_form_required');
$postback=self::POSTBACK_FUNC.'(\''.$formID.'\',\''.$control->getUniqueID().'\',\''.THttpUtility::quoteJavaScriptString($parameter).'\''.$opt.')';
if($options && $options->getAutoPostBack())
$postback='setTimeout(\''.THttpUtility::quoteJavaScriptString($postback).'\',0)';
return $javascriptPrefix?'javascript:'.$postback:$postback;
}*/
}
/*class TPostBackOptions extends TComponent
{
public $_actionUrl='';
public $_autoPostBack=false;
public $_clientSubmit=true;
public $_performValidation=false;
public $_validationGroup='';
public $_trackFocus=false;
public function getActionUrl()
{
return $this->_actionUrl;
}
public function setActionUrl($value)
{
$this->_actionUrl=THttpUtility::quoteJavaScriptString($value);
}
public function getAutoPostBack()
{
return $this->_autoPostBack;
}
public function setAutoPostBack($value)
{
$this->_autoPostBack=$value;
}
public function getClientSubmit()
{
return $this->_clientSubmit;
}
public function setClientSubmit($value)
{
$this->_clientSubmit=$value;
}
public function getPerformValidation()
{
return $this->_performValidation;
}
public function setPerformValidation($value)
{
$this->_performValidation=$value;
}
public function getValidationGroup()
{
return $this->_validationGroup;
}
public function setValidationGroup($value)
{
$this->_validationGroup=$value;
}
public function getTrackFocus()
{
return $this->_trackFocus;
}
public function setTrackFocus($value)
{
$this->_trackFocus=$value;
}
}
*/
?>
|