/
index.html
155 lines (129 loc) · 5.05 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<html ng-app="perpendicular">
<head>
<title>Testie</title>
<link rel="stylesheet" href="chosen.css" />
</head>
<body ng-controller="ApplicationController">
<select ng-disabled="!tags.length" class="chzn-select" data-placeholder="Choose a Country..." style="width:350px;" multiple tabindex="3" ng-model="selectedTags" pp-chosen-bind="{ tentative: 'tentativeInput.text', results: 'tags', filtered: 'visibleTags' }">
<option ng-repeat="visibleTag in visibleTags" value="{{ visibleTag }}">{{ visibleTag }}</option>
</select>
<hr>
<p>
<strong>Selected: </strong>{{ selectedTags }}
</p>
<script src="jquery.js"></script>
<script src="jquery-migrate.js"></script>
<script src="chosen.js"></script>
<script src="angular.js"></script>
<script>
var app = angular.module('perpendicular', []);
app.directive('ppChosenBind', function($compile) {
return function($scope, $element, $attrs) {
var parameters = $scope.$eval($attrs.ppChosenBind);
var boundAngularModel = $attrs.ngModel;
var boundTentativeModel = parameters.tentative;
var boundResultsModel = parameters.results;
var boundFilteredModel = parameters.filtered;
$(document).ready(function (){
var currentTentativeTagExists = function () {
var modelText = $scope.$eval(boundTentativeModel);
var existingTags = ($scope.$eval(boundAngularModel) || []);
return (-1 != existingTags.indexOf(modelText));
}
function ChosenHax () {
}
ChosenHax.prototype.no_results = function (terms) {
this.search_results.append(
currentTentativeTagExists()
? $('<li class="no-results">You already have this tag.</li>')
: $('<li class="no-results">No tags match <strong>' + terms + '</strong>. Hit Return to create it.</li>')
);
}
ChosenHax.prototype.search_field_scale = function () {
}
ChosenHax.prototype.close_field = function () {
if (this.suppressClosingField) {
return;
}
this.search_field.val("");
Chosen.prototype.close_field.call(this);
}
ChosenHax.prototype.result_select = function (event) {
if (this.result_highlight) {
Chosen.prototype.result_select.call(this, event);
} else {
var me = this;
$scope.$apply(function(){
if (currentTentativeTagExists()) {
me.search_field.val("");
return;
}
var modelText = $scope.$eval(boundTentativeModel);
$scope.$eval(boundResultsModel).push(modelText);
$scope.$eval(boundFilteredModel).push(modelText);
window.setTimeout(function(){
me.suppressClosingField = true;
$element.trigger("liszt:updated");
me.search_field.val(modelText);
me.winnow_results();
Chosen.prototype.result_select.call(me, event);
me.suppressClosingField = false;
}, 1);
return;
});
}
}
$element.bind("liszt:ready", function (event, data) {
var chosen = data.chosen;
var $textField = chosen.search_field.attr("ng-model", boundTentativeModel);
var textFieldHTML = $("<div>").append($textField.eq(0).clone()).html();
var $ngTextField = $compile(textFieldHTML)($scope);
$textField.before($ngTextField).remove();
chosen.search_field = $ngTextField;
$element.data("chosen", $.extend(chosen, new ChosenHax()));
}).chosen();
$scope.$watch(boundTentativeModel, function (toVal, fromVal) {
if (!toVal) {
$scope.$eval(boundFilteredModel + " = []");
return;
}
var matches = $.map($scope.$eval(boundResultsModel), function (obj) {
var needle = angular.lowercase(toVal);
var haystack = angular.lowercase(obj);
return (haystack.slice(0, needle.length) == needle)
? haystack
: null;
});
var visibleFilteredItems = $.merge([], ($scope.$eval(boundAngularModel) || []));
$.each(matches, function (idx, obj) {
if (-1 == visibleFilteredItems.indexOf(obj))
visibleFilteredItems.push(obj);
});
$scope.$eval(boundFilteredModel + " = " + JSON.stringify(visibleFilteredItems));
});
$scope.$watch(boundFilteredModel, function (toVal, fromVal) {
window.setTimeout(function(){
var chosen = $element.data("chosen");
var currentTerm = chosen.search_field.val();
chosen.suppressClosingField = true;
$element.trigger("liszt:updated");
chosen.search_field.val(currentTerm);
chosen.winnow_results();
chosen.suppressClosingField = false;
});
});
});
};
});
function ApplicationController ($scope, $http, $compile) {
$scope.tags = [];
$scope.visibleTags = [];
$scope.tentativeInput = {};
$http.get('tags.json').then(function(response){
$scope.tags = response.data;
$scope.visibleTags = [];
});
};
</script>
</body>
</html>