API Docs for: 1.0.0
Show:

File: Resources/public/js/views/ez-notificationview.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-notificationview', function (Y) {
  6. "use strict";
  7. /**
  8. * Provides the notification view class
  9. *
  10. * @method ez-notificationview
  11. */
  12.  
  13. var IS_ACTIVE = 'is-active';
  14.  
  15. /**
  16. * The notification view.
  17. *
  18. * @namespace eZ
  19. * @class NotificationView
  20. * @constructor
  21. * @extends eZ.TemplateBasedView
  22. */
  23. Y.eZ.NotificationView = Y.Base.create('notificationView', Y.eZ.TemplateBasedView, [], {
  24. events: {
  25. '.ez-notification-close': {
  26. 'tap': function (e) {
  27. e.preventDefault();
  28. this._closeNotification();
  29. }
  30. }
  31. },
  32.  
  33. /**
  34. * Closes the notification view by destroying the corresponding
  35. * Notification object.
  36. *
  37. * @method _closeNotification
  38. * @protected
  39. */
  40. _closeNotification: function (e) {
  41. this.get('notification').destroy();
  42. },
  43.  
  44. initializer: function () {
  45. var notification = this.get('notification');
  46.  
  47. /**
  48. * Stores the auto hide timer returned by Y.later.
  49. *
  50. * @property _autohideTimer
  51. * @type {Object|Null}
  52. * @protected
  53. */
  54. this._autohideTimer = null;
  55. this.containerTemplate = '<li class="' + this._generateViewClassName(this._getName()) + '"/>';
  56.  
  57. notification.after('stateChange', Y.bind(function (e) {
  58. this._uiChangeState(e.prevVal, e.newVal);
  59. }, this));
  60. notification.after('textChange', Y.bind(function () {
  61. this._uiChangeText(notification.get('text'));
  62. }, this));
  63.  
  64. notification.after('destroy', Y.bind(this._stopAutohide, this));
  65.  
  66. this.after('activeChange', function (e) {
  67. if ( this.get('active') ) {
  68. this.get('container').addClass(IS_ACTIVE);
  69. this._setAutohide();
  70. notification.after(
  71. 'timeoutChange',
  72. Y.bind(this._setAutohide, this)
  73. );
  74. } else {
  75. this.get('container').removeClass(IS_ACTIVE);
  76. }
  77. });
  78. },
  79.  
  80. /**
  81. * Creates a timer so that the notification is automatically hidden
  82. * after its timeout. An already existing timer if any is stopped
  83. *
  84. * @method _setAutohide
  85. * @protected
  86. */
  87. _setAutohide: function () {
  88. var timeout = this.get('notification').get('timeout');
  89.  
  90. this._stopAutohide();
  91. if ( timeout ) {
  92. this._autohideTimer = Y.later(timeout * 1000, this, this._closeNotification);
  93. }
  94. },
  95.  
  96. /**
  97. * Stops the auto hide timer if any
  98. *
  99. * @method _stopAutohide
  100. * @protected
  101. */
  102. _stopAutohide: function () {
  103. if ( this._autohideTimer ) {
  104. this._autohideTimer.cancel();
  105. }
  106. },
  107.  
  108. /**
  109. * Returns the state class from the given state string
  110. *
  111. * @method _getStateClass
  112. * @protected
  113. * @param {String} state
  114. */
  115. _getStateClass: function (state) {
  116. return 'is-state-' + state;
  117. },
  118.  
  119. /**
  120. * Set and unset the state class on the container depending on the
  121. * prevState and newState parameters
  122. *
  123. * @protected
  124. * @method _uiChangeState
  125. * @param {String} prevState
  126. * @param {String} newState
  127. */
  128. _uiChangeState: function (prevState, newState) {
  129. var container = this.get('container');
  130.  
  131. if ( prevState ) {
  132. container.removeClass(this._getStateClass(prevState));
  133. }
  134. if ( newState ) {
  135. container.addClass(this._getStateClass(newState));
  136. }
  137. },
  138.  
  139. /**
  140. * Updates the displayed notification text
  141. *
  142. * @method _uiChangeText
  143. * @param {String} text
  144. * @protected
  145. */
  146. _uiChangeText: function (text) {
  147. this.get('container').one('.ez-notification-text').setContent(text);
  148. },
  149.  
  150. render: function () {
  151. var container = this.get('container');
  152.  
  153. this._uiChangeState(null, this.get('notification').get('state'));
  154. container.setHTML(this.template({
  155. notification: this.get('notification').toJSON(),
  156. }));
  157. return this;
  158. },
  159.  
  160. /**
  161. * Hides the notification view and destroys the view after the CSS
  162. * transition has been executed.
  163. *
  164. * @method vanish
  165. */
  166. vanish: function () {
  167. this.get('container').onceAfter(['webkitTransitionEnd', 'transitionend'], Y.bind(function () {
  168. this.destroy({remove: true});
  169. }, this));
  170. this.set('active', false);
  171. },
  172. }, {
  173. ATTRS: {
  174. /**
  175. * The notification model to displayed
  176. *
  177. * @attribute notification
  178. * @type {eZ.Notification}
  179. * @required
  180. */
  181. notification: {
  182. writeOnce: 'initOnly',
  183. },
  184. },
  185. });
  186. });
  187.