angular-strap.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * AngularStrap - Twitter Bootstrap directives for AngularJS
  3. * @version v0.7.5 - 2013-07-21
  4. * @link http://mgcrea.github.com/angular-strap
  5. * @author Olivier Louvignes <olivier@mg-crea.com>
  6. * @license MIT License, http://www.opensource.org/licenses/MIT
  7. */
  8. angular.module('$strap.config', []).value('$strapConfig', {});
  9. angular.module('$strap.filters', ['$strap.config']);
  10. angular.module('$strap.directives', ['$strap.config']);
  11. angular.module('$strap', [
  12. '$strap.filters',
  13. '$strap.directives',
  14. '$strap.config'
  15. ]);
  16. 'use strict';
  17. angular.module('$strap.directives').factory('$modal', [
  18. '$rootScope',
  19. '$compile',
  20. '$http',
  21. '$timeout',
  22. '$q',
  23. '$templateCache',
  24. '$strapConfig',
  25. function ($rootScope, $compile, $http, $timeout, $q, $templateCache, $strapConfig) {
  26. var ModalFactory = function ModalFactory(config) {
  27. function Modal(config) {
  28. var options = angular.extend({ show: true }, $strapConfig.modal, config), scope = options.scope ? options.scope : $rootScope.$new(), templateUrl = options.template;
  29. return $q.when($templateCache.get(templateUrl) || $http.get(templateUrl, { cache: true }).then(function (res) {
  30. return res.data;
  31. })).then(function onSuccess(template) {
  32. var id = templateUrl.replace('.html', '').replace(/[\/|\.|:]/g, '-') + '-' + scope.$id;
  33. // grafana change, removed fade
  34. var $modal = $('<div class="modal hide" tabindex="-1"></div>').attr('id', id).html(template);
  35. if (options.modalClass)
  36. $modal.addClass(options.modalClass);
  37. $('body').append($modal);
  38. $timeout(function () {
  39. $compile($modal)(scope);
  40. });
  41. scope.$modal = function (name) {
  42. $modal.modal(name);
  43. };
  44. angular.forEach([
  45. 'show',
  46. 'hide'
  47. ], function (name) {
  48. scope[name] = function () {
  49. $modal.modal(name);
  50. };
  51. });
  52. scope.dismiss = scope.hide;
  53. angular.forEach([
  54. 'show',
  55. 'shown',
  56. 'hide',
  57. 'hidden'
  58. ], function (name) {
  59. $modal.on(name, function (ev) {
  60. scope.$emit('modal-' + name, ev);
  61. });
  62. });
  63. $modal.on('shown', function (ev) {
  64. $('input[autofocus], textarea[autofocus]', $modal).first().trigger('focus');
  65. });
  66. $modal.on('hidden', function (ev) {
  67. if (!options.persist)
  68. scope.$destroy();
  69. });
  70. scope.$on('$destroy', function () {
  71. $modal.remove();
  72. });
  73. $modal.modal(options);
  74. return $modal;
  75. });
  76. }
  77. return new Modal(config);
  78. };
  79. return ModalFactory;
  80. }
  81. ])
  82. 'use strict';
  83. angular.module('$strap.directives').directive('bsTooltip', [
  84. '$parse',
  85. '$compile',
  86. function ($parse, $compile) {
  87. return {
  88. restrict: 'A',
  89. scope: true,
  90. link: function postLink(scope, element, attrs, ctrl) {
  91. var getter = $parse(attrs.bsTooltip), setter = getter.assign, value = getter(scope);
  92. scope.$watch(attrs.bsTooltip, function (newValue, oldValue) {
  93. if (newValue !== oldValue) {
  94. value = newValue;
  95. }
  96. });
  97. // Grafana change, always hide other tooltips
  98. if (true) {
  99. element.on('show', function (ev) {
  100. $('.tooltip.in').each(function () {
  101. var $this = $(this), tooltip = $this.data('tooltip');
  102. if (tooltip && !tooltip.$element.is(element)) {
  103. $this.tooltip('hide');
  104. }
  105. });
  106. });
  107. }
  108. element.tooltip({
  109. title: function () {
  110. return angular.isFunction(value) ? value.apply(null, arguments) : value;
  111. },
  112. html: true,
  113. container: 'body', // Grafana change
  114. });
  115. var tooltip = element.data('tooltip');
  116. tooltip.show = function () {
  117. var r = $.fn.tooltip.Constructor.prototype.show.apply(this, arguments);
  118. this.tip().data('tooltip', this);
  119. return r;
  120. };
  121. scope._tooltip = function (event) {
  122. element.tooltip(event);
  123. };
  124. scope.hide = function () {
  125. element.tooltip('hide');
  126. };
  127. scope.show = function () {
  128. element.tooltip('show');
  129. };
  130. scope.dismiss = scope.hide;
  131. }
  132. };
  133. }
  134. ]);
  135. 'use strict';
  136. angular.module('$strap.directives').directive('bsTypeahead', [
  137. '$parse',
  138. function ($parse) {
  139. return {
  140. restrict: 'A',
  141. require: '?ngModel',
  142. link: function postLink(scope, element, attrs, controller) {
  143. var getter = $parse(attrs.bsTypeahead), setter = getter.assign, value = getter(scope);
  144. scope.$watch(attrs.bsTypeahead, function (newValue, oldValue) {
  145. if (newValue !== oldValue) {
  146. value = newValue;
  147. }
  148. });
  149. element.attr('data-provide', 'typeahead');
  150. element.typeahead({
  151. source: function (query) {
  152. return angular.isFunction(value) ? value.apply(null, arguments) : value;
  153. },
  154. minLength: attrs.minLength || 1,
  155. items: attrs.items,
  156. updater: function (value) {
  157. if (controller) {
  158. scope.$apply(function () {
  159. controller.$setViewValue(value);
  160. });
  161. }
  162. scope.$emit('typeahead-updated', value);
  163. return value;
  164. }
  165. });
  166. var typeahead = element.data('typeahead');
  167. typeahead.lookup = function (ev) {
  168. var items;
  169. this.query = this.$element.val() || '';
  170. if (this.query.length < this.options.minLength) {
  171. return this.shown ? this.hide() : this;
  172. }
  173. items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source;
  174. return items ? this.process(items) : this;
  175. };
  176. if (!!attrs.matchAll) {
  177. typeahead.matcher = function (item) {
  178. return true;
  179. };
  180. }
  181. if (attrs.minLength === '0') {
  182. setTimeout(function () {
  183. element.on('focus', function () {
  184. element.val().length === 0 && setTimeout(element.typeahead.bind(element, 'lookup'), 200);
  185. });
  186. });
  187. }
  188. }
  189. };
  190. }
  191. ]);