This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathng-suggest.min.js
1 lines (1 loc) · 4.3 KB
/
ng-suggest.min.js
1
"use strict";angular.module("ngSuggest",[]).value("ngSuggest.version","0.1.2"),angular.module("ngSuggest").directive("seealsoApi",["SeeAlso",function(SeeAlso){return{restrict:"A",scope:{api:"@seealsoApi",id:"@seealsoId",jsonp:"@jsonp"},templateUrl:function(elem,attrs){return attrs.templateUrl?attrs.templateUrl:"template/seealso-response.html"},link:function(scope){function request(){scope.service.suggest(scope.id).then(function(response){scope.query=response.query,scope.links=response.values})}scope.$watch("id",function(){scope.service&&request()}),scope.$watch("api",function(service){angular.isObject(service)&&service instanceof SeeAlso?scope.service=service:(angular.isObject(service)||(service={url:service,jsonp:scope.jsonp}),scope.service=new SeeAlso(service)),request()})}}}]),angular.module("ngSuggest").directive("suggestTypeahead",["OpenSearchSuggestions","$injector",function(OpenSearchSuggestions,$injector){return{restrict:"A",scope:{api:"=suggestTypeahead",jsonp:"@jsonp"},require:"ngModel",compile:function(element,attrs){function suggestLink(scope){scope.$watch("api",function(service){scope.service=angular.isObject(service)?service:new OpenSearchSuggestions({url:service,jsonp:scope.jsonp})}),scope.$parent[suggestFunction]=function(value){var s=scope.service.suggest(value);return s.then(function(suggestions){return suggestions.values})}}var suggestFunction="suggest_"+Math.random().toString(36).slice(2);attrs.typeaheadTemplateUrl||attrs.$set("typeaheadTemplateUrl","template/suggest-typeahead.html");var typeaheadLink=function(){};if(!attrs.typeahead){if(!$injector.has("typeaheadDirective"))throw new Error("ui.bootstrap.typehead directive required!");var expr="item.label for item in "+suggestFunction+"($viewValue) | filter:$viewValue";attrs.$set("typeahead",expr);var directive=$injector.get("typeaheadDirective")[0];typeaheadLink=directive.compile(element,attrs)}return function(scope,element,attrs,modelCtrl){suggestLink(scope,element,attrs),typeaheadLink(scope.$parent,element,attrs,modelCtrl)}}}}]),angular.module("ngSuggest").filter("htmlescape",function(){return function(input){return input?input.replace(/[<>]/g,"-"):""}}),angular.module("ngSuggest").factory("OpenSearchSuggestions",["$http","$q",function($http,$q){var transformSuggestions=function(data){var suggestions={query:data[0],values:[]};angular.isArray(data[2])||(data[2]=[]),angular.isArray(data[3])||(data[3]=[]);for(var i=0;i<data[1].length;i++)suggestions.values.push({label:data[1][i],description:data[2][i],url:data[3][i]});return suggestions},OpenSearchSuggestions=function(args){angular.isObject(args)||(args={url:args}),this.url=args.url,this.url&&-1==this.url.indexOf("{searchTerms}")&&(this.url+="{searchTerms}"),this.transform=args.transform?args.transform:transformSuggestions;var jsonp=args.jsonp;jsonp&&(jsonp===!0||angular.isNumber(jsonp)||jsonp.match(/^\d/))&&(jsonp="callback"),this.jsonp=jsonp};return OpenSearchSuggestions.prototype={suggest:function(searchTerms){if(!this.url)return $q.reject(null);var url=this.url,transform=this.transform,get=$http.get;return this.jsonp&&(get=$http.jsonp,url+=-1==url.indexOf("?")?"?":"&",url+=this.jsonp+"=JSON_CALLBACK"),url=url.replace("{searchTerms}",decodeURIComponent(searchTerms)),get(url).then(function(response){try{return transform(response.data,searchTerms)}catch(e){return $q.reject(e)}},function(response){return $q.reject(response.data)})}},OpenSearchSuggestions}]),angular.module("ngSuggest").factory("SeeAlso",["OpenSearchSuggestions",function(OpenSearchSuggestions){function SeeAlso(args){angular.isObject(args)||(args={url:args}),args.url+="?id={searchTerms}&format=seealso",OpenSearchSuggestions.call(this,args)}return SeeAlso.prototype=new OpenSearchSuggestions,SeeAlso}]),angular.module("ngSuggest").run(["$templateCache",function($templateCache){$templateCache.put("template/seealso-response.html",'<ul ng-if="links"><li ng-repeat="link in links"><a ng-if="link.url" href="{{link.url}}" title="{{link.description}}">{{link.label}}</a> <span ng-if="!link.url">{{link.label}}</span></li></ul>'),$templateCache.put("template/suggest-typeahead.html",'<a tabindex="-1" class="suggest-typeahead-item"><div bind-html-unsafe="match.label | htmlescape | typeaheadHighlight:query" class="suggest-typeahead-label"></div><i ng-if="match.model.description" class="suggest-typeahead-description">{{match.model.description}}</i></a>')}]);