diff --git a/.jshintrc b/.jshintrc
index b25013f6..fd87e3cc 100644
--- a/.jshintrc
+++ b/.jshintrc
@@ -14,7 +14,6 @@
"quotmark": "single",
"regexp": true,
"undef": true,
- "unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
diff --git a/Gruntfile.js b/Gruntfile.js
index ec361e00..9c51abed 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -37,7 +37,7 @@ module.exports = function (grunt) {
jshint: {
all:[
'gruntFile.js',
- 'src/**/*.js',
+ 'angucomplete.js',
'test/**/*Spec.js'
],
options: {
diff --git a/angucomplete.js b/angucomplete.js
index cb31f6ac..759a51d6 100644
--- a/angucomplete.js
+++ b/angucomplete.js
@@ -1,228 +1,227 @@
/**
- * Angucomplete
+ * angucomplete-extra
* Autocomplete directive for AngularJS
- * By Daryl Rowland
+ * This is a fork of Daryl Rowland's angucomplete with some extra features.
+ * By Hidenari Nozaki
*/
-
-angular.module('angucomplete', [] )
- .directive('angucomplete', function ($parse, $http, $sce, $timeout) {
- return {
- restrict: 'EA',
- scope: {
- "id": "@id",
- "placeholder": "@placeholder",
- "selectedObject": "=selectedobject",
- "url": "@url",
- "dataField": "@datafield",
- "titleField": "@titlefield",
- "descriptionField": "@descriptionfield",
- "imageField": "@imagefield",
- "inputClass": "@inputclass",
- "userPause": "@pause",
- "localData": "=localdata",
- "searchFields": "@searchfields",
- "minLengthUser": "@minlength",
- "matchClass": "@matchclass"
- },
- template: '
Searching...
No results found
{{ result.title }}
{{result.description}}
',
-
- link: function($scope, elem, attrs) {
- $scope.lastSearchTerm = null;
- $scope.currentIndex = null;
- $scope.justChanged = false;
- $scope.searchTimer = null;
- $scope.searching = false;
- $scope.pause = 500;
- $scope.minLength = 3;
- $scope.searchStr = null;
-
- if ($scope.minLengthUser && $scope.minLengthUser != "") {
- $scope.minLength = $scope.minLengthUser;
+'use strict';
+
+angular.module('angucomplete', [] ).directive('angucomplete', function ($parse, $http, $sce, $timeout) {
+ return {
+ restrict: 'EA',
+ scope: {
+ id: '@id',
+ placeholder: '@placeholder',
+ selectedObject: '=selectedobject',
+ url: '@url',
+ dataField: '@datafield',
+ titleField: '@titlefield',
+ descriptionField: '@descriptionfield',
+ imageField: '@imagefield',
+ inputClass: '@inputclass',
+ userPause: '@pause',
+ localData: '=localdata',
+ searchFields: '@searchfields',
+ minLengthUser: '@minlength',
+ matchClass: '@matchclass'
+ },
+ template: 'Searching...
No results found
{{ result.title }}
{{result.description}}
',
+ link: function($scope, elem, attrs) {
+ $scope.lastSearchTerm = null;
+ $scope.currentIndex = null;
+ $scope.justChanged = false;
+ $scope.searchTimer = null;
+ $scope.searching = false;
+ $scope.pause = 500;
+ $scope.minLength = 3;
+ $scope.searchStr = null;
+
+ if ($scope.minLengthUser && $scope.minLengthUser !== '') {
+ $scope.minLength = $scope.minLengthUser;
+ }
+
+ if ($scope.userPause) {
+ $scope.pause = $scope.userPause;
+ }
+
+ var isNewSearchNeeded = function(newTerm, oldTerm) {
+ return newTerm.length >= $scope.minLength && newTerm !== oldTerm;
+ };
+
+ $scope.processResults = function(responseData, str) {
+ if (responseData && responseData.length > 0) {
+ $scope.results = [];
+
+ var titleFields = [];
+ if ($scope.titleField && $scope.titleField !== '') {
+ titleFields = $scope.titleField.split(',');
+ }
+
+ for (var i = 0; i < responseData.length; i++) {
+ // Get title variables
+ var titleCode = [];
+
+ for (var t = 0; t < titleFields.length; t++) {
+ titleCode.push(responseData[i][titleFields[t]]);
}
- if ($scope.userPause) {
- $scope.pause = $scope.userPause;
+ var description = '';
+ if ($scope.descriptionField) {
+ description = responseData[i][$scope.descriptionField];
}
- isNewSearchNeeded = function(newTerm, oldTerm) {
- return newTerm.length >= $scope.minLength && newTerm != oldTerm
+ var image = '';
+ if ($scope.imageField) {
+ image = responseData[i][$scope.imageField];
}
- $scope.processResults = function(responseData, str) {
- if (responseData && responseData.length > 0) {
- $scope.results = [];
-
- var titleFields = [];
- if ($scope.titleField && $scope.titleField != "") {
- titleFields = $scope.titleField.split(",");
- }
-
- for (var i = 0; i < responseData.length; i++) {
- // Get title variables
- var titleCode = [];
-
- for (var t = 0; t < titleFields.length; t++) {
- titleCode.push(responseData[i][titleFields[t]]);
- }
-
- var description = "";
- if ($scope.descriptionField) {
- description = responseData[i][$scope.descriptionField];
- }
-
- var image = "";
- if ($scope.imageField) {
- image = responseData[i][$scope.imageField];
- }
-
- var text = titleCode.join(' ');
- if ($scope.matchClass) {
- var re = new RegExp(str, 'i');
- var strPart = text.match(re)[0];
- text = $sce.trustAsHtml(text.replace(re, ''+ strPart +''));
- }
-
- var resultRow = {
- title: text,
- description: description,
- image: image,
- originalObject: responseData[i]
- }
-
- $scope.results[$scope.results.length] = resultRow;
- }
-
-
- } else {
- $scope.results = [];
- }
+ var text = titleCode.join(' ');
+ if ($scope.matchClass) {
+ var re = new RegExp(str, 'i');
+ var strPart = text.match(re)[0];
+ text = $sce.trustAsHtml(text.replace(re, ''+ strPart +''));
}
- $scope.searchTimerComplete = function(str) {
- // Begin the search
+ var resultRow = {
+ title: text,
+ description: description,
+ image: image,
+ originalObject: responseData[i]
+ };
- if (str.length >= $scope.minLength) {
- if ($scope.localData) {
- var searchFields = $scope.searchFields.split(",");
+ $scope.results[$scope.results.length] = resultRow;
+ }
- var matches = [];
- for (var i = 0; i < $scope.localData.length; i++) {
- var match = false;
+ } else {
+ $scope.results = [];
+ }
+ };
- for (var s = 0; s < searchFields.length; s++) {
- match = match || ($scope.localData[i][searchFields[s]].toLowerCase().indexOf(str.toLowerCase()) >= 0);
- }
+ $scope.searchTimerComplete = function(str) {
+ // Begin the search
- if (match) {
- matches[matches.length] = $scope.localData[i];
- }
- }
+ if (str.length >= $scope.minLength) {
+ if ($scope.localData) {
+ var searchFields = $scope.searchFields.split(',');
- $scope.searching = false;
- $scope.processResults(matches, str);
+ var matches = [];
- } else {
- $http.get($scope.url + str, {}).
- success(function(responseData, status, headers, config) {
- $scope.searching = false;
- $scope.processResults(responseData[$scope.dataField], str);
- }).
- error(function(data, status, headers, config) {
- console.log("error");
- });
- }
- }
+ for (var i = 0; i < $scope.localData.length; i++) {
+ var match = false;
- }
+ for (var s = 0; s < searchFields.length; s++) {
+ match = match || ($scope.localData[i][searchFields[s]].toLowerCase().indexOf(str.toLowerCase()) >= 0);
+ }
- $scope.hoverRow = function(index) {
- $scope.currentIndex = index;
+ if (match) {
+ matches[matches.length] = $scope.localData[i];
+ }
}
- $scope.keyPressed = function(event) {
- if (!(event.which == 38 || event.which == 40 || event.which == 13)) {
- if (!$scope.searchStr || $scope.searchStr == "") {
- $scope.showDropdown = false;
- $scope.lastSearchTerm = null
- } else if (isNewSearchNeeded($scope.searchStr, $scope.lastSearchTerm)) {
- $scope.lastSearchTerm = $scope.searchStr
- $scope.showDropdown = true;
- $scope.currentIndex = -1;
- $scope.results = [];
-
- if ($scope.searchTimer) {
- $timeout.cancel($scope.searchTimer);
- }
-
- $scope.searching = true;
-
- $scope.searchTimer = $timeout(function() {
- $scope.searchTimerComplete($scope.searchStr);
- }, $scope.pause);
- }
- } else {
- event.preventDefault();
- }
- }
+ $scope.searching = false;
+ $scope.processResults(matches, str);
+
+ } else {
+ $http.get($scope.url + str, {}).
+ success(function(responseData, status, headers, config) {
+ $scope.searching = false;
+ $scope.processResults(responseData[$scope.dataField], str);
+ }).
+ error(function(data, status, headers, config) {
+ console.log('error');
+ });
+ }
+ }
+
+ };
- $scope.selectResult = function(result) {
- if ($scope.matchClass) {
- result.title = result.title.toString().replace(/(<([^>]+)>)/ig, '');
- }
- $scope.searchStr = $scope.lastSearchTerm = result.title;
- $scope.selectedObject = result;
- $scope.showDropdown = false;
- $scope.results = [];
- //$scope.$apply();
+ $scope.hoverRow = function(index) {
+ $scope.currentIndex = index;
+ };
+
+ $scope.keyPressed = function(event) {
+ if (!(event.which === 38 || event.which === 40 || event.which === 13)) {
+ if (!$scope.searchStr || $scope.searchStr === '') {
+ $scope.showDropdown = false;
+ $scope.lastSearchTerm = null;
+ } else if (isNewSearchNeeded($scope.searchStr, $scope.lastSearchTerm)) {
+ $scope.lastSearchTerm = $scope.searchStr;
+ $scope.showDropdown = true;
+ $scope.currentIndex = -1;
+ $scope.results = [];
+
+ if ($scope.searchTimer) {
+ $timeout.cancel($scope.searchTimer);
}
- var inputField = elem.find('input');
-
- inputField.on('keyup', $scope.keyPressed);
-
- elem.on("keyup", function (event) {
- if(event.which === 40) {
- if (($scope.currentIndex + 1) < $scope.results.length) {
- $scope.currentIndex ++;
- $scope.$apply();
- event.preventDefault;
- event.stopPropagation();
- }
-
- $scope.$apply();
- } else if(event.which == 38) {
- if ($scope.currentIndex >= 1) {
- $scope.currentIndex --;
- $scope.$apply();
- event.preventDefault;
- event.stopPropagation();
- }
-
- } else if (event.which == 13) {
- if ($scope.currentIndex >= 0 && $scope.currentIndex < $scope.results.length) {
- $scope.selectResult($scope.results[$scope.currentIndex]);
- $scope.$apply();
- event.preventDefault;
- event.stopPropagation();
- } else {
- $scope.results = [];
- $scope.$apply();
- event.preventDefault;
- event.stopPropagation();
- }
-
- } else if (event.which == 27) {
- $scope.results = [];
- $scope.showDropdown = false;
- $scope.$apply();
- } else if (event.which == 8) {
- $scope.selectedObject = null;
- $scope.$apply();
- }
- });
+ $scope.searching = true;
+
+ $scope.searchTimer = $timeout(function() {
+ $scope.searchTimerComplete($scope.searchStr);
+ }, $scope.pause);
+ }
+ } else {
+ event.preventDefault();
+ }
+ };
+ $scope.selectResult = function(result) {
+ if ($scope.matchClass) {
+ result.title = result.title.toString().replace(/(<([^>]+)>)/ig, '');
+ }
+ $scope.searchStr = $scope.lastSearchTerm = result.title;
+ $scope.selectedObject = result;
+ $scope.showDropdown = false;
+ $scope.results = [];
+ //$scope.$apply();
+ };
+
+ var inputField = elem.find('input');
+
+ inputField.on('keyup', $scope.keyPressed);
+
+ elem.on('keyup', function (event) {
+ if(event.which === 40) {
+ if (($scope.currentIndex + 1) < $scope.results.length) {
+ $scope.currentIndex ++;
+ $scope.$apply();
+ event.preventDefault();
+ event.stopPropagation();
+ }
+
+ $scope.$apply();
+ } else if(event.which === 38) {
+ if ($scope.currentIndex >= 1) {
+ $scope.currentIndex --;
+ $scope.$apply();
+ event.preventDefault();
+ event.stopPropagation();
+ }
+
+ } else if (event.which === 13) {
+ if ($scope.currentIndex >= 0 && $scope.currentIndex < $scope.results.length) {
+ $scope.selectResult($scope.results[$scope.currentIndex]);
+ $scope.$apply();
+ event.preventDefault();
+ event.stopPropagation();
+ } else {
+ $scope.results = [];
+ $scope.$apply();
+ event.preventDefault();
+ event.stopPropagation();
+ }
+
+ } else if (event.which === 27) {
+ $scope.results = [];
+ $scope.showDropdown = false;
+ $scope.$apply();
+ } else if (event.which === 8) {
+ $scope.selectedObject = null;
+ $scope.$apply();
}
- };
+ });
+ }
+ };
});