Skip to content

Commit

Permalink
Merge pull request #13 from ghiden/master
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
Daryl Rowland committed Feb 18, 2014
2 parents 9e6ec01 + a3b84ae commit 599cf7e
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
components
.*.swp
.DS_Store
46 changes: 46 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"node": true,
"browser": true,
"esnext": true,
"bitwise": true,
"camelcase": false,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"white": false,
"globals": {
"angular": false,
"_": false,
"_V_": false,
"afterEach": false,
"beforeEach": false,
"confirm": false,
"context": false,
"describe": false,
"expect": false,
"it": false,
"inject": false,
"jasmine": false,
"JSHINT": false,
"mostRecentAjaxRequest": false,
"qq": false,
"runs": false,
"spyOn": false,
"spyOnEvent": false,
"waitsFor": false,
"xdescribe": false,
"$": false,
"dump": false
}
}
61 changes: 61 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = function (grunt) {
'use strict';

var initConfig;

// Loading external tasks
require('load-grunt-tasks')(grunt);

// Project configuration.
initConfig = {
bower: 'bower_components',
pkg: grunt.file.readJSON('package.json'),
watch: {
test: {
// Lint & run unit tests in Karma
// Just running `$ grunt watch` will only lint your code; to run tests
// on watch, use `$ grunt watch:karma` to start a Karma server first
files: ['src/select2.js', 'test/select2Spec.js'],
tasks: ['jshint', 'karma:unit:run']
}
},
karma: {
options: {
configFile: 'test/karma.conf.js',
browsers: ['PhantomJS']
},
unit: {
singleRun: true
},
watch: {
autoWatch: true
},
server: {
background: true
}
},
jshint: {
all:[
'gruntFile.js',
'src/**/*.js',
'test/**/*Spec.js'
],
options: {
jshintrc: '.jshintrc'
}
},
changelog: {
options: {
dest: 'CHANGELOG.md'
}
}
};

// Register tasks
grunt.registerTask('default', ['jshint', 'karma:unit']);
grunt.registerTask('watch', ['jshint', 'karma:watch']);

grunt.initConfig(initConfig);
};


50 changes: 18 additions & 32 deletions angucomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

angular.module('angucomplete', [] )
.directive('angucomplete', function ($parse, $http, $sce) {
.directive('angucomplete', function ($parse, $http, $sce, $timeout) {
return {
restrict: 'EA',
scope: {
Expand All @@ -24,15 +24,17 @@ angular.module('angucomplete', [] )
"minLengthUser": "@minlength",
"matchClass": "@matchclass"
},
template: '<div class="angucomplete-holder"><input id="{{id}}_value" ng-model="searchStr" type="text" placeholder="{{placeholder}}" class="{{inputClass}}" ng-keyup="keyPressed($event)"/><div id="{{id}}_dropdown" class="angucomplete-dropdown" ng-if="showDropdown"><div class="angucomplete-searching" ng-show="searching">Searching...</div><div class="angucomplete-searching" ng-show="!searching && (!results || results.length == 0)">No results found</div><div class="angucomplete-row" ng-repeat="result in results" ng-click="selectResult(result)" ng-mouseover="hoverRow()" ng-class="{\'angucomplete-selected-row\': $index == currentIndex}"><div ng-if="imageField" class="angucomplete-image-holder"><img ng-if="result.image && result.image != \'\'" ng-src="{{result.image}}" class="angucomplete-image"/><div ng-if="!result.image && result.image != \'\'" class="angucomplete-image-default"></div></div><div class="angucomplete-title" ng-if="matchClass" ng-bind-html="result.title"></div><div class="angucomplete-title" ng-if="!matchClass">{{ result.title }}</div><div ng-if="result.description && result.description != \'\'" class="angucomplete-description">{{result.description}}</div></div></div></div>',
controller: function ( $scope ) {
template: '<div class="angucomplete-holder"><input id="{{id}}_value" ng-model="searchStr" type="text" placeholder="{{placeholder}}" class="{{inputClass}}"/><div id="{{id}}_dropdown" class="angucomplete-dropdown" ng-if="showDropdown"><div class="angucomplete-searching" ng-show="searching">Searching...</div><div class="angucomplete-searching" ng-show="!searching && (!results || results.length == 0)">No results found</div><div class="angucomplete-row" ng-repeat="result in results" ng-click="selectResult(result)" ng-mouseover="hoverRow()" ng-class="{\'angucomplete-selected-row\': $index == currentIndex}"><div ng-if="imageField" class="angucomplete-image-holder"><img ng-if="result.image && result.image != \'\'" ng-src="{{result.image}}" class="angucomplete-image"/><div ng-if="!result.image && result.image != \'\'" class="angucomplete-image-default"></div></div><div class="angucomplete-title" ng-if="matchClass" ng-bind-html="result.title"></div><div class="angucomplete-title" ng-if="!matchClass">{{ result.title }}</div><div ng-if="result.description && result.description != \'\'" class="angucomplete-description">{{result.description}}</div></div></div></div>',

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;
Expand All @@ -42,15 +44,6 @@ angular.module('angucomplete', [] )
$scope.pause = $scope.userPause;
}

extractValue = function(obj, key) {
if (key) {
value = eval("obj." + key);
} else {
value = obj;
}
return value;
}

isNewSearchNeeded = function(newTerm, oldTerm) {
return newTerm.length >= $scope.minLength && newTerm != oldTerm
}
Expand All @@ -66,26 +59,23 @@ angular.module('angucomplete', [] )

for (var i = 0; i < responseData.length; i++) {
// Get title variables
var titleCode = "";
var titleCode = [];

for (var t = 0; t < titleFields.length; t++) {
if (t > 0) {
titleCode = titleCode + " + ' ' + ";
}
titleCode = titleCode + "responseData[i]." + titleFields[t];
titleCode.push(responseData[i][titleFields[t]]);
}

var description = "";
if ($scope.descriptionField) {
description = extractValue(responseData[i], $scope.descriptionField);
description = responseData[i][$scope.descriptionField];
}

var image = "";
if ($scope.imageField) {
image = extractValue(responseData[i], $scope.imageField);
image = responseData[i][$scope.imageField];
}

var text = eval(titleCode);
var text = titleCode.join(' ');
if ($scope.matchClass) {
var re = new RegExp(str, 'i');
var strPart = text.match(re)[0];
Expand Down Expand Up @@ -121,8 +111,7 @@ angular.module('angucomplete', [] )
var match = false;

for (var s = 0; s < searchFields.length; s++) {
var evalStr = 'match = match || ($scope.localData[i].' + searchFields[s] + '.toLowerCase().indexOf("' + str.toLowerCase() + '") >= 0)';
eval(evalStr);
match = match || ($scope.localData[i][searchFields[s]].toLowerCase().indexOf(str.toLowerCase()) >= 0);
}

if (match) {
Expand All @@ -132,15 +121,12 @@ angular.module('angucomplete', [] )

$scope.searching = false;
$scope.processResults(matches, str);
$scope.$apply();


} else {
$http.get($scope.url + str, {}).
success(function(responseData, status, headers, config) {
$scope.searching = false;
data = extractValue(responseData, $scope.dataField)
$scope.processResults(data, str);
$scope.processResults(responseData[$scope.dataField], str);
}).
error(function(data, status, headers, config) {
console.log("error");
Expand All @@ -166,12 +152,12 @@ angular.module('angucomplete', [] )
$scope.results = [];

if ($scope.searchTimer) {
clearTimeout($scope.searchTimer);
$timeout.cancel($scope.searchTimer);
}

$scope.searching = true;

$scope.searchTimer = setTimeout(function() {
$scope.searchTimer = $timeout(function() {
$scope.searchTimerComplete($scope.searchStr);
}, $scope.pause);
}
Expand All @@ -190,11 +176,12 @@ angular.module('angucomplete', [] )
$scope.results = [];
//$scope.$apply();
}
},

link: function($scope, elem, attrs, ctrl) {
var inputField = elem.find('input');

elem.bind("keyup", function (event) {
inputField.on('keyup', $scope.keyPressed);

elem.on("keyup", function (event) {
if(event.which === 40) {
if (($scope.currentIndex + 1) < $scope.results.length) {
$scope.currentIndex ++;
Expand Down Expand Up @@ -235,7 +222,6 @@ angular.module('angucomplete', [] )
}
});


}
};
});
Expand Down
9 changes: 8 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@
"example",
"bower_components",
"node_modules"
]
],
"dependencies": {
"angular": ">=1.2.0"
},
"devDependencies": {
"angular-mocks": ">=1.2.0",
"jquery": "~2.1.0"
}
}
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"Daryl Rowland <[email protected]>"
],
"name": "angucomplete",
"description": "Awesome Autocompleteness for AngularJS",
"version": "1.0.0",
"homepage": "http://darylrowland.github.io/angucomplete/",
"engines": {
"node": ">= 0.8.4"
},
"dependencies": {},
"devDependencies": {
"async": "0.1.x",
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.4",
"grunt-contrib-watch": "~0.5.3",
"grunt-karma": "~0.6.2",
"karma": "~0.10.2",
"grunt-conventional-changelog": "~1.0.0",
"load-grunt-tasks": "~0.2.0"
}
}
Loading

0 comments on commit 599cf7e

Please sign in to comment.