ion-autocomplete.min.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * ion-autocomplete 0.3.2
  3. * Copyright 2016 Danny Povolotski
  4. * Copyright modifications 2016 Guy Brand
  5. * https://github.com/guylabs/ion-autocomplete
  6. */
  7. !function () {
  8. "use strict";
  9. angular.module("ion-autocomplete", []).directive("ionAutocomplete", ["$ionicBackdrop", "$ionicScrollDelegate", "$document", "$q", "$parse", "$interpolate", "$ionicPlatform", "$compile", "$templateRequest", function (a, b, c, d, e, f, g, h, i) {
  10. return {
  11. require: ["ngModel", "ionAutocomplete"],
  12. restrict: "A",
  13. scope: {},
  14. bindToController: {
  15. ngModel: "=",
  16. externalModel: "=",
  17. templateData: "=",
  18. itemsMethod: "&",
  19. itemsClickedMethod: "&",
  20. itemsRemovedMethod: "&",
  21. modelToItemMethod: "&",
  22. cancelButtonClickedMethod: "&",
  23. placeholder: "@",
  24. cancelLabel: "@",
  25. selectItemsLabel: "@",
  26. selectedItemsLabel: "@"
  27. },
  28. controllerAs: "viewModel",
  29. controller: ["$attrs", "$timeout", "$scope", function (a, b, c) {
  30. var d = function (a, b) {
  31. return a ? a : b
  32. }, e = this;
  33. b(function () {
  34. e.placeholder = d(e.placeholder, "Click to enter a value..."), e.cancelLabel = d(e.cancelLabel, "Done"), e.selectItemsLabel = d(e.selectItemsLabel, "Select an item..."), e.selectedItemsLabel = d(e.selectedItemsLabel, f("Selected items{{maxSelectedItems ? ' (max. ' + maxSelectedItems + ')' : ''}}:")(e))
  35. }), this.maxSelectedItems = d(a.maxSelectedItems, void 0), this.templateUrl = d(a.templateUrl, void 0), this.itemsMethodValueKey = d(a.itemsMethodValueKey, void 0), this.itemValueKey = d(a.itemValueKey, void 0), this.itemViewValueKey = d(a.itemViewValueKey, void 0), this.componentId = d(a.componentId, void 0), this.loadingIcon = d(a.loadingIcon, void 0), this.manageExternally = d(a.manageExternally, "false"), this.ngModelOptions = d(c.$eval(a.ngModelOptions), {}), this.showLoadingIcon = !1, this.searchItems = [], this.selectedItems = [], this.searchQuery = void 0, this.isArray = function (a) {
  36. return angular.isArray(a)
  37. }
  38. }],
  39. link: function (f, j, k, l) {
  40. var m = l[0], n = l[1];
  41. n.randomCssClass = "ion-autocomplete-random-" + Math.floor(1e3 * Math.random() + 1);
  42. var o = ['<div class="ion-autocomplete-container ' + n.randomCssClass + ' modal" style="display: none;">', '<div class="bar bar-header item-input-inset">', '<label class="item-input-wrapper">', '<i class="icon ion-search placeholder-icon"></i>', '<input type="search" class="ion-autocomplete-search" ng-model="viewModel.searchQuery" ng-model-options="viewModel.ngModelOptions" placeholder="{{viewModel.placeholder}}"/>', "</label>", '<div class="ion-autocomplete-loading-icon" ng-if="viewModel.showLoadingIcon && viewModel.loadingIcon"><ion-spinner icon="{{viewModel.loadingIcon}}"></ion-spinner></div>', '<button class="ion-autocomplete-cancel button button-clear" ng-click="viewModel.cancelClick()">{{viewModel.cancelLabel}}</button>', "</div>", '<ion-content class="has-header">', '<ion-item class="item-divider">{{viewModel.selectedItemsLabel}}</ion-item>', '<ion-item ng-if="viewModel.isArray(viewModel.selectedItems)" ng-repeat="selectedItem in viewModel.selectedItems track by $index" class="item-icon-left item-icon-right item-text-wrap">', '<i class="icon ion-checkmark"></i>', "{{viewModel.getItemValue(selectedItem, viewModel.itemViewValueKey)}}", '<i class="icon ion-trash-a" style="cursor:pointer" ng-click="viewModel.removeItem($index)"></i>', "</ion-item>", '<ion-item ng-if="!viewModel.isArray(viewModel.selectedItems)" class="item-icon-left item-icon-right item-text-wrap">', '<i class="icon ion-checkmark"></i>', "{{viewModel.getItemValue(viewModel.selectedItems, viewModel.itemViewValueKey)}}", '<i class="icon ion-trash-a" style="cursor:pointer" ng-click="viewModel.removeItem(0)"></i>', "</ion-item>", '<ion-item class="item-divider" ng-if="viewModel.searchItems.length > 0">{{viewModel.selectItemsLabel}}</ion-item>', '<ion-item ng-repeat="item in viewModel.searchItems" item-height="55px" item-width="100%" ng-click="viewModel.selectItem(item)" class="item-text-wrap">', "{{viewModel.getItemValue(item, viewModel.itemViewValueKey)}}", "</ion-item>", "</ion-content>", "</div>"].join("");
  43. d.when().then(function () {
  44. return n.templateUrl ? i(n.templateUrl) : o
  45. }).then(function (i) {
  46. var l = h(angular.element(i))(f);
  47. c.find("body").append(l), n.getItemValue = function (a, b) {
  48. if (angular.isArray(a)) {
  49. var c = [];
  50. return angular.forEach(a, function (d) {
  51. b && angular.isObject(a) ? c.push(e(b)(d)) : c.push(d)
  52. }), c
  53. }
  54. return b && angular.isObject(a) ? e(b)(a) : a
  55. }, n.selectItem = function (a) {
  56. n.searchQuery = void 0, "1" != n.maxSelectedItems && angular.isArray(n.selectedItems) && n.maxSelectedItems == n.selectedItems.length || (t(n.selectedItems, n.itemValueKey, n.getItemValue(a, n.itemValueKey)) || ("1" == n.maxSelectedItems ? n.selectedItems = a : n.selectedItems = n.selectedItems.concat([a])), m.$setViewValue(n.selectedItems), m.$render(), 1 == n.maxSelectedItems && n.hideModal(), angular.isDefined(k.itemsClickedMethod) && n.itemsClickedMethod({
  57. callback: {
  58. item: a,
  59. selectedItems: angular.isArray(n.selectedItems) ? n.selectedItems.slice() : n.selectedItems,
  60. componentId: n.componentId
  61. }
  62. }))
  63. }, n.removeItem = function (a) {
  64. if (angular.isArray(n.selectedItems)) {
  65. var b = n.selectedItems.splice(a, 1)[0];
  66. n.selectedItems = n.selectedItems.slice()
  67. } else n.selectedItems = [];
  68. m.$setViewValue(n.selectedItems), m.$render(), angular.isDefined(k.itemsRemovedMethod) && n.itemsRemovedMethod({
  69. callback: {
  70. item: b,
  71. selectedItems: angular.isArray(n.selectedItems) ? n.selectedItems.slice() : n.selectedItems,
  72. componentId: n.componentId
  73. }
  74. })
  75. }, f.$watch("viewModel.searchQuery", function (a) {
  76. n.fetchSearchQuery(a, !1)
  77. }), n.fetchSearchQuery = function (a, c) {
  78. if (void 0 !== a && angular.isDefined(k.itemsMethod)) {
  79. n.showLoadingIcon = !0;
  80. var e = {query: a, isInitializing: c};
  81. n.componentId && (e = {query: a, isInitializing: c, componentId: n.componentId});
  82. var f = d.when(n.itemsMethod(e));
  83. f.then(function (a) {
  84. a && (a && a.data && (a = a.data), n.searchItems = n.getItemValue(a, n.itemsMethodValueKey), b.resize(), n.showLoadingIcon = !1)
  85. }, function (a) {
  86. return d.reject(a)
  87. })
  88. }
  89. };
  90. var o = !1;
  91. n.showModal = function () {
  92. if (!o) {
  93. a.retain(), angular.element(c[0].querySelector("div.ion-autocomplete-container." + n.randomCssClass)).css("display", "block"), f.$deregisterBackButton = g.registerBackButtonAction(function () {
  94. n.hideModal()
  95. }, 300);
  96. var d = angular.element(c[0].querySelector("div.ion-autocomplete-container." + n.randomCssClass + " input"));
  97. d.length > 0 && (d[0].focus(), setTimeout(function () {
  98. d[0].focus()
  99. }, 0)), b.resize(), o = !0
  100. }
  101. }, n.hideModal = function () {
  102. angular.element(c[0].querySelector("div.ion-autocomplete-container." + n.randomCssClass)).css("display", "none"), n.searchQuery = void 0, a.release(), f.$deregisterBackButton && f.$deregisterBackButton(), o = !1
  103. };
  104. var p = {moved: !1, startX: 0, startY: 0}, q = function (a) {
  105. p.moved = !1, "undefined" != typeof a.originalEvent && (a = a.originalEvent), p.startX = a.touches[0].clientX, p.startY = a.touches[0].clientY
  106. }, r = function (a) {
  107. "undefined" != typeof a.originalEvent && (a = a.originalEvent), (Math.abs(a.touches[0].clientX - p.startX) > 10 || Math.abs(a.touches[0].clientY - p.startY) > 10) && (p.moved = !0)
  108. }, s = function (a) {
  109. p.moved || (a.preventDefault(), a.stopPropagation(), n.fetchSearchQuery("", !0), n.showModal())
  110. }, t = function (a, b, c) {
  111. if (angular.isArray(a))for (var d = 0; d < a.length; d++)if (n.getItemValue(a[d], b) === c)return !0;
  112. return !1
  113. }, u = function (a) {
  114. var b = d.when(n.modelToItemMethod({modelValue: a}));
  115. b.then(function (a) {
  116. n.selectItem(a)
  117. }, function (a) {
  118. return d.reject(a)
  119. })
  120. };
  121. "false" == n.manageExternally && (j.bind("touchstart", q), j.bind("touchmove", r), j.bind("touchend click focus", s)), n.cancelClick = function () {
  122. n.hideModal(), angular.isDefined(k.cancelButtonClickedMethod) && n.cancelButtonClickedMethod({
  123. callback: {
  124. selectedItems: angular.isArray(n.selectedItems) ? n.selectedItems.slice() : n.selectedItems,
  125. componentId: n.componentId
  126. }
  127. })
  128. }, f.$watch("viewModel.externalModel", function (a) {
  129. return angular.isArray(a) && 0 == a.length ? (n.selectedItems = [], m.$setViewValue(n.selectedItems), void m.$render()) : void(a && angular.isDefined(k.modelToItemMethod) && (angular.isArray(a) ? (n.selectedItems = [], angular.forEach(a, function (a) {
  130. u(a)
  131. })) : u(a)))
  132. }), f.$on("$destroy", function () {
  133. l.remove()
  134. }), m.$render = function () {
  135. j.val(n.getItemValue(m.$viewValue, n.itemViewValueKey))
  136. }, m.$formatters.push(function (a) {
  137. var b = n.getItemValue(a, n.itemViewValueKey);
  138. return void 0 == b ? "" : b
  139. }), m.$parsers.push(function (a) {
  140. return n.getItemValue(a, n.itemValueKey)
  141. })
  142. })
  143. }
  144. }
  145. }])
  146. }();