API Docs for: 1.0.0
Show:

File: Resources/public/js/views/ez-serversideview.js

  1. /*
  2. * Copyright (C) eZ Systems AS. All rights reserved.
  3. * For full copyright and license information view LICENSE file distributed with this source code.
  4. */
  5. YUI.add('ez-serversideview', function (Y) {
  6. "use strict";
  7. /**
  8. * Provides the generic server side view class
  9. *
  10. * @module ez-serversideview
  11. */
  12. Y.namespace('eZ');
  13.  
  14. /**
  15. * The server side view
  16. *
  17. * @namespace eZ
  18. * @class ServerSideView
  19. * @constructor
  20. * @extends eZ.View
  21. */
  22. Y.eZ.ServerSideView = Y.Base.create('serverSideView', Y.eZ.View, [Y.eZ.Tabs, Y.eZ.SelectionTable], {
  23. events: {
  24. 'form input[type="submit"], form button, form input[type="image"]': {
  25. 'click': '_trackUsedButton',
  26. },
  27. 'form': {
  28. 'submit': '_confirmSubmit'
  29. },
  30. },
  31.  
  32. /**
  33. * Click handler on the buttons in form element. It is used to keep
  34. * which button was used to submit the form. This was introduced to fix
  35. * https://jira.ez.no/browse/EZP-25393 because under MacOS, Safari and
  36. * Firefox do not correctly update document.activeElement, as a result,
  37. * when handling the form submit, the button was lost.
  38. *
  39. * @method _trackUsedButton
  40. * @protected
  41. * @param {EventFacade} e
  42. */
  43. _trackUsedButton: function (e) {
  44. this._formButton = e.target;
  45. },
  46.  
  47. /**
  48. * Check if the submit action requires a confirm box,
  49. *
  50. * @method _confirmSubmit
  51. * @protected
  52. * @param {EventFacade} e
  53. */
  54. _confirmSubmit: function (e) {
  55. var button = this._formButton,
  56. details = '',
  57. container = this.get('container'),
  58. detailsAttr;
  59.  
  60. if (button && button.hasClass('ez-confirm-action')) {
  61. detailsAttr = button.getAttribute('data-confirmbox-details-selector');
  62. if (container.one(detailsAttr)) {
  63. details = container.one(detailsAttr).get('innerHTML');
  64. }
  65. e.preventDefault();
  66. this.fire('confirmBoxOpen', {
  67. config: {
  68. title: button.getAttribute('data-confirmbox-title'),
  69. details: details,
  70. confirmHandler: Y.bind(function () {
  71. this._submitForm(e, button);
  72. }, this)
  73. },
  74. });
  75. } else {
  76. this._submitForm(e, button);
  77. }
  78. },
  79.  
  80. /**
  81. * Form handling. The DOM submit is transformed into an application
  82. * level event `submitForm` so that the server side view service can
  83. * handle it.
  84. *
  85. * @method _submitForm
  86. * @protected
  87. * @param {EventFacade} e
  88. * @param {Node} button
  89. */
  90. _submitForm: function (e, button) {
  91. /**
  92. * Fired when a form is submitted in the browser
  93. *
  94. * @event submitForm
  95. * @param {Node} form the Node object of the submitted form
  96. * @param {Object} formData the serialized form data including the
  97. * used button to validate the form
  98. * @param {Event} originalEvent the original DOM submit event
  99. */
  100. this.fire('submitForm', {
  101. form: e.target,
  102. formData: this._serializeForm(e.target, button),
  103. originalEvent: e,
  104. });
  105. this._formButton = null;
  106. },
  107.  
  108. /**
  109. * Serializes the form data so that it can be sent with an AJAX request.
  110. * It also checks which button was used (if any) to include it in the
  111. * data.
  112. *
  113. * @method _serializeForm
  114. * @param {Node} form
  115. * @param {Node} button
  116. * @return {Object} key/value data of the form
  117. */
  118. _serializeForm: function (form, button) {
  119. var data = {};
  120.  
  121. if ( this._isSubmitButton(button, form) ) {
  122. data[button.getAttribute('name')] = "";
  123. }
  124.  
  125. form.get('elements').each(function (field) {
  126. var name = field.getAttribute('name'),
  127. type = field.get('type');
  128.  
  129. if ( !name ) {
  130. return;
  131. }
  132.  
  133. /* jshint -W015 */
  134. switch (type) {
  135. case 'button':
  136. case 'reset':
  137. case 'submit':
  138. break;
  139. case 'radio':
  140. case 'checkbox':
  141. if ( field.get('checked') ) {
  142. data[name] = field.get('value');
  143. }
  144. break;
  145. case 'select-multiple':
  146. if ( field.get('selectedIndex') >= 0 ) {
  147. data[name] = [];
  148. field.get('options').each(function (opt) {
  149. if ( opt.get('selected') ) {
  150. data[name].push(opt.get('value'));
  151. }
  152. });
  153. }
  154. break;
  155. default:
  156. // `.get('value')` returns the expected field value for
  157. // inputs, select-one and even textarea.
  158. data[name] = field.get('value');
  159. }
  160. /* jshint +W015 */
  161. });
  162. return data;
  163. },
  164.  
  165. /**
  166. * Checks whether the given node is a valid submit button for the given
  167. * form.
  168. *
  169. * @method _isSubmitButton
  170. * @protected
  171. * @param {Node} node
  172. * @param {Node} form
  173. * @return {Boolean}
  174. */
  175. _isSubmitButton: function (node, form) {
  176. var localName, name, type;
  177.  
  178. if ( !node || !form.contains(node) ) {
  179. return false;
  180. }
  181. name = node.getAttribute('name');
  182. if ( !name ) {
  183. return false;
  184. }
  185. localName = node.get('localName');
  186. type = node.getAttribute('type');
  187. return (
  188. localName === 'button' ||
  189. ( localName === 'input' && (type === 'submit' || type === 'image' ) )
  190. );
  191. },
  192.  
  193. /**
  194. * Initializes the view to make sure the container will get the
  195. * ez-view-serversideview class
  196. *
  197. * @method initializer
  198. */
  199. initializer: function () {
  200. /**
  201. * References the button used to submit a form, it is updated by
  202. * `_trackUsedButton` as a replacement for `document.activeElement`
  203. * which is not correctly updated by Firefox for OSX or Safari.
  204. * see https://jira.ez.no/browse/EZP-25393
  205. *
  206. * @property _formButton
  207. * @type {Node|null}
  208. */
  209. this._formButton = null;
  210. this.containerTemplate = '<div class="ez-view-serversideview"/>';
  211.  
  212. this.on('activeChange', function () {
  213. this.after('htmlChange', this.render);
  214. });
  215. },
  216.  
  217. /**
  218. * Renders the view in its container. It just puts the html attibute
  219. * content as the content of the view container
  220. *
  221. * @method render
  222. * @return {eZ.ServerSideView} the view it self
  223. */
  224. render: function () {
  225. this.get('container').setContent(this.get('html'));
  226. return this;
  227. },
  228.  
  229. /**
  230. * Returns the string to use as the page title
  231. *
  232. * @method getTitle
  233. * @return {String}
  234. */
  235. getTitle: function () {
  236. return this.get('title');
  237. },
  238. }, {
  239. ATTRS: {
  240. /**
  241. * The title of the view
  242. *
  243. * @attribute title
  244. * @default ""
  245. * @type String
  246. */
  247. title: {
  248. value: ""
  249. },
  250.  
  251. /**
  252. * The HTML content of the view
  253. *
  254. * @attribute html
  255. * @default ""
  256. * @type String
  257. */
  258. html: {
  259. value: ""
  260. },
  261. }
  262. });
  263. });
  264.