angular-strap.js 6.4 KB

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