Skip to content

Commit 8e71091

Browse files
committed
update search behavior
1 parent 29430b5 commit 8e71091

6 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/config/states.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ angular.module('codexen')
2929

3030
/* Snippets */
3131
.state('snippets', {
32-
url: '/snippets',
32+
url: '/snippets?search',
3333
views: {
3434
'main-view': {
3535
templateUrl: 'tpls/states/snippets.list.tpl.html',

src/controllers/modals/NewSnippetModalController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ angular.module('codexen')
1111
callSign: vm.callSign,
1212
mode: vm.mode == null ? null : vm.mode.toLowerCase(),
1313
content: vm.content,
14-
tags: angular.isArray(vm.Tags) ? vm.Tags.map(function (tag) { return tag.name }) : []
14+
Tags: angular.isArray(vm.Tags) ? vm.Tags.map(function (tag) { return tag.name }) : []
1515
}
1616

1717
Snippet.create(params)

src/controllers/states/SnippetsListController.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
/* global angular */
22
angular.module('codexen')
3-
.controller('SnippetsListController', function ($auth, Snippet, $scope, $state) {
3+
.controller('SnippetsListController', function ($auth, Snippet, $scope, $state, $scope) {
44
var vm = this
55

66
vm.isLoaded = false
77

8+
vm.search = $state.params.search
9+
10+
vm.snippetId = parseInt($state.params.id)
11+
$scope.$on('$stateChangeStart', function (e, toState, toParams) {
12+
vm.snippetId = parseInt(toParams.id)
13+
})
14+
815
var loadSnippets = function () {
916
if ($auth.isAuthenticated) {
1017
Snippet.findMine({

src/directives/tags.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ angular.module('codexen')
55
restrict: 'A',
66
template: '<p class="tags" ng-if="tags.length">' +
77
'<i class="fa fa-tags"></i> ' +
8-
'<a ng-repeat="tag in tags" href="#">#<span ng-bind="tag.name"></span></a>' +
8+
'<a ui-sref="snippets({search:\'tag:\'+tag.name})" ng-repeat="tag in tags" href="#">#<span ng-bind="tag.name"></span></a>' +
99
'</p>',
1010
scope: {
1111
tags: '='
12+
},
13+
link: function (scope, el) {
14+
el.ready(function () {
15+
el.find('a').on('click', function (e) {
16+
e.stopPropagation()
17+
})
18+
})
1219
}
1320
}
1421
})

src/filters/search-snippets.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
angular.module('codexen')
33
.filter('searchSnippets', function ($filter) {
44
return function (input, needle) {
5-
if (!angular.isString(needle)) return input
6-
if (needle.match(/^#./)) {
7-
var name = needle.match(/^#(.+)/)[1]
5+
if (!angular.isString(needle) || !angular.isArray(input)) return input
6+
if (needle.match(/#(.+)|tag:(.+)/)) {
7+
var name = needle.match(/#(.+)/) ? needle.match(/#(.+)/)[1] : needle.match(/tag:(.+)/)[1]
8+
89
return input.filter(function (snippet) {
910
return snippet.Tags.some(function (tag) {
1011
return tag.name.match(new RegExp(name))

src/tpls/states/snippets.list.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ <h4>
2626
<a ui-sref="auth.signin" class="btn btn-default"><i class="fa fa-signin"></i> Sign In</a>
2727
</li>
2828

29-
<li ng-repeat="snippet in vm.snippets | searchSnippets:vm.search" ui-sref="snippets.detail({id:snippet.id})" ui-sref-active="active">
29+
<li ng-repeat="snippet in vm.snippets | searchSnippets:vm.search" ui-sref="snippets.detail({id:snippet.id})" ng-class="{active:vm.snippetId===snippet.id}">
3030
<div class="media">
3131
<div class="media-left">
3232
<img width="25" height="25" class="img-circle" src="http://www.gravatar.com/avatar/ea0b6ad1c11700120d1af08810caa19d" alt="" />

0 commit comments

Comments
 (0)