You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VCustomLayout.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.HashMap;
  18. import java.util.Locale;
  19. import java.util.Map;
  20. import com.google.gwt.dom.client.Element;
  21. import com.google.gwt.dom.client.ImageElement;
  22. import com.google.gwt.dom.client.NodeList;
  23. import com.google.gwt.dom.client.Style;
  24. import com.google.gwt.dom.client.Style.BorderStyle;
  25. import com.google.gwt.dom.client.Style.Position;
  26. import com.google.gwt.dom.client.Style.Unit;
  27. import com.google.gwt.user.client.DOM;
  28. import com.google.gwt.user.client.Event;
  29. import com.google.gwt.user.client.ui.ComplexPanel;
  30. import com.google.gwt.user.client.ui.Widget;
  31. import com.vaadin.client.ApplicationConnection;
  32. import com.vaadin.client.BrowserInfo;
  33. import com.vaadin.client.ComponentConnector;
  34. import com.vaadin.client.StyleConstants;
  35. import com.vaadin.client.Util;
  36. import com.vaadin.client.VCaption;
  37. import com.vaadin.client.VCaptionWrapper;
  38. import com.vaadin.client.WidgetUtil;
  39. /**
  40. * Custom Layout implements complex layout defined with HTML template.
  41. *
  42. * @author Vaadin Ltd
  43. *
  44. */
  45. public class VCustomLayout extends ComplexPanel {
  46. public static final String CLASSNAME = "v-customlayout";
  47. /** Location-name to containing element in DOM map */
  48. private final Map<String, Element> locationToElement = new HashMap<>();
  49. /** Location-name to contained widget map */
  50. final Map<String, Widget> locationToWidget = new HashMap<>();
  51. /** Widget to captionwrapper map */
  52. private final Map<Widget, VCaptionWrapper> childWidgetToCaptionWrapper = new HashMap<>();
  53. /**
  54. * Unexecuted scripts loaded from the template.
  55. * <p>
  56. * For internal use only. May be removed or replaced in the future.
  57. */
  58. public String scripts = "";
  59. /**
  60. * Paintable ID of this paintable.
  61. * <p>
  62. * For internal use only. May be removed or replaced in the future.
  63. */
  64. public String pid;
  65. /** For internal use only. May be removed or replaced in the future. */
  66. public ApplicationConnection client;
  67. private boolean htmlInitialized = false;
  68. private Element elementWithNativeResizeFunction;
  69. private String height = "";
  70. private String width = "";
  71. public VCustomLayout() {
  72. setElement(DOM.createDiv());
  73. // Clear any unwanted styling
  74. Style style = getElement().getStyle();
  75. style.setBorderStyle(BorderStyle.NONE);
  76. style.setMargin(0, Unit.PX);
  77. style.setPadding(0, Unit.PX);
  78. if (BrowserInfo.get().isIE()) {
  79. style.setPosition(Position.RELATIVE);
  80. }
  81. setStyleName(CLASSNAME);
  82. }
  83. @Override
  84. public void setStyleName(String style) {
  85. super.setStyleName(style);
  86. addStyleName(StyleConstants.UI_LAYOUT);
  87. }
  88. /**
  89. * Sets widget to given location.
  90. *
  91. * If location already contains a widget it will be removed.
  92. *
  93. * @param widget
  94. * Widget to be set into location.
  95. * @param location
  96. * location name where widget will be added
  97. *
  98. * @throws IllegalArgumentException
  99. * if no such location is found in the layout.
  100. */
  101. public void setWidget(Widget widget, String location) {
  102. if (widget == null) {
  103. return;
  104. }
  105. // If no given location is found in the layout, and exception is throws
  106. Element elem = locationToElement.get(location);
  107. if (elem == null && hasTemplate()) {
  108. throw new IllegalArgumentException(
  109. "No location " + location + " found");
  110. }
  111. // Get previous widget
  112. final Widget previous = locationToWidget.get(location);
  113. // NOP if given widget already exists in this location
  114. if (previous == widget) {
  115. return;
  116. }
  117. if (previous != null) {
  118. remove(previous);
  119. }
  120. // if template is missing add element in order
  121. if (!hasTemplate()) {
  122. elem = getElement();
  123. }
  124. // Add widget to location
  125. super.add(widget, elem);
  126. locationToWidget.put(location, widget);
  127. }
  128. /** Initialize HTML-layout. */
  129. public void initializeHTML(String template, String themeUri) {
  130. // Connect body of the template to DOM
  131. template = extractBodyAndScriptsFromTemplate(template);
  132. // TODO prefix img src:s here with a regeps, cannot work further with IE
  133. String relImgPrefix = WidgetUtil
  134. .escapeAttribute(themeUri + "/layouts/");
  135. // prefix all relative image elements to point to theme dir with a
  136. // regexp search
  137. template = template.replaceAll(
  138. "<((?:img)|(?:IMG))\\s([^>]*)src=\"((?![a-z]+:)[^/][^\"]+)\"",
  139. "<$1 $2src=\"" + relImgPrefix + "$3\"");
  140. // also support src attributes without quotes
  141. template = template.replaceAll(
  142. "<((?:img)|(?:IMG))\\s([^>]*)src=[^\"]((?![a-z]+:)[^/][^ />]+)[ />]",
  143. "<$1 $2src=\"" + relImgPrefix + "$3\"");
  144. // also prefix relative style="...url(...)..."
  145. template = template.replaceAll(
  146. "(<[^>]+style=\"[^\"]*url\\()((?![a-z]+:)[^/][^\"]+)(\\)[^>]*>)",
  147. "$1 " + relImgPrefix + "$2 $3");
  148. getElement().setInnerHTML(template);
  149. // Remap locations to elements
  150. locationToElement.clear();
  151. scanForLocations(getElement());
  152. initImgElements();
  153. elementWithNativeResizeFunction = DOM.getFirstChild(getElement());
  154. if (elementWithNativeResizeFunction == null) {
  155. elementWithNativeResizeFunction = getElement();
  156. }
  157. publishResizedFunction(elementWithNativeResizeFunction);
  158. htmlInitialized = true;
  159. }
  160. private native boolean uriEndsWithSlash()
  161. /*-{
  162. var path = $wnd.location.pathname;
  163. if (path.charAt(path.length - 1) == "/")
  164. return true;
  165. return false;
  166. }-*/;
  167. /** For internal use only. May be removed or replaced in the future. */
  168. public boolean hasTemplate() {
  169. return htmlInitialized;
  170. }
  171. /** Collect locations from template */
  172. private void scanForLocations(Element elem) {
  173. if (elem.hasAttribute("location")) {
  174. final String location = elem.getAttribute("location");
  175. locationToElement.put(location, elem);
  176. elem.setInnerHTML("");
  177. } else if (elem.hasAttribute("data-location")) {
  178. final String location = elem.getAttribute("data-location");
  179. locationToElement.put(location, elem);
  180. elem.setInnerHTML("");
  181. } else {
  182. final int len = DOM.getChildCount(elem);
  183. for (int i = 0; i < len; i++) {
  184. scanForLocations(DOM.getChild(elem, i));
  185. }
  186. }
  187. }
  188. /**
  189. * Evaluate given script in browser document.
  190. * <p>
  191. * For internal use only. May be removed or replaced in the future.
  192. */
  193. public static native void eval(String script)
  194. /*-{
  195. try {
  196. if (script != null)
  197. eval("{ var document = $doc; var window = $wnd; "+ script + "}");
  198. } catch (e) {
  199. }
  200. }-*/;
  201. /**
  202. * Img elements needs some special handling in custom layout. Img elements
  203. * will get their onload events sunk. This way custom layout can notify
  204. * parent about possible size change.
  205. */
  206. private void initImgElements() {
  207. NodeList<Element> nodeList = getElement().getElementsByTagName("IMG");
  208. for (int i = 0; i < nodeList.getLength(); i++) {
  209. ImageElement img = ImageElement.as(nodeList.getItem(i));
  210. DOM.sinkEvents(img, Event.ONLOAD);
  211. }
  212. }
  213. /**
  214. * Extract body part and script tags from raw html-template.
  215. *
  216. * Saves contents of all script-tags to private property: scripts. Returns
  217. * contents of the body part for the html without script-tags. Also replaces
  218. * all _UID_ tags with an unique id-string.
  219. *
  220. * @param html
  221. * Original HTML-template received from server
  222. * @return html that is used to create the HTMLPanel.
  223. */
  224. private String extractBodyAndScriptsFromTemplate(String html) {
  225. // Replace UID:s
  226. html = html.replaceAll("_UID_", pid + "__");
  227. // Exctract script-tags
  228. scripts = "";
  229. int endOfPrevScript = 0;
  230. int nextPosToCheck = 0;
  231. String lc = html.toLowerCase(Locale.ROOT);
  232. String res = "";
  233. int scriptStart = lc.indexOf("<script", nextPosToCheck);
  234. while (scriptStart > 0) {
  235. res += html.substring(endOfPrevScript, scriptStart);
  236. scriptStart = lc.indexOf(">", scriptStart);
  237. final int j = lc.indexOf("</script>", scriptStart);
  238. scripts += html.substring(scriptStart + 1, j) + ";";
  239. nextPosToCheck = endOfPrevScript = j + "</script>".length();
  240. scriptStart = lc.indexOf("<script", nextPosToCheck);
  241. }
  242. res += html.substring(endOfPrevScript);
  243. // Extract body
  244. html = res;
  245. lc = html.toLowerCase(Locale.ROOT);
  246. int startOfBody = lc.indexOf("<body");
  247. if (startOfBody < 0) {
  248. res = html;
  249. } else {
  250. res = "";
  251. startOfBody = lc.indexOf(">", startOfBody) + 1;
  252. final int endOfBody = lc.indexOf("</body>", startOfBody);
  253. if (endOfBody > startOfBody) {
  254. res = html.substring(startOfBody, endOfBody);
  255. } else {
  256. res = html.substring(startOfBody);
  257. }
  258. }
  259. return res;
  260. }
  261. /**
  262. * Update caption for the given child connector.
  263. */
  264. public void updateCaption(ComponentConnector childConnector) {
  265. Widget widget = childConnector.getWidget();
  266. if (!widget.isAttached()) {
  267. // Widget has not been added because the location was not found
  268. return;
  269. }
  270. VCaptionWrapper wrapper = childWidgetToCaptionWrapper.get(widget);
  271. if (VCaption.isNeeded(childConnector)) {
  272. if (wrapper == null) {
  273. // Add a wrapper between the layout and the child widget
  274. final String loc = getLocation(widget);
  275. super.remove(widget);
  276. wrapper = new VCaptionWrapper(childConnector, client);
  277. super.add(wrapper, locationToElement.get(loc));
  278. childWidgetToCaptionWrapper.put(widget, wrapper);
  279. }
  280. wrapper.updateCaption();
  281. } else {
  282. if (wrapper != null) {
  283. // Remove the wrapper and add the widget directly to the layout
  284. final String loc = getLocation(widget);
  285. super.remove(wrapper);
  286. super.add(widget, locationToElement.get(loc));
  287. childWidgetToCaptionWrapper.remove(widget);
  288. }
  289. }
  290. }
  291. /** Get the location of an widget. */
  292. public String getLocation(Widget w) {
  293. for (final String location : locationToWidget.keySet()) {
  294. if (locationToWidget.get(location) == w) {
  295. return location;
  296. }
  297. }
  298. return null;
  299. }
  300. /** Removes given widget from the layout. */
  301. @Override
  302. public boolean remove(Widget w) {
  303. final String location = getLocation(w);
  304. if (location != null) {
  305. locationToWidget.remove(location);
  306. }
  307. final VCaptionWrapper cw = childWidgetToCaptionWrapper.get(w);
  308. if (cw != null) {
  309. childWidgetToCaptionWrapper.remove(w);
  310. return super.remove(cw);
  311. } else if (w != null) {
  312. return super.remove(w);
  313. }
  314. return false;
  315. }
  316. /** Adding widget without specifying location is not supported. */
  317. @Override
  318. public void add(Widget w) {
  319. throw new UnsupportedOperationException();
  320. }
  321. /** Clear all widgets from the layout. */
  322. @Override
  323. public void clear() {
  324. super.clear();
  325. locationToWidget.clear();
  326. childWidgetToCaptionWrapper.clear();
  327. }
  328. /**
  329. * This method is published to JS side with the same name into first DOM
  330. * node of custom layout. This way if one implements some resizeable
  331. * containers in custom layout he/she can notify children after resize.
  332. */
  333. public void notifyChildrenOfSizeChange() {
  334. client.runDescendentsLayout(this);
  335. }
  336. @Override
  337. public void onDetach() {
  338. super.onDetach();
  339. if (elementWithNativeResizeFunction != null) {
  340. detachResizedFunction(elementWithNativeResizeFunction);
  341. }
  342. }
  343. private native void detachResizedFunction(Element element)
  344. /*-{
  345. element.notifyChildrenOfSizeChange = null;
  346. }-*/;
  347. private native void publishResizedFunction(Element element)
  348. /*-{
  349. var self = this;
  350. element.notifyChildrenOfSizeChange = $entry(function() {
  351. self.@com.vaadin.client.ui.VCustomLayout::notifyChildrenOfSizeChange()();
  352. });
  353. }-*/;
  354. /**
  355. * In custom layout one may want to run layout functions made with
  356. * JavaScript. This function tests if one exists (with name "iLayoutJS" in
  357. * layouts first DOM node) and runs it. Return value is used to determine if
  358. * children needs to be notified of size changes.
  359. * <p>
  360. * Note! When implementing a JS layout function you most likely want to call
  361. * notifyChildrenOfSizeChange() function on your custom layouts main
  362. * element. That method is used to control whether child components layout
  363. * functions are to be run.
  364. * <p>
  365. * For internal use only. May be removed or replaced in the future.
  366. *
  367. * @param el
  368. * The first element of the layout
  369. * @return true if layout function exists and was run successfully, else
  370. * false.
  371. */
  372. public native boolean iLayoutJS(com.google.gwt.user.client.Element el)
  373. /*-{
  374. if (el && el.iLayoutJS) {
  375. try {
  376. el.iLayoutJS();
  377. return true;
  378. } catch (e) {
  379. return false;
  380. }
  381. } else {
  382. return false;
  383. }
  384. }-*/;
  385. @Override
  386. public void onBrowserEvent(Event event) {
  387. super.onBrowserEvent(event);
  388. if (event.getTypeInt() == Event.ONLOAD) {
  389. Util.notifyParentOfSizeChange(this, true);
  390. event.cancelBubble(true);
  391. }
  392. }
  393. }