summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TImageMap.php
blob: 3ca178b23ba491555d7c0e29ef1fdf46789651e7 (plain)
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
<?php
/**
 * TImageMap and related class file.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package System.Web.UI.WebControls
 */

/**
 * Includes TImage class file
 */
Prado::using('System.Web.UI.WebControls.TImage');

/**
 * TImageMap class
 *
 * TImageMap represents an image on a page. Hotspot regions can be defined
 * within the image. Depending on the {@link setHotSpotMode HotSpotMode},
 * clicking on the hotspots may trigger a postback or navigate to a specified
 * URL. The hotspots defined may be accessed via {@link getHotSpots HotSpots}.
 * Each hotspot is described as a {@link THotSpot}, which can be a circle,
 * rectangle, polygon, etc. To add hotspot in a template, use the following,
 * <code>
 *  <com:TImageMap>
 *    <com:TCircleHotSpot ... />
 *    <com:TRectangleHotSpot ... />
 *    <com:TPolygonHotSpot ... />
 *  </com:TImageMap>
 * </code>
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TImageMap extends TImage implements IPostBackEventHandler
{
	const MAP_NAME_PREFIX='ImageMap';

	/**
	 * Processes an object that is created during parsing template.
	 * This method adds {@link THotSpot} objects into the hotspot collection
	 * of the imagemap.
	 * @param string|TComponent text string or component parsed and instantiated in template
	 */
	public function addParsedObject($object)
	{
		if($object instanceof THotSpot)
			$this->getHotSpots()->add($object);
	}

	/**
	 * Adds attribute name-value pairs to renderer.
	 * This overrides the parent implementation with additional imagemap specific attributes.
	 * @param THtmlWriter the writer used for the rendering purpose
	 */
	protected function addAttributesToRender($writer)
	{
		parent::addAttributesToRender($writer);
		if($this->getHotSpots()->getCount()>0)
		{
			$writer->addAttribute('usemap','#'.self::MAP_NAME_PREFIX.$this->getClientID());
			$writer->addAttribute('id',$this->getUniqueID());
		}
		if($this->getEnabled() && !$this->getEnabled(true))
			$writer->addAttribute('disabled','disabled');
	}

	/**
	 * Renders this imagemap.
	 * @param THtmlWriter
	 */
	public function render($writer)
	{
		parent::render($writer);

		$hotspots=$this->getHotSpots();

		if($hotspots->getCount()>0)
		{
			$clientID=$this->getClientID();
			$cs=$this->getPage()->getClientScript();
			$writer->writeLine();
			$writer->addAttribute('name',self::MAP_NAME_PREFIX.$clientID);
			$writer->renderBeginTag('map');
			$writer->writeLine();
			if(($mode=$this->getHotSpotMode())===THotSpotMode::NotSet)
				$mode=THotSpotMode::Navigate;
			$target=$this->getTarget();
			$i=0;
			$options['EventTarget'] = $this->getUniqueID();
			$options['StopEvent'] = true;
			$cs=$this->getPage()->getClientScript();
			foreach($hotspots as $hotspot)
			{
				if($hotspot->getHotSpotMode()===THotSpotMode::NotSet)
					$hotspot->setHotSpotMode($mode);
				if($target!=='' && $hotspot->getTarget()==='')
					$hotspot->setTarget($target);
				if($hotspot->getHotSpotMode()===THotSpotMode::PostBack)
				{
					$id=$clientID.'_'.$i;
					$writer->addAttribute('id',$id);
					$writer->addAttribute('href','#'.$id); //create unique no-op url references
					$options['ID']=$id;
					$options['EventParameter']="$i";
					$options['CausesValidation']=$hotspot->getCausesValidation();
					$options['ValidationGroup']=$hotspot->getValidationGroup();
					$cs->registerPostBackControl($this->getClientClassName(),$options);
				}
				$hotspot->render($writer);
				$writer->writeLine();
				$i++;
			}
			$writer->renderEndTag();
		}
	}

	/**
	 * Gets the name of the javascript class responsible for performing postback for this control.
	 * This method overrides the parent implementation.
	 * @return string the javascript class name
	 */
	protected function getClientClassName()
	{
		return 'Prado.WebUI.TImageMap';
	}

	/**
	 * Raises the postback event.
	 * This method is required by {@link IPostBackEventHandler} interface.
	 * This method is mainly used by framework and control developers.
	 * @param TEventParameter the event parameter
	 */
	public function raisePostBackEvent($param)
	{
		$postBackValue=null;
		if($param!=='')
		{
			$index=TPropertyValue::ensureInteger($param);
			$hotspots=$this->getHotSpots();
			if($index>=0 && $index<$hotspots->getCount())
			{
				$hotspot=$hotspots->itemAt($index);
				if(($mode=$hotspot->getHotSpotMode())===THotSpotMode::NotSet)
					$mode=$this->getHotSpotMode();
				if($mode===THotSpotMode::PostBack)
				{
					$postBackValue=$hotspot->getPostBackValue();
					if($hotspot->getCausesValidation())
						$this->getPage()->validate($hotspot->getValidationGroup());
				}
			}
		}
		if($postBackValue!==null)
			$this->onClick(new TImageMapEventParameter($postBackValue));
	}

	/**
	 * @return THotSpotMode the behavior of hotspot regions in this imagemap when they are clicked. Defaults to THotSpotMode::NotSet.
	 */
	public function getHotSpotMode()
	{
		return $this->getViewState('HotSpotMode',THotSpotMode::NotSet);
	}

	/**
	 * Sets the behavior of hotspot regions in this imagemap when they are clicked.
	 * If an individual hotspot has a mode other than 'NotSet', the mode set in this
	 * imagemap will be ignored. By default, 'NotSet' is equivalent to 'Navigate'.
	 * @param THotSpotMode the behavior of hotspot regions in this imagemap when they are clicked.
	 */
	public function setHotSpotMode($value)
	{
		$this->setViewState('HotSpotMode',TPropertyValue::ensureEnum($value,'THotSpotMode'),THotSpotMode::NotSet);
	}

	/**
	 * @return THotSpotCollection collection of hotspots defined in this imagemap.
	 */
	public function getHotSpots()
	{
		if(($hotspots=$this->getViewState('HotSpots',null))===null)
		{
			$hotspots=new THotSpotCollection;
			$this->setViewState('HotSpots',$hotspots);
		}
		return $hotspots;
	}

	/**
	 * @return string  the target window or frame to display the new page when a hotspot region is clicked within the imagemap. Defaults to ''.
	 */
	public function getTarget()
	{
		return $this->getViewState('Target','');
	}

	/**
	 * @param string  the target window or frame to display the new page when a hotspot region is clicked within the imagemap.
	 */
	public function setTarget($value)
	{
		$this->setViewState('Target',TPropertyValue::ensureString($value),'');
	}

	/**
	 * Raises <b>OnClick</b> event.
	 * This method is invoked when a hotspot region is clicked within the imagemap.
	 * If you override this method, be sure to call the parent implementation
	 * so that the event handler can be invoked.
	 * @param TImageMapEventParameter event parameter to be passed to the event handlers
	 */
	public function onClick($param)
	{
		$this->raiseEvent('OnClick',$this,$param);
	}
}