toast.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v0.11.4
  6. */
  7. (function( window, angular, undefined ){
  8. "use strict";
  9. /**
  10. * @ngdoc module
  11. * @name material.components.toast
  12. * @description
  13. * Toast
  14. */
  15. angular.module('material.components.toast', [
  16. 'material.core',
  17. 'material.components.button'
  18. ])
  19. .directive('mdToast', MdToastDirective)
  20. .provider('$mdToast', MdToastProvider);
  21. /* ngInject */
  22. function MdToastDirective($mdToast) {
  23. return {
  24. restrict: 'E',
  25. link: function postLink(scope, element, attr) {
  26. // When navigation force destroys an interimElement, then
  27. // listen and $destroy() that interim instance...
  28. scope.$on('$destroy', function() {
  29. $mdToast.destroy();
  30. });
  31. }
  32. };
  33. }
  34. MdToastDirective.$inject = ["$mdToast"];
  35. /**
  36. * @ngdoc service
  37. * @name $mdToast
  38. * @module material.components.toast
  39. *
  40. * @description
  41. * `$mdToast` is a service to build a toast notification on any position
  42. * on the screen with an optional duration, and provides a simple promise API.
  43. *
  44. *
  45. * ## Restrictions on custom toasts
  46. * - The toast's template must have an outer `<md-toast>` element.
  47. * - For a toast action, use element with class `md-action`.
  48. * - Add the class `md-capsule` for curved corners.
  49. *
  50. * @usage
  51. * <hljs lang="html">
  52. * <div ng-controller="MyController">
  53. * <md-button ng-click="openToast()">
  54. * Open a Toast!
  55. * </md-button>
  56. * </div>
  57. * </hljs>
  58. *
  59. * <hljs lang="js">
  60. * var app = angular.module('app', ['ngMaterial']);
  61. * app.controller('MyController', function($scope, $mdToast) {
  62. * $scope.openToast = function($event) {
  63. * $mdToast.show($mdToast.simple().content('Hello!'));
  64. * // Could also do $mdToast.showSimple('Hello');
  65. * };
  66. * });
  67. * </hljs>
  68. */
  69. /**
  70. * @ngdoc method
  71. * @name $mdToast#showSimple
  72. *
  73. * @description
  74. * Convenience method which builds and shows a simple toast.
  75. *
  76. * @returns {promise} A promise that can be resolved with `$mdToast.hide()` or
  77. * rejected with `$mdToast.cancel()`.
  78. *
  79. */
  80. /**
  81. * @ngdoc method
  82. * @name $mdToast#simple
  83. *
  84. * @description
  85. * Builds a preconfigured toast.
  86. *
  87. * @returns {obj} a `$mdToastPreset` with the chainable configuration methods:
  88. *
  89. * - $mdToastPreset#content(string) - sets toast content to string
  90. * - $mdToastPreset#action(string) - adds an action button. If clicked the promise (returned from `show()`) will resolve
  91. * with value 'ok'; otherwise it promise is resolved with 'true' after a hideDelay timeout.
  92. * - $mdToastPreset#highlightAction(boolean) - sets action button to be highlighted
  93. * - $mdToastPreset#capsule(boolean) - adds 'md-capsule' class to the toast (curved corners)
  94. * - $mdToastPreset#theme(string) - sets the theme on the toast to theme (default is `$mdThemingProvider`'s default theme)
  95. */
  96. /**
  97. * @ngdoc method
  98. * @name $mdToast#updateContent
  99. *
  100. * @description
  101. * Updates the content of an existing toast. Useful for updating things like counts, etc.
  102. *
  103. */
  104. /**
  105. * @ngdoc method
  106. * @name $mdToast#build
  107. *
  108. * @description
  109. * Creates a custom `$mdToastPreset` that you can configure.
  110. *
  111. * @returns {obj} a `$mdToastPreset` with the chainable configuration methods for shows' options (see below).
  112. */
  113. /**
  114. * @ngdoc method
  115. * @name $mdToast#show
  116. *
  117. * @description Shows the toast.
  118. *
  119. * @param {object} optionsOrPreset Either provide an `$mdToastPreset` returned from `simple()`
  120. * and `build()`, or an options object with the following properties:
  121. *
  122. * - `templateUrl` - `{string=}`: The url of an html template file that will
  123. * be used as the content of the toast. Restrictions: the template must
  124. * have an outer `md-toast` element.
  125. * - `template` - `{string=}`: Same as templateUrl, except this is an actual
  126. * template string.
  127. * - `scope` - `{object=}`: the scope to link the template / controller to. If none is specified, it will create a new child scope.
  128. * This scope will be destroyed when the toast is removed unless `preserveScope` is set to true.
  129. * - `preserveScope` - `{boolean=}`: whether to preserve the scope when the element is removed. Default is false
  130. * - `hideDelay` - `{number=}`: How many milliseconds the toast should stay
  131. * active before automatically closing. Set to 0 or false to have the toast stay open until
  132. * closed manually. Default: 3000.
  133. * - `position` - `{string=}`: Where to place the toast. Available: any combination
  134. * of 'bottom', 'left', 'top', 'right', 'fit'. Default: 'bottom left'.
  135. * - `controller` - `{string=}`: The controller to associate with this toast.
  136. * The controller will be injected the local `$mdToast.hide( )`, which is a function
  137. * used to hide the toast.
  138. * - `locals` - `{string=}`: An object containing key/value pairs. The keys will
  139. * be used as names of values to inject into the controller. For example,
  140. * `locals: {three: 3}` would inject `three` into the controller with the value
  141. * of 3.
  142. * - `bindToController` - `bool`: bind the locals to the controller, instead of passing them in. These values will not be available until after initialization.
  143. * - `resolve` - `{object=}`: Similar to locals, except it takes promises as values
  144. * and the toast will not open until the promises resolve.
  145. * - `controllerAs` - `{string=}`: An alias to assign the controller to on the scope.
  146. * - `parent` - `{element=}`: The element to append the toast to. Defaults to appending
  147. * to the root element of the application.
  148. *
  149. * @returns {promise} A promise that can be resolved with `$mdToast.hide()` or
  150. * rejected with `$mdToast.cancel()`. `$mdToast.hide()` will resolve either with a Boolean
  151. * value == 'true' or the value passed as an argument to `$mdToast.hide()`.
  152. * And `$mdToast.cancel()` will resolve the promise with a Boolean value == 'false'
  153. */
  154. /**
  155. * @ngdoc method
  156. * @name $mdToast#hide
  157. *
  158. * @description
  159. * Hide an existing toast and resolve the promise returned from `$mdToast.show()`.
  160. *
  161. * @param {*=} response An argument for the resolved promise.
  162. *
  163. * @returns {promise} a promise that is called when the existing element is removed from the DOM.
  164. * The promise is resolved with either a Boolean value == 'true' or the value passed as the
  165. * argument to `.hide()`.
  166. *
  167. */
  168. /**
  169. * @ngdoc method
  170. * @name $mdToast#cancel
  171. *
  172. * @description
  173. * Hide the existing toast and reject the promise returned from
  174. * `$mdToast.show()`.
  175. *
  176. * @param {*=} response An argument for the rejected promise.
  177. *
  178. * @returns {promise} a promise that is called when the existing element is removed from the DOM
  179. * The promise is resolved with a Boolean value == 'false'.
  180. *
  181. */
  182. function MdToastProvider($$interimElementProvider) {
  183. // Differentiate promise resolves: hide timeout (value == true) and hide action clicks (value == ok).
  184. var ACTION_RESOLVE = 'ok';
  185. var activeToastContent;
  186. var $mdToast = $$interimElementProvider('$mdToast')
  187. .setDefaults({
  188. methods: ['position', 'hideDelay', 'capsule', 'parent' ],
  189. options: toastDefaultOptions
  190. })
  191. .addPreset('simple', {
  192. argOption: 'content',
  193. methods: ['content', 'action', 'highlightAction', 'theme', 'parent'],
  194. options: /* ngInject */ ["$mdToast", "$mdTheming", function($mdToast, $mdTheming) {
  195. var opts = {
  196. template: [
  197. '<md-toast md-theme="{{ toast.theme }}" ng-class="{\'md-capsule\': toast.capsule}">',
  198. '<span flex>{{ toast.content }}</span>',
  199. '<md-button class="md-action" ng-if="toast.action" ng-click="toast.resolve()" ng-class="{\'md-highlight\': toast.highlightAction}">',
  200. '{{ toast.action }}',
  201. '</md-button>',
  202. '</md-toast>'
  203. ].join(''),
  204. controller: /* ngInject */ ["$scope", function mdToastCtrl($scope) {
  205. var self = this;
  206. $scope.$watch(function() { return activeToastContent; }, function() {
  207. self.content = activeToastContent;
  208. });
  209. this.resolve = function() {
  210. $mdToast.hide( ACTION_RESOLVE );
  211. };
  212. }],
  213. theme: $mdTheming.defaultTheme(),
  214. controllerAs: 'toast',
  215. bindToController: true
  216. };
  217. return opts;
  218. }]
  219. })
  220. .addMethod('updateContent', function(newContent) {
  221. activeToastContent = newContent;
  222. });
  223. toastDefaultOptions.$inject = ["$animate", "$mdToast", "$mdUtil"];
  224. return $mdToast;
  225. /* ngInject */
  226. function toastDefaultOptions($animate, $mdToast, $mdUtil) {
  227. var SWIPE_EVENTS = '$md.swipeleft $md.swiperight';
  228. return {
  229. onShow: onShow,
  230. onRemove: onRemove,
  231. position: 'bottom left',
  232. themable: true,
  233. hideDelay: 3000
  234. };
  235. function onShow(scope, element, options) {
  236. activeToastContent = options.content;
  237. element = $mdUtil.extractElementByName(element, 'md-toast', true);
  238. options.onSwipe = function(ev, gesture) {
  239. //Add swipeleft/swiperight class to element so it can animate correctly
  240. element.addClass('md-' + ev.type.replace('$md.',''));
  241. $mdUtil.nextTick($mdToast.cancel);
  242. };
  243. options.openClass = toastOpenClass(options.position);
  244. // 'top left' -> 'md-top md-left'
  245. options.parent.addClass(options.openClass);
  246. element.on(SWIPE_EVENTS, options.onSwipe);
  247. element.addClass(options.position.split(' ').map(function(pos) {
  248. return 'md-' + pos;
  249. }).join(' '));
  250. return $animate.enter(element, options.parent);
  251. }
  252. function onRemove(scope, element, options) {
  253. element.off(SWIPE_EVENTS, options.onSwipe);
  254. options.parent.removeClass(options.openClass);
  255. return (options.$destroy == true) ? element.remove() : $animate.leave(element);
  256. }
  257. function toastOpenClass(position) {
  258. return 'md-toast-open-' +
  259. (position.indexOf('top') > -1 ? 'top' : 'bottom');
  260. }
  261. }
  262. }
  263. MdToastProvider.$inject = ["$$interimElementProvider"];
  264. })(window, window.angular);