Roberto Sánchez
2014-01-10 3d0c6e4865d4a0ddd764da533a327faf76e0cb32
#394 feature - Catalog module finished in beta - Security and Error
management are not completed
7 files added
10 files modified
changed files
securis/src/main/java/net/curisit/securis/db/Application.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/Organization.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/ApplicationResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/UserResource.java patch | view | blame | history
securis/src/main/resources/static/admin.html patch | view | blame | history
securis/src/main/resources/static/css/chosen-spinner.css patch | view | blame | history
securis/src/main/resources/static/css/chosen-sprite.png patch | view | blame | history
securis/src/main/resources/static/css/chosen-sprite@2x.png patch | view | blame | history
securis/src/main/resources/static/css/chosen.css patch | view | blame | history
securis/src/main/resources/static/css/spinner.gif patch | view | blame | history
securis/src/main/resources/static/js/admin.js patch | view | blame | history
securis/src/main/resources/static/js/catalogs.js patch | view | blame | history
securis/src/main/resources/static/js/catalogs.json patch | view | blame | history
securis/src/main/resources/static/js/chosen.js patch | view | blame | history
securis/src/main/resources/static/js/toaster.js patch | view | blame | history
securis/src/main/resources/static/js/vendor/chosen.jquery.js patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/Application.java
....@@ -2,6 +2,7 @@
22
33 import java.io.Serializable;
44 import java.util.Date;
5
+import java.util.Set;
56
67 import javax.persistence.Column;
78 import javax.persistence.Entity;
....@@ -9,9 +10,11 @@
910 import javax.persistence.Id;
1011 import javax.persistence.NamedQueries;
1112 import javax.persistence.NamedQuery;
13
+import javax.persistence.OneToMany;
1214 import javax.persistence.Table;
1315
1416 import org.codehaus.jackson.annotate.JsonAutoDetect;
17
+import org.codehaus.jackson.annotate.JsonIgnore;
1518 import org.codehaus.jackson.map.annotate.JsonSerialize;
1619
1720 /**
....@@ -37,6 +40,11 @@
3740
3841 @Column(name = "creation_timestamp")
3942 private Date creationTimestamp;
43
+
44
+ @JsonIgnore
45
+ // We don't include the referenced entities to limit the size of each row at the listing
46
+ @OneToMany(mappedBy = "application")
47
+ private Set<LicenseType> licenseTypes;
4048
4149 public int getId() {
4250 return id;
....@@ -70,4 +78,12 @@
7078 this.creationTimestamp = creationTimestamp;
7179 }
7280
81
+ public Set<LicenseType> getLicenseTypes() {
82
+ return licenseTypes;
83
+ }
84
+
85
+ public void setLicenseTypes(Set<LicenseType> licenseTypes) {
86
+ this.licenseTypes = licenseTypes;
87
+ }
88
+
7389 }
securis/src/main/java/net/curisit/securis/db/Organization.java
....@@ -4,7 +4,9 @@
44 import java.util.ArrayList;
55 import java.util.Date;
66 import java.util.List;
7
+import java.util.Set;
78
9
+import javax.persistence.CascadeType;
810 import javax.persistence.Column;
911 import javax.persistence.Entity;
1012 import javax.persistence.GeneratedValue;
....@@ -15,6 +17,7 @@
1517 import javax.persistence.ManyToOne;
1618 import javax.persistence.NamedQueries;
1719 import javax.persistence.NamedQuery;
20
+import javax.persistence.OneToMany;
1821 import javax.persistence.Table;
1922
2023 import org.codehaus.jackson.annotate.JsonAutoDetect;
....@@ -33,7 +36,7 @@
3336 @Entity
3437 @Table(name = "organization")
3538 @NamedQueries(
36
- { @NamedQuery(name = "list-organizations", query = "SELECT o FROM Organization o") })
39
+ { @NamedQuery(name = "list-organizations", query = "SELECT o FROM Organization o"), @NamedQuery(name = "find-children-org", query = "SELECT o FROM Organization o where o.parentOrganization = :parentOrganization") })
3740 public class Organization implements Serializable {
3841
3942 @SuppressWarnings("unused")
....@@ -54,7 +57,7 @@
5457
5558 @JsonIgnore
5659 // We don't include the users to limit the size of each row a the listing
57
- @ManyToMany
60
+ @ManyToMany(cascade = CascadeType.REMOVE)
5861 @JoinTable(name = "user_organization", //
5962 joinColumns =
6063 { @JoinColumn(name = "organization_id", referencedColumnName = "id") }, //
....@@ -67,6 +70,11 @@
6770 @ManyToOne
6871 @JoinColumn(name = "org_parent_id")
6972 private Organization parentOrganization;
73
+
74
+ @JsonIgnore
75
+ // We don't include the users to limit the size of each row a the listing
76
+ @OneToMany(mappedBy = "parentOrganization")
77
+ private Set<Organization> childOrganizations;
7078
7179 public int getId() {
7280 return id;
....@@ -163,4 +171,12 @@
163171 return ids;
164172 }
165173
174
+ public Set<Organization> getChildOrganizations() {
175
+ return childOrganizations;
176
+ }
177
+
178
+ public void setChildOrganizations(Set<Organization> childOrganizations) {
179
+ this.childOrganizations = childOrganizations;
180
+ }
181
+
166182 }
securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
....@@ -144,6 +144,10 @@
144144 return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Application not found with ID: " + appid).build();
145145 }
146146
147
+ if (app.getLicenseTypes() != null && app.getLicenseTypes().size() > 0) {
148
+ return Response.status(Status.FORBIDDEN).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Application can not be deleted becasue has assigned one or more License types, ID: " + appid).build();
149
+ }
150
+
147151 em.remove(app);
148152 return Response.ok(Utils.createMap("success", true, "id", appid)).build();
149153 }
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
....@@ -119,7 +119,7 @@
119119 users = new ArrayList<>();
120120 for (String username : usersIds) {
121121 User user = em.find(User.class, username);
122
- if (parentOrg == null) {
122
+ if (user == null) {
123123 log.error("Organization user with id {} not found in DB", username);
124124 return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization's user not found with ID: " + username).build();
125125 }
....@@ -191,13 +191,17 @@
191191 public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) {
192192 log.info("Deleting app with id: {}", orgid);
193193 EntityManager em = emProvider.get();
194
- Organization app = em.find(Organization.class, Integer.parseInt(orgid));
195
- if (app == null) {
194
+ Organization org = em.find(Organization.class, Integer.parseInt(orgid));
195
+ if (org == null) {
196196 log.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
197
- return Response.status(Status.NOT_FOUND).build();
197
+ return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization was not found, ID: " + orgid).build();
198
+ }
199
+ if (org.getChildOrganizations() != null && org.getChildOrganizations().size() > 0) {
200
+ log.error("Organization has children and can not be deleted, ID: " + orgid);
201
+ return Response.status(Status.FORBIDDEN).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization has children and can not be deleted, ID: " + orgid).build();
198202 }
199203
200
- em.remove(app);
204
+ em.remove(org);
201205 return Response.ok(Utils.createMap("success", true, "id", orgid)).build();
202206 }
203207
securis/src/main/java/net/curisit/securis/services/UserResource.java
....@@ -139,10 +139,10 @@
139139 public Response modify(User user, @PathParam("uid") String uid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
140140 log.info("Modifying user with id: {}", uid);
141141 EntityManager em = emProvider.get();
142
- User currentUser = em.find(User.class, Integer.parseInt(uid));
142
+ User currentUser = em.find(User.class, uid);
143143 if (currentUser == null) {
144
- log.error("User with id {} not found in DB", uid);
145
- return Response.status(Status.NOT_FOUND).header("SECURIS_ERROR", "User not found with ID: " + uid).build();
144
+ log.info("User with id {} not found in DB, we'll try to create it", uid);
145
+ return create(user, token);
146146 }
147147
148148 List<Organization> orgs = null;
securis/src/main/resources/static/admin.html
....@@ -12,6 +12,8 @@
1212 <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
1313 <link rel="stylesheet" href="/css/bootstrap-dialog.css">
1414 <link rel="stylesheet" href="/css/toaster.css">
15
+<link rel="stylesheet" href="/css/chosen.css">
16
+<link rel="stylesheet" href="/css/chosen-spinner.css">
1517
1618 <style>
1719 body {
....@@ -52,7 +54,25 @@
5254 margin-bottom: 5px;
5355 }
5456
57
+.chosen-choices {
58
+ min-width: 100% !important;
59
+ height: 34px !important;
60
+ padding: 3px 6px;
61
+ font-size: 14px;
62
+ vertical-align: middle;
63
+ border: 1px solid #ccc;
64
+ border-radius: 4px;
65
+}
5566
67
+.chosen-container {
68
+ min-width: 100% !important;
69
+ border: none;
70
+ padding: 0px;
71
+}
72
+.chosen-container-multi li.search-field input[type="text"] {
73
+ min-height: 25px !important;
74
+ height: 25px !important;
75
+}
5676
5777 </style>
5878 <link rel="stylesheet"
....@@ -123,7 +143,7 @@
123143 <div class="panel panel-default animate-show ng-hide" ng-show="showForm">
124144 <form role="form" class="form-horizontal " name="catalogForm" id="catalogForm" ng-submit="saveCatalog()" >
125145 <!-- <pre>formu: {{formu | json}}</pre>-->
126
- <div class="form-group" ng-repeat="field in catalogMetadata.fields" ng-if="(!isNew || !field.readOnly) && !field.listingOnly">
146
+ <div class="form-group" ng-repeat="field in catalogMetadata.fields" ng-if="(!isNew || !field.autogenerate) && !field.listingOnly">
127147 <label class="col-md-3 control-label" for="{{field.name}}">{{field.display}}</label>
128148 <div class="col-md-5">
129149 <div ng-switch on="inputType(field)">
....@@ -136,6 +156,10 @@
136156 <select ng-switch-when="select" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
137157 ng-options="o.id as o.label for o in refs[field.name]">
138158 </select>
159
+ <select chosen multiple ng-switch-when="multiselect" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
160
+ ng-options="o.id as o.label for o in refs[field.name]" data-placeholder="...">
161
+ </select>
162
+
139163
140164 </div>
141165 <div class="alert inline-alert alert-warning" ng-show="catalogForm[field.name].$invalid">
....@@ -207,11 +231,15 @@
207231 <script type="text/javascript"
208232 src="/js/angular-resource.min.js"></script>
209233 <script type="text/javascript"
210
- src="/js/angular-animate.min.js"></script>
211
- <script type="text/javascript"
212234 src="/js/bootstrap-dialog.js"></script>
213235 <script type="text/javascript"
214236 src="/js/toaster.js"></script>
237
+ <script type="text/javascript"
238
+ src="/js/toaster.js"></script>
239
+ <script type="text/javascript"
240
+ src="/js/vendor/chosen.jquery.js"></script>
241
+ <script type="text/javascript"
242
+ src="/js/chosen.js"></script>
215243
216244
217245 <!--
securis/src/main/resources/static/css/chosen-spinner.css
....@@ -0,0 +1,12 @@
1
+/* Additional styles to display a spinner image while options are loading */
2
+.localytics-chosen.loading+.chosen-container-multi .chosen-choices {
3
+ background-image: url('spinner.gif');
4
+ background-repeat: no-repeat;
5
+ background-position: 95%;
6
+}
7
+.localytics-chosen.loading+.chosen-container-single .chosen-single span {
8
+ background: url('spinner.gif') no-repeat right;
9
+}
10
+.localytics-chosen.loading+.chosen-container-single .chosen-single .search-choice-close {
11
+ display: none;
12
+}
securis/src/main/resources/static/css/chosen-sprite.png
Binary files differ
securis/src/main/resources/static/css/chosen-sprite@2x.png
Binary files differ
securis/src/main/resources/static/css/chosen.css
....@@ -0,0 +1,430 @@
1
+/* @group Base */
2
+.chosen-container {
3
+ position: relative;
4
+ display: inline-block;
5
+ vertical-align: middle;
6
+ font-size: 13px;
7
+ zoom: 1;
8
+ *display: inline;
9
+ -webkit-user-select: none;
10
+ -moz-user-select: none;
11
+ user-select: none;
12
+}
13
+.chosen-container .chosen-drop {
14
+ position: absolute;
15
+ top: 100%;
16
+ left: -9999px;
17
+ z-index: 1010;
18
+ -webkit-box-sizing: border-box;
19
+ -moz-box-sizing: border-box;
20
+ box-sizing: border-box;
21
+ width: 100%;
22
+ border: 1px solid #aaa;
23
+ border-top: 0;
24
+ background: #fff;
25
+ box-shadow: 0 4px 5px rgba(0, 0, 0, 0.15);
26
+}
27
+.chosen-container.chosen-with-drop .chosen-drop {
28
+ left: 0;
29
+}
30
+.chosen-container a {
31
+ cursor: pointer;
32
+}
33
+
34
+/* @end */
35
+/* @group Single Chosen */
36
+.chosen-container-single .chosen-single {
37
+ position: relative;
38
+ display: block;
39
+ overflow: hidden;
40
+ padding: 0 0 0 8px;
41
+ height: 23px;
42
+ border: 1px solid #aaa;
43
+ border-radius: 5px;
44
+ background-color: #fff;
45
+ background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(20%, #ffffff), color-stop(50%, #f6f6f6), color-stop(52%, #eeeeee), color-stop(100%, #f4f4f4));
46
+ background: -webkit-linear-gradient(top, #ffffff 20%, #f6f6f6 50%, #eeeeee 52%, #f4f4f4 100%);
47
+ background: -moz-linear-gradient(top, #ffffff 20%, #f6f6f6 50%, #eeeeee 52%, #f4f4f4 100%);
48
+ background: -o-linear-gradient(top, #ffffff 20%, #f6f6f6 50%, #eeeeee 52%, #f4f4f4 100%);
49
+ background: linear-gradient(top, #ffffff 20%, #f6f6f6 50%, #eeeeee 52%, #f4f4f4 100%);
50
+ background-clip: padding-box;
51
+ box-shadow: 0 0 3px white inset, 0 1px 1px rgba(0, 0, 0, 0.1);
52
+ color: #444;
53
+ text-decoration: none;
54
+ white-space: nowrap;
55
+ line-height: 24px;
56
+}
57
+.chosen-container-single .chosen-default {
58
+ color: #999;
59
+}
60
+.chosen-container-single .chosen-single span {
61
+ display: block;
62
+ overflow: hidden;
63
+ margin-right: 26px;
64
+ text-overflow: ellipsis;
65
+ white-space: nowrap;
66
+}
67
+.chosen-container-single .chosen-single-with-deselect span {
68
+ margin-right: 38px;
69
+}
70
+.chosen-container-single .chosen-single abbr {
71
+ position: absolute;
72
+ top: 6px;
73
+ right: 26px;
74
+ display: block;
75
+ width: 12px;
76
+ height: 12px;
77
+ background: url('chosen-sprite.png') -42px 1px no-repeat;
78
+ font-size: 1px;
79
+}
80
+.chosen-container-single .chosen-single abbr:hover {
81
+ background-position: -42px -10px;
82
+}
83
+.chosen-container-single.chosen-disabled .chosen-single abbr:hover {
84
+ background-position: -42px -10px;
85
+}
86
+.chosen-container-single .chosen-single div {
87
+ position: absolute;
88
+ top: 0;
89
+ right: 0;
90
+ display: block;
91
+ width: 18px;
92
+ height: 100%;
93
+}
94
+.chosen-container-single .chosen-single div b {
95
+ display: block;
96
+ width: 100%;
97
+ height: 100%;
98
+ background: url('chosen-sprite.png') no-repeat 0px 2px;
99
+}
100
+.chosen-container-single .chosen-search {
101
+ position: relative;
102
+ z-index: 1010;
103
+ margin: 0;
104
+ padding: 3px 4px;
105
+ white-space: nowrap;
106
+}
107
+.chosen-container-single .chosen-search input[type="text"] {
108
+ -webkit-box-sizing: border-box;
109
+ -moz-box-sizing: border-box;
110
+ box-sizing: border-box;
111
+ margin: 1px 0;
112
+ padding: 4px 20px 4px 5px;
113
+ width: 100%;
114
+ height: auto;
115
+ outline: 0;
116
+ border: 1px solid #aaa;
117
+ background: white url('chosen-sprite.png') no-repeat 100% -20px;
118
+ background: url('chosen-sprite.png') no-repeat 100% -20px, -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(1%, #eeeeee), color-stop(15%, #ffffff));
119
+ background: url('chosen-sprite.png') no-repeat 100% -20px, -webkit-linear-gradient(#eeeeee 1%, #ffffff 15%);
120
+ background: url('chosen-sprite.png') no-repeat 100% -20px, -moz-linear-gradient(#eeeeee 1%, #ffffff 15%);
121
+ background: url('chosen-sprite.png') no-repeat 100% -20px, -o-linear-gradient(#eeeeee 1%, #ffffff 15%);
122
+ background: url('chosen-sprite.png') no-repeat 100% -20px, linear-gradient(#eeeeee 1%, #ffffff 15%);
123
+ font-size: 1em;
124
+ font-family: sans-serif;
125
+ line-height: normal;
126
+ border-radius: 0;
127
+}
128
+.chosen-container-single .chosen-drop {
129
+ margin-top: -1px;
130
+ border-radius: 0 0 4px 4px;
131
+ background-clip: padding-box;
132
+}
133
+.chosen-container-single.chosen-container-single-nosearch .chosen-search {
134
+ position: absolute;
135
+ left: -9999px;
136
+}
137
+
138
+/* @end */
139
+/* @group Results */
140
+.chosen-container .chosen-results {
141
+ position: relative;
142
+ overflow-x: hidden;
143
+ overflow-y: auto;
144
+ margin: 0 4px 4px 0;
145
+ padding: 0 0 0 4px;
146
+ max-height: 240px;
147
+ -webkit-overflow-scrolling: touch;
148
+}
149
+.chosen-container .chosen-results li {
150
+ display: none;
151
+ margin: 0;
152
+ padding: 5px 6px;
153
+ list-style: none;
154
+ line-height: 15px;
155
+}
156
+.chosen-container .chosen-results li.active-result {
157
+ display: list-item;
158
+ cursor: pointer;
159
+}
160
+.chosen-container .chosen-results li.disabled-result {
161
+ display: list-item;
162
+ color: #ccc;
163
+ cursor: default;
164
+}
165
+.chosen-container .chosen-results li.highlighted {
166
+ background-color: #3875d7;
167
+ background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(20%, #3875d7), color-stop(90%, #2a62bc));
168
+ background-image: -webkit-linear-gradient(#3875d7 20%, #2a62bc 90%);
169
+ background-image: -moz-linear-gradient(#3875d7 20%, #2a62bc 90%);
170
+ background-image: -o-linear-gradient(#3875d7 20%, #2a62bc 90%);
171
+ background-image: linear-gradient(#3875d7 20%, #2a62bc 90%);
172
+ color: #fff;
173
+}
174
+.chosen-container .chosen-results li.no-results {
175
+ display: list-item;
176
+ background: #f4f4f4;
177
+}
178
+.chosen-container .chosen-results li.group-result {
179
+ display: list-item;
180
+ font-weight: bold;
181
+ cursor: default;
182
+}
183
+.chosen-container .chosen-results li.group-option {
184
+ padding-left: 15px;
185
+}
186
+.chosen-container .chosen-results li em {
187
+ font-style: normal;
188
+ text-decoration: underline;
189
+}
190
+
191
+/* @end */
192
+/* @group Multi Chosen */
193
+.chosen-container-multi .chosen-choices {
194
+ position: relative;
195
+ overflow: hidden;
196
+ -webkit-box-sizing: border-box;
197
+ -moz-box-sizing: border-box;
198
+ box-sizing: border-box;
199
+ margin: 0;
200
+ padding: 0;
201
+ width: 100%;
202
+ height: auto !important;
203
+ height: 1%;
204
+ border: 1px solid #aaa;
205
+ background-color: #fff;
206
+ background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(1%, #eeeeee), color-stop(15%, #ffffff));
207
+ background-image: -webkit-linear-gradient(#eeeeee 1%, #ffffff 15%);
208
+ background-image: -moz-linear-gradient(#eeeeee 1%, #ffffff 15%);
209
+ background-image: -o-linear-gradient(#eeeeee 1%, #ffffff 15%);
210
+ background-image: linear-gradient(#eeeeee 1%, #ffffff 15%);
211
+ cursor: text;
212
+}
213
+.chosen-container-multi .chosen-choices li {
214
+ float: left;
215
+ list-style: none;
216
+}
217
+.chosen-container-multi .chosen-choices li.search-field {
218
+ margin: 0;
219
+ padding: 0;
220
+ white-space: nowrap;
221
+}
222
+.chosen-container-multi .chosen-choices li.search-field input[type="text"] {
223
+ margin: 1px 0;
224
+ padding: 5px;
225
+ height: 15px;
226
+ outline: 0;
227
+ border: 0 !important;
228
+ background: transparent !important;
229
+ box-shadow: none;
230
+ color: #666;
231
+ font-size: 100%;
232
+ font-family: sans-serif;
233
+ line-height: normal;
234
+ border-radius: 0;
235
+}
236
+.chosen-container-multi .chosen-choices li.search-field .default {
237
+ color: #999;
238
+}
239
+.chosen-container-multi .chosen-choices li.search-choice {
240
+ position: relative;
241
+ margin: 3px 0 3px 5px;
242
+ padding: 3px 20px 3px 5px;
243
+ border: 1px solid #aaa;
244
+ border-radius: 3px;
245
+ background-color: #e4e4e4;
246
+ background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(20%, #f4f4f4), color-stop(50%, #f0f0f0), color-stop(52%, #e8e8e8), color-stop(100%, #eeeeee));
247
+ background-image: -webkit-linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
248
+ background-image: -moz-linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
249
+ background-image: -o-linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
250
+ background-image: linear-gradient(#f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
251
+ background-clip: padding-box;
252
+ box-shadow: 0 0 2px white inset, 0 1px 0 rgba(0, 0, 0, 0.05);
253
+ color: #333;
254
+ line-height: 13px;
255
+ cursor: default;
256
+}
257
+.chosen-container-multi .chosen-choices li.search-choice .search-choice-close {
258
+ position: absolute;
259
+ top: 4px;
260
+ right: 3px;
261
+ display: block;
262
+ width: 12px;
263
+ height: 12px;
264
+ background: url('chosen-sprite.png') -42px 1px no-repeat;
265
+ font-size: 1px;
266
+}
267
+.chosen-container-multi .chosen-choices li.search-choice .search-choice-close:hover {
268
+ background-position: -42px -10px;
269
+}
270
+.chosen-container-multi .chosen-choices li.search-choice-disabled {
271
+ padding-right: 5px;
272
+ border: 1px solid #ccc;
273
+ background-color: #e4e4e4;
274
+ background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(20%, #f4f4f4), color-stop(50%, #f0f0f0), color-stop(52%, #e8e8e8), color-stop(100%, #eeeeee));
275
+ background-image: -webkit-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
276
+ background-image: -moz-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
277
+ background-image: -o-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
278
+ background-image: linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
279
+ color: #666;
280
+}
281
+.chosen-container-multi .chosen-choices li.search-choice-focus {
282
+ background: #d4d4d4;
283
+}
284
+.chosen-container-multi .chosen-choices li.search-choice-focus .search-choice-close {
285
+ background-position: -42px -10px;
286
+}
287
+.chosen-container-multi .chosen-results {
288
+ margin: 0;
289
+ padding: 0;
290
+}
291
+.chosen-container-multi .chosen-drop .result-selected {
292
+ display: list-item;
293
+ color: #ccc;
294
+ cursor: default;
295
+}
296
+
297
+/* @end */
298
+/* @group Active */
299
+.chosen-container-active .chosen-single {
300
+ border: 1px solid #5897fb;
301
+ box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
302
+}
303
+.chosen-container-active.chosen-with-drop .chosen-single {
304
+ border: 1px solid #aaa;
305
+ -moz-border-radius-bottomright: 0;
306
+ border-bottom-right-radius: 0;
307
+ -moz-border-radius-bottomleft: 0;
308
+ border-bottom-left-radius: 0;
309
+ background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(20%, #eeeeee), color-stop(80%, #ffffff));
310
+ background-image: -webkit-linear-gradient(#eeeeee 20%, #ffffff 80%);
311
+ background-image: -moz-linear-gradient(#eeeeee 20%, #ffffff 80%);
312
+ background-image: -o-linear-gradient(#eeeeee 20%, #ffffff 80%);
313
+ background-image: linear-gradient(#eeeeee 20%, #ffffff 80%);
314
+ box-shadow: 0 1px 0 #fff inset;
315
+}
316
+.chosen-container-active.chosen-with-drop .chosen-single div {
317
+ border-left: none;
318
+ background: transparent;
319
+}
320
+.chosen-container-active.chosen-with-drop .chosen-single div b {
321
+ background-position: -18px 2px;
322
+}
323
+.chosen-container-active .chosen-choices {
324
+ border: 1px solid #5897fb;
325
+ box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
326
+}
327
+.chosen-container-active .chosen-choices li.search-field input[type="text"] {
328
+ color: #111 !important;
329
+}
330
+
331
+/* @end */
332
+/* @group Disabled Support */
333
+.chosen-disabled {
334
+ opacity: 0.5 !important;
335
+ cursor: default;
336
+}
337
+.chosen-disabled .chosen-single {
338
+ cursor: default;
339
+}
340
+.chosen-disabled .chosen-choices .search-choice .search-choice-close {
341
+ cursor: default;
342
+}
343
+
344
+/* @end */
345
+/* @group Right to Left */
346
+.chosen-rtl {
347
+ text-align: right;
348
+}
349
+.chosen-rtl .chosen-single {
350
+ overflow: visible;
351
+ padding: 0 8px 0 0;
352
+}
353
+.chosen-rtl .chosen-single span {
354
+ margin-right: 0;
355
+ margin-left: 26px;
356
+ direction: rtl;
357
+}
358
+.chosen-rtl .chosen-single-with-deselect span {
359
+ margin-left: 38px;
360
+}
361
+.chosen-rtl .chosen-single div {
362
+ right: auto;
363
+ left: 3px;
364
+}
365
+.chosen-rtl .chosen-single abbr {
366
+ right: auto;
367
+ left: 26px;
368
+}
369
+.chosen-rtl .chosen-choices li {
370
+ float: right;
371
+}
372
+.chosen-rtl .chosen-choices li.search-field input[type="text"] {
373
+ direction: rtl;
374
+}
375
+.chosen-rtl .chosen-choices li.search-choice {
376
+ margin: 3px 5px 3px 0;
377
+ padding: 3px 5px 3px 19px;
378
+}
379
+.chosen-rtl .chosen-choices li.search-choice .search-choice-close {
380
+ right: auto;
381
+ left: 4px;
382
+}
383
+.chosen-rtl.chosen-container-single-nosearch .chosen-search,
384
+.chosen-rtl .chosen-drop {
385
+ left: 9999px;
386
+}
387
+.chosen-rtl.chosen-container-single .chosen-results {
388
+ margin: 0 0 4px 4px;
389
+ padding: 0 4px 0 0;
390
+}
391
+.chosen-rtl .chosen-results li.group-option {
392
+ padding-right: 15px;
393
+ padding-left: 0;
394
+}
395
+.chosen-rtl.chosen-container-active.chosen-with-drop .chosen-single div {
396
+ border-right: none;
397
+}
398
+.chosen-rtl .chosen-search input[type="text"] {
399
+ padding: 4px 5px 4px 20px;
400
+ background: white url('chosen-sprite.png') no-repeat -30px -20px;
401
+ background: url('chosen-sprite.png') no-repeat -30px -20px, -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(1%, #eeeeee), color-stop(15%, #ffffff));
402
+ background: url('chosen-sprite.png') no-repeat -30px -20px, -webkit-linear-gradient(#eeeeee 1%, #ffffff 15%);
403
+ background: url('chosen-sprite.png') no-repeat -30px -20px, -moz-linear-gradient(#eeeeee 1%, #ffffff 15%);
404
+ background: url('chosen-sprite.png') no-repeat -30px -20px, -o-linear-gradient(#eeeeee 1%, #ffffff 15%);
405
+ background: url('chosen-sprite.png') no-repeat -30px -20px, linear-gradient(#eeeeee 1%, #ffffff 15%);
406
+ direction: rtl;
407
+}
408
+.chosen-rtl.chosen-container-single .chosen-single div b {
409
+ background-position: 6px 2px;
410
+}
411
+.chosen-rtl.chosen-container-single.chosen-with-drop .chosen-single div b {
412
+ background-position: -12px 2px;
413
+}
414
+
415
+/* @end */
416
+/* @group Retina compatibility */
417
+@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-resolution: 144dpi) {
418
+ .chosen-rtl .chosen-search input[type="text"],
419
+ .chosen-container-single .chosen-single abbr,
420
+ .chosen-container-single .chosen-single div b,
421
+ .chosen-container-single .chosen-search input[type="text"],
422
+ .chosen-container-multi .chosen-choices .search-choice .search-choice-close,
423
+ .chosen-container .chosen-results-scroll-down span,
424
+ .chosen-container .chosen-results-scroll-up span {
425
+ background-image: url('chosen-sprite@2x.png') !important;
426
+ background-size: 52px 37px !important;
427
+ background-repeat: no-repeat !important;
428
+ }
429
+}
430
+/* @end */
securis/src/main/resources/static/css/spinner.gif
Binary files differ
securis/src/main/resources/static/js/admin.js
....@@ -1,7 +1,7 @@
11 (function() {
22 'use strict';
33
4
- var app = angular.module('app', [ 'ngRoute', 'ngAnimate', 'ngResource', 'toaster', 'catalogs' ]);
4
+ var app = angular.module('app', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs' ]);
55
66 app.directive(
77 'catalogField',
....@@ -35,7 +35,10 @@
3535 $scope.catalogsList = null;
3636 $scope.list = null;
3737
38
+
3839 var _changeCatalog = function(index) {
40
+ $scope.showForm = false;
41
+ $scope.formu = {};
3942 if (!$scope.catalogsList) $scope.catalogsList = Catalogs.getList(); // catalog list is also in index.data
4043 if (typeof index === 'number') $scope.catalogIndex = index;
4144 Catalogs.setCurrent($scope.catalogIndex);
....@@ -43,7 +46,6 @@
4346 $scope.list = Catalogs.query();
4447 $scope.refs = {}
4548 Catalogs.loadRefs($scope.refs)
46
- console.log($scope.refs)
4749 }
4850
4951 Catalogs.init().then(_changeCatalog);
....@@ -55,14 +57,14 @@
5557 $scope.isNew = false;
5658 $scope.formu = {}
5759 for (var k in data) {
58
- if (k.indexOf('$') !== 0) $scope.formu[k] = data[k]
60
+ if (k.indexOf('$') !== 0 && !Catalogs.getField(k).listingOnly) $scope.formu[k] = data[k]
5961 }
60
- console.log('$scope.edit')
61
- console.log($scope.formu)
62
- $('#'+ Catalogs.getFFF()).focus();
6362
64
-
63
+ setTimeout(function() {
64
+ $('#'+Catalogs.getFFF()).focus();
65
+ }, 0);
6566 }
67
+
6668 $scope.delete = function(data) {
6769 BootstrapDialog.confirm('The record will be deleted, are you sure?', function(result){
6870 if(result) {
....@@ -89,10 +91,12 @@
8991
9092 if (field.readOnly && field.type === 'date')
9193 return 'readonly_date';
92
- if (field.readOnly)
94
+ if (field.readOnly && (!field.pk || !$scope.isNew ))
9395 return 'readonly';
9496 if (field.type === 'select')
9597 return 'select';
98
+ if (field.type === 'multiselect')
99
+ return 'multiselect';
96100 if (!field.multiline)
97101 return 'normal';
98102 if (field.multiline)
....@@ -101,10 +105,13 @@
101105 }
102106
103107 $scope.editNew = function() {
104
- $('#'+ Catalogs.getFFF()).focus();
105
- $scope.$parent.showForm = true;
106108 $scope.$parent.isNew = true;
109
+ $scope.$parent.showForm = true;
107110 $scope.$parent.formu = {};
111
+ setTimeout(function() {
112
+ $('#'+Catalogs.getFFF()).focus();
113
+ }, 0);
114
+
108115 }
109116 $scope.cancel = function() {
110117 $scope.$parent.showForm = false;
....@@ -139,7 +146,7 @@
139146 var type = Catalogs.getField(name).type;
140147 var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
141148 if (printedValue !== value) // this line is a work around to allow search in formatted fields
142
- row['_display_'+name] = printedValue;
149
+ row['$display_'+name] = printedValue;
143150 return printedValue;
144151 }
145152
securis/src/main/resources/static/js/catalogs.js
....@@ -11,24 +11,15 @@
1111 var resources = {
1212 application : $resource('/application/:appId', {
1313 appId : '@id'
14
- }, {
15
- update : {
16
- method : "PUT"
17
- }
1814 }),
1915 user : $resource('/user/:userId', {
20
- userId : '@id'
21
- }, {
22
- update : {
23
- method : "PUT"
24
- }
16
+ userId : '@username'
17
+ }),
18
+ organization : $resource('/organization/:orgId', {
19
+ orgId : '@id'
2520 }),
2621 licensetype : $resource('/licensetype/:licenseTypeId', {
2722 licenseTypeId : '@id'
28
- }, {
29
- update : {
30
- method : "PUT"
31
- }
3223 })
3324 }
3425
....@@ -96,8 +87,8 @@
9687 this.getFFF = this.getFirstFocusableField = function() {
9788 if (!_current) throw new Error('There is no current catalog selected');
9889
99
- for(var i = i; i < _current.fields.length; i++)
100
- if (f.readOnly) return f.name;
90
+ for(var i = 0; i < _current.fields.length; i++)
91
+ if (!_current.fields[i].readOnly) return _current.fields[i].name;
10192
10293 return null;
10394 }
....@@ -135,10 +126,7 @@
135126 if (!_current) throw new Error('There is no current catalog selected');
136127
137128 var resource = this.getResource();
138
- if (data.id && data.id !== '')
139
- return resource.update(data, _success, _fail);
140
- else
141
- return resource.save(data, _success, _fail);
129
+ return resource.save(data, _success, _fail);
142130 }
143131 this.remove = function(data) {
144132 return this.getResource().remove({}, data, _success, _fail)
....@@ -165,18 +153,19 @@
165153 console.log('promises: ' + promises.length + ' ')
166154 console.log(promises)
167155 $q.all(promises).then(function() {
168
- console.log('ALL promises OK :::::::::::::::::::::::::::: ')
169156 for (var k in refs) {
170
- var pk = that.getPk(that.getMetadata(k))
171
- console.log('PK for '+k+' is ' + pk);
157
+ var pk = that.getPk(that.getMetadata(that.getField(k).resource))
158
+ console.log('PK field for ' + k + ' is ' + pk)
172159 var comboData = []
173160 refs[k].forEach(function(row) {
174161 comboData.push({
175162 id: row[pk],
176
- label: row.label || row.name || row.code
163
+ label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name
177164 });
178165 })
179166 refs[k] = comboData;
167
+ console.log('Ready for combo for ' + k)
168
+ console.log(comboData);
180169 }
181170
182171 })
securis/src/main/resources/static/js/catalogs.json
....@@ -7,6 +7,7 @@
77 "display" : "ID",
88 "type" : "number",
99 "pk" : true,
10
+ "autogenerate" : true,
1011 "readOnly" : true
1112 }, {
1213 "name" : "name",
....@@ -23,6 +24,7 @@
2324 }, {
2425 "name" : "creationTimestamp",
2526 "display" : "Creation date",
27
+ "autogenerate" : true,
2628 "type" : "date",
2729 "readOnly" : true
2830 } ]
....@@ -35,6 +37,7 @@
3537 "display" : "ID",
3638 "type" : "number",
3739 "pk" : true,
40
+ "autogenerate" : true,
3841 "readOnly" : true
3942 }, {
4043 "name" : "code",
....@@ -62,6 +65,7 @@
6265 }, {
6366 "name" : "creationTimestamp",
6467 "display" : "Creation date",
68
+ "autogenerate" : true,
6569 "type" : "date",
6670 "readOnly" : true
6771 }, {
....@@ -71,13 +75,14 @@
7175 } ]
7276 }, {
7377 "name" : "Organizations",
74
- "list_fields" : [ "code", "name", "application_name", "creationTimestamp" ],
78
+ "list_fields" : [ "code", "name", "org_parent_name", "creationTimestamp" ],
7579 "resource" : "organization",
7680 "fields" : [ {
7781 "name" : "id",
7882 "display" : "ID",
7983 "type" : "number",
8084 "pk" : true,
85
+ "autogenerate" : true,
8186 "readOnly" : true
8287 }, {
8388 "name" : "code",
....@@ -98,24 +103,67 @@
98103 "maxlength" : 500,
99104 "multiline" : 2
100105 }, {
101
- "name" : "application_id",
102
- "display" : "Application",
103
- "resource" : "application",
106
+ "name" : "org_parent_id",
107
+ "display" : "Parent organization",
108
+ "resource" : "organization",
104109 "type" : "select"
110
+ }, {
111
+ "name" : "users_ids",
112
+ "display" : "Users",
113
+ "resource" : "user",
114
+ "type" : "multiselect"
105115 }, {
106116 "name" : "creationTimestamp",
107117 "display" : "Creation date",
118
+ "autogenerate" : true,
108119 "type" : "date",
109120 "readOnly" : true
110121 }, {
111
- "name" : "application_name",
112
- "display" : "Application",
122
+ "name" : "org_parent_name",
123
+ "display" : "Parent org",
113124 "listingOnly" : true
114125 } ]
115126 }, {
116127 "name" : "Users",
128
+ "list_fields" : [ "username", "first_name", "last_name", "lastLogin" ],
117129 "resource" : "user",
118
- "fields" : []
130
+ "fields" : [ {
131
+ "name" : "username",
132
+ "display" : "Username",
133
+ "type" : "string",
134
+ "maxlength" : 45,
135
+ "pk" : true,
136
+ "readOnly" : true,
137
+ "mandatory" : true
138
+ }, {
139
+ "name" : "first_name",
140
+ "display" : "First name",
141
+ "type" : "string",
142
+ "maxlength" : 100,
143
+ "mandatory" : true
144
+ }, {
145
+ "name" : "last_name",
146
+ "display" : "Last name",
147
+ "type" : "string",
148
+ "maxlength" : 100
149
+ }, {
150
+ "name" : "organizations_ids",
151
+ "display" : "Organizations",
152
+ "resource" : "organization",
153
+ "type" : "multiselect"
154
+ }, {
155
+ "name" : "lastLogin",
156
+ "display" : "Last login",
157
+ "autogenerate" : true,
158
+ "type" : "date",
159
+ "readOnly" : true
160
+ }, {
161
+ "name" : "creationTimestamp",
162
+ "display" : "Creation date",
163
+ "autogenerate" : true,
164
+ "type" : "date",
165
+ "readOnly" : true
166
+ }]
119167 }, {
120168 "name" : "System params",
121169 "resource" : "systemparams",
securis/src/main/resources/static/js/chosen.js
....@@ -0,0 +1,106 @@
1
+// Generated by CoffeeScript 1.6.3
2
+(function() {
3
+ var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
4
+
5
+ angular.module('localytics.directives', []);
6
+
7
+ angular.module('localytics.directives').directive('chosen', function() {
8
+ var CHOSEN_OPTION_WHITELIST, NG_OPTIONS_REGEXP, chosen, isEmpty, snakeCase;
9
+ NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+(.*?)(?:\s+track\s+by\s+(.*?))?$/;
10
+ CHOSEN_OPTION_WHITELIST = ['noResultsText', 'allowSingleDeselect', 'disableSearchThreshold', 'disableSearch', 'enableSplitWordSearch', 'inheritSelectClasses', 'maxSelectedOptions', 'placeholderTextMultiple', 'placeholderTextSingle', 'searchContains', 'singleBackstrokeDelete', 'displayDisabledOptions', 'displaySelectedOptions', 'width'];
11
+ snakeCase = function(input) {
12
+ return input.replace(/[A-Z]/g, function($1) {
13
+ return "_" + ($1.toLowerCase());
14
+ });
15
+ };
16
+ isEmpty = function(value) {
17
+ var key;
18
+ if (angular.isArray(value)) {
19
+ return value.length === 0;
20
+ } else if (angular.isObject(value)) {
21
+ for (key in value) {
22
+ if (value.hasOwnProperty(key)) {
23
+ return false;
24
+ }
25
+ }
26
+ }
27
+ return true;
28
+ };
29
+ return chosen = {
30
+ restrict: 'A',
31
+ require: '?ngModel',
32
+ terminal: true,
33
+ link: function(scope, element, attr, ctrl) {
34
+ var disableWithMessage, empty, initOrUpdate, initialized, match, options, origRender, removeEmptyMessage, startLoading, stopLoading, valuesExpr, viewWatch;
35
+ element.addClass('localytics-chosen');
36
+ options = scope.$eval(attr.chosen) || {};
37
+ angular.forEach(attr, function(value, key) {
38
+ if (__indexOf.call(CHOSEN_OPTION_WHITELIST, key) >= 0) {
39
+ return options[snakeCase(key)] = scope.$eval(value);
40
+ }
41
+ });
42
+ startLoading = function() {
43
+ return element.addClass('loading').attr('disabled', true).trigger('chosen:updated');
44
+ };
45
+ stopLoading = function() {
46
+ return element.removeClass('loading').attr('disabled', false).trigger('chosen:updated');
47
+ };
48
+ initialized = false;
49
+ empty = false;
50
+ initOrUpdate = function() {
51
+ if (initialized) {
52
+ return element.trigger('chosen:updated');
53
+ } else {
54
+ element.chosen(options);
55
+ return initialized = true;
56
+ }
57
+ };
58
+ removeEmptyMessage = function() {
59
+ empty = false;
60
+ return element.find('option.empty').remove();
61
+ };
62
+ disableWithMessage = function(message) {
63
+ empty = true;
64
+ return element.empty().append("<option selected class=\"empty\">" + message + "</option>").attr('disabled', true).trigger('chosen:updated');
65
+ };
66
+ if (ctrl) {
67
+ origRender = ctrl.$render;
68
+ ctrl.$render = function() {
69
+ origRender();
70
+ return initOrUpdate();
71
+ };
72
+ if (attr.multiple) {
73
+ viewWatch = function() {
74
+ return ctrl.$viewValue;
75
+ };
76
+ scope.$watch(viewWatch, ctrl.$render, true);
77
+ }
78
+ } else {
79
+ initOrUpdate();
80
+ }
81
+ attr.$observe('disabled', function(value) {
82
+ return element.trigger('chosen:updated');
83
+ });
84
+ if (attr.ngOptions) {
85
+ match = attr.ngOptions.match(NG_OPTIONS_REGEXP);
86
+ valuesExpr = match[7];
87
+ if (angular.isUndefined(scope.$eval(valuesExpr))) {
88
+ startLoading();
89
+ }
90
+ return scope.$watchCollection(valuesExpr, function(newVal, oldVal) {
91
+ if (newVal !== oldVal) {
92
+ if (empty) {
93
+ removeEmptyMessage();
94
+ }
95
+ stopLoading();
96
+ if (isEmpty(newVal)) {
97
+ return disableWithMessage(options.no_results_text || 'No values available');
98
+ }
99
+ }
100
+ });
101
+ }
102
+ }
103
+ };
104
+ });
105
+
106
+}).call(this);
securis/src/main/resources/static/js/toaster.js
....@@ -13,7 +13,7 @@
1313 * Related to project of John Papa and Hans Fjällemark
1414 */
1515
16
-angular.module('toaster', ['ngAnimate'])
16
+angular.module('toaster',[] )
1717 .service('toaster', ['$rootScope', function ($rootScope) {
1818 this.pop = function (type, title, body, timeout, bodyOutputType) {
1919 this.toast = {
securis/src/main/resources/static/js/vendor/chosen.jquery.js
....@@ -0,0 +1,1166 @@
1
+// Chosen, a Select Box Enhancer for jQuery and Prototype
2
+// by Patrick Filler for Harvest, http://getharvest.com
3
+//
4
+// Version 1.0.0
5
+// Full source at https://github.com/harvesthq/chosen
6
+// Copyright (c) 2011 Harvest http://getharvest.com
7
+
8
+// MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
9
+// This file is generated by `grunt build`, do not edit it by hand.
10
+(function() {
11
+ var $, AbstractChosen, Chosen, SelectParser, _ref,
12
+ __hasProp = {}.hasOwnProperty,
13
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
14
+
15
+ SelectParser = (function() {
16
+ function SelectParser() {
17
+ this.options_index = 0;
18
+ this.parsed = [];
19
+ }
20
+
21
+ SelectParser.prototype.add_node = function(child) {
22
+ if (child.nodeName.toUpperCase() === "OPTGROUP") {
23
+ return this.add_group(child);
24
+ } else {
25
+ return this.add_option(child);
26
+ }
27
+ };
28
+
29
+ SelectParser.prototype.add_group = function(group) {
30
+ var group_position, option, _i, _len, _ref, _results;
31
+
32
+ group_position = this.parsed.length;
33
+ this.parsed.push({
34
+ array_index: group_position,
35
+ group: true,
36
+ label: this.escapeExpression(group.label),
37
+ children: 0,
38
+ disabled: group.disabled
39
+ });
40
+ _ref = group.childNodes;
41
+ _results = [];
42
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
43
+ option = _ref[_i];
44
+ _results.push(this.add_option(option, group_position, group.disabled));
45
+ }
46
+ return _results;
47
+ };
48
+
49
+ SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
50
+ if (option.nodeName.toUpperCase() === "OPTION") {
51
+ if (option.text !== "") {
52
+ if (group_position != null) {
53
+ this.parsed[group_position].children += 1;
54
+ }
55
+ this.parsed.push({
56
+ array_index: this.parsed.length,
57
+ options_index: this.options_index,
58
+ value: option.value,
59
+ text: option.text,
60
+ html: option.innerHTML,
61
+ selected: option.selected,
62
+ disabled: group_disabled === true ? group_disabled : option.disabled,
63
+ group_array_index: group_position,
64
+ classes: option.className,
65
+ style: option.style.cssText
66
+ });
67
+ } else {
68
+ this.parsed.push({
69
+ array_index: this.parsed.length,
70
+ options_index: this.options_index,
71
+ empty: true
72
+ });
73
+ }
74
+ return this.options_index += 1;
75
+ }
76
+ };
77
+
78
+ SelectParser.prototype.escapeExpression = function(text) {
79
+ var map, unsafe_chars;
80
+
81
+ if ((text == null) || text === false) {
82
+ return "";
83
+ }
84
+ if (!/[\&\<\>\"\'\`]/.test(text)) {
85
+ return text;
86
+ }
87
+ map = {
88
+ "<": "&lt;",
89
+ ">": "&gt;",
90
+ '"': "&quot;",
91
+ "'": "&#x27;",
92
+ "`": "&#x60;"
93
+ };
94
+ unsafe_chars = /&(?!\w+;)|[\<\>\"\'\`]/g;
95
+ return text.replace(unsafe_chars, function(chr) {
96
+ return map[chr] || "&amp;";
97
+ });
98
+ };
99
+
100
+ return SelectParser;
101
+
102
+ })();
103
+
104
+ SelectParser.select_to_array = function(select) {
105
+ var child, parser, _i, _len, _ref;
106
+
107
+ parser = new SelectParser();
108
+ _ref = select.childNodes;
109
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
110
+ child = _ref[_i];
111
+ parser.add_node(child);
112
+ }
113
+ return parser.parsed;
114
+ };
115
+
116
+ AbstractChosen = (function() {
117
+ function AbstractChosen(form_field, options) {
118
+ this.form_field = form_field;
119
+ this.options = options != null ? options : {};
120
+ if (!AbstractChosen.browser_is_supported()) {
121
+ return;
122
+ }
123
+ this.is_multiple = this.form_field.multiple;
124
+ this.set_default_text();
125
+ this.set_default_values();
126
+ this.setup();
127
+ this.set_up_html();
128
+ this.register_observers();
129
+ }
130
+
131
+ AbstractChosen.prototype.set_default_values = function() {
132
+ var _this = this;
133
+
134
+ this.click_test_action = function(evt) {
135
+ return _this.test_active_click(evt);
136
+ };
137
+ this.activate_action = function(evt) {
138
+ return _this.activate_field(evt);
139
+ };
140
+ this.active_field = false;
141
+ this.mouse_on_container = false;
142
+ this.results_showing = false;
143
+ this.result_highlighted = null;
144
+ this.result_single_selected = null;
145
+ this.allow_single_deselect = (this.options.allow_single_deselect != null) && (this.form_field.options[0] != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false;
146
+ this.disable_search_threshold = this.options.disable_search_threshold || 0;
147
+ this.disable_search = this.options.disable_search || false;
148
+ this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true;
149
+ this.group_search = this.options.group_search != null ? this.options.group_search : true;
150
+ this.search_contains = this.options.search_contains || false;
151
+ this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true;
152
+ this.max_selected_options = this.options.max_selected_options || Infinity;
153
+ this.inherit_select_classes = this.options.inherit_select_classes || false;
154
+ this.display_selected_options = this.options.display_selected_options != null ? this.options.display_selected_options : true;
155
+ return this.display_disabled_options = this.options.display_disabled_options != null ? this.options.display_disabled_options : true;
156
+ };
157
+
158
+ AbstractChosen.prototype.set_default_text = function() {
159
+ if (this.form_field.getAttribute("data-placeholder")) {
160
+ this.default_text = this.form_field.getAttribute("data-placeholder");
161
+ } else if (this.is_multiple) {
162
+ this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
163
+ } else {
164
+ this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
165
+ }
166
+ return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
167
+ };
168
+
169
+ AbstractChosen.prototype.mouse_enter = function() {
170
+ return this.mouse_on_container = true;
171
+ };
172
+
173
+ AbstractChosen.prototype.mouse_leave = function() {
174
+ return this.mouse_on_container = false;
175
+ };
176
+
177
+ AbstractChosen.prototype.input_focus = function(evt) {
178
+ var _this = this;
179
+
180
+ if (this.is_multiple) {
181
+ if (!this.active_field) {
182
+ return setTimeout((function() {
183
+ return _this.container_mousedown();
184
+ }), 50);
185
+ }
186
+ } else {
187
+ if (!this.active_field) {
188
+ return this.activate_field();
189
+ }
190
+ }
191
+ };
192
+
193
+ AbstractChosen.prototype.input_blur = function(evt) {
194
+ var _this = this;
195
+
196
+ if (!this.mouse_on_container) {
197
+ this.active_field = false;
198
+ return setTimeout((function() {
199
+ return _this.blur_test();
200
+ }), 100);
201
+ }
202
+ };
203
+
204
+ AbstractChosen.prototype.results_option_build = function(options) {
205
+ var content, data, _i, _len, _ref;
206
+
207
+ content = '';
208
+ _ref = this.results_data;
209
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
210
+ data = _ref[_i];
211
+ if (data.group) {
212
+ content += this.result_add_group(data);
213
+ } else {
214
+ content += this.result_add_option(data);
215
+ }
216
+ if (options != null ? options.first : void 0) {
217
+ if (data.selected && this.is_multiple) {
218
+ this.choice_build(data);
219
+ } else if (data.selected && !this.is_multiple) {
220
+ this.single_set_selected_text(data.text);
221
+ }
222
+ }
223
+ }
224
+ return content;
225
+ };
226
+
227
+ AbstractChosen.prototype.result_add_option = function(option) {
228
+ var classes, style;
229
+
230
+ if (!option.search_match) {
231
+ return '';
232
+ }
233
+ if (!this.include_option_in_results(option)) {
234
+ return '';
235
+ }
236
+ classes = [];
237
+ if (!option.disabled && !(option.selected && this.is_multiple)) {
238
+ classes.push("active-result");
239
+ }
240
+ if (option.disabled && !(option.selected && this.is_multiple)) {
241
+ classes.push("disabled-result");
242
+ }
243
+ if (option.selected) {
244
+ classes.push("result-selected");
245
+ }
246
+ if (option.group_array_index != null) {
247
+ classes.push("group-option");
248
+ }
249
+ if (option.classes !== "") {
250
+ classes.push(option.classes);
251
+ }
252
+ style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
253
+ return "<li class=\"" + (classes.join(' ')) + "\"" + style + " data-option-array-index=\"" + option.array_index + "\">" + option.search_text + "</li>";
254
+ };
255
+
256
+ AbstractChosen.prototype.result_add_group = function(group) {
257
+ if (!(group.search_match || group.group_match)) {
258
+ return '';
259
+ }
260
+ if (!(group.active_options > 0)) {
261
+ return '';
262
+ }
263
+ return "<li class=\"group-result\">" + group.search_text + "</li>";
264
+ };
265
+
266
+ AbstractChosen.prototype.results_update_field = function() {
267
+ this.set_default_text();
268
+ if (!this.is_multiple) {
269
+ this.results_reset_cleanup();
270
+ }
271
+ this.result_clear_highlight();
272
+ this.result_single_selected = null;
273
+ this.results_build();
274
+ if (this.results_showing) {
275
+ return this.winnow_results();
276
+ }
277
+ };
278
+
279
+ AbstractChosen.prototype.results_toggle = function() {
280
+ if (this.results_showing) {
281
+ return this.results_hide();
282
+ } else {
283
+ return this.results_show();
284
+ }
285
+ };
286
+
287
+ AbstractChosen.prototype.results_search = function(evt) {
288
+ if (this.results_showing) {
289
+ return this.winnow_results();
290
+ } else {
291
+ return this.results_show();
292
+ }
293
+ };
294
+
295
+ AbstractChosen.prototype.winnow_results = function() {
296
+ var escapedSearchText, option, regex, regexAnchor, results, results_group, searchText, startpos, text, zregex, _i, _len, _ref;
297
+
298
+ this.no_results_clear();
299
+ results = 0;
300
+ searchText = this.get_search_text();
301
+ escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
302
+ regexAnchor = this.search_contains ? "" : "^";
303
+ regex = new RegExp(regexAnchor + escapedSearchText, 'i');
304
+ zregex = new RegExp(escapedSearchText, 'i');
305
+ _ref = this.results_data;
306
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
307
+ option = _ref[_i];
308
+ option.search_match = false;
309
+ results_group = null;
310
+ if (this.include_option_in_results(option)) {
311
+ if (option.group) {
312
+ option.group_match = false;
313
+ option.active_options = 0;
314
+ }
315
+ if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
316
+ results_group = this.results_data[option.group_array_index];
317
+ if (results_group.active_options === 0 && results_group.search_match) {
318
+ results += 1;
319
+ }
320
+ results_group.active_options += 1;
321
+ }
322
+ if (!(option.group && !this.group_search)) {
323
+ option.search_text = option.group ? option.label : option.html;
324
+ option.search_match = this.search_string_match(option.search_text, regex);
325
+ if (option.search_match && !option.group) {
326
+ results += 1;
327
+ }
328
+ if (option.search_match) {
329
+ if (searchText.length) {
330
+ startpos = option.search_text.search(zregex);
331
+ text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
332
+ option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
333
+ }
334
+ if (results_group != null) {
335
+ results_group.group_match = true;
336
+ }
337
+ } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
338
+ option.search_match = true;
339
+ }
340
+ }
341
+ }
342
+ }
343
+ this.result_clear_highlight();
344
+ if (results < 1 && searchText.length) {
345
+ this.update_results_content("");
346
+ return this.no_results(searchText);
347
+ } else {
348
+ this.update_results_content(this.results_option_build());
349
+ return this.winnow_results_set_highlight();
350
+ }
351
+ };
352
+
353
+ AbstractChosen.prototype.search_string_match = function(search_string, regex) {
354
+ var part, parts, _i, _len;
355
+
356
+ if (regex.test(search_string)) {
357
+ return true;
358
+ } else if (this.enable_split_word_search && (search_string.indexOf(" ") >= 0 || search_string.indexOf("[") === 0)) {
359
+ parts = search_string.replace(/\[|\]/g, "").split(" ");
360
+ if (parts.length) {
361
+ for (_i = 0, _len = parts.length; _i < _len; _i++) {
362
+ part = parts[_i];
363
+ if (regex.test(part)) {
364
+ return true;
365
+ }
366
+ }
367
+ }
368
+ }
369
+ };
370
+
371
+ AbstractChosen.prototype.choices_count = function() {
372
+ var option, _i, _len, _ref;
373
+
374
+ if (this.selected_option_count != null) {
375
+ return this.selected_option_count;
376
+ }
377
+ this.selected_option_count = 0;
378
+ _ref = this.form_field.options;
379
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
380
+ option = _ref[_i];
381
+ if (option.selected) {
382
+ this.selected_option_count += 1;
383
+ }
384
+ }
385
+ return this.selected_option_count;
386
+ };
387
+
388
+ AbstractChosen.prototype.choices_click = function(evt) {
389
+ evt.preventDefault();
390
+ if (!(this.results_showing || this.is_disabled)) {
391
+ return this.results_show();
392
+ }
393
+ };
394
+
395
+ AbstractChosen.prototype.keyup_checker = function(evt) {
396
+ var stroke, _ref;
397
+
398
+ stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
399
+ this.search_field_scale();
400
+ switch (stroke) {
401
+ case 8:
402
+ if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) {
403
+ return this.keydown_backstroke();
404
+ } else if (!this.pending_backstroke) {
405
+ this.result_clear_highlight();
406
+ return this.results_search();
407
+ }
408
+ break;
409
+ case 13:
410
+ evt.preventDefault();
411
+ if (this.results_showing) {
412
+ return this.result_select(evt);
413
+ }
414
+ break;
415
+ case 27:
416
+ if (this.results_showing) {
417
+ this.results_hide();
418
+ }
419
+ return true;
420
+ case 9:
421
+ case 38:
422
+ case 40:
423
+ case 16:
424
+ case 91:
425
+ case 17:
426
+ break;
427
+ default:
428
+ return this.results_search();
429
+ }
430
+ };
431
+
432
+ AbstractChosen.prototype.container_width = function() {
433
+ if (this.options.width != null) {
434
+ return this.options.width;
435
+ } else {
436
+ return "" + this.form_field.offsetWidth + "px";
437
+ }
438
+ };
439
+
440
+ AbstractChosen.prototype.include_option_in_results = function(option) {
441
+ if (this.is_multiple && (!this.display_selected_options && option.selected)) {
442
+ return false;
443
+ }
444
+ if (!this.display_disabled_options && option.disabled) {
445
+ return false;
446
+ }
447
+ if (option.empty) {
448
+ return false;
449
+ }
450
+ return true;
451
+ };
452
+
453
+ AbstractChosen.browser_is_supported = function() {
454
+ if (window.navigator.appName === "Microsoft Internet Explorer") {
455
+ return document.documentMode >= 8;
456
+ }
457
+ if (/iP(od|hone)/i.test(window.navigator.userAgent)) {
458
+ return false;
459
+ }
460
+ if (/Android/i.test(window.navigator.userAgent)) {
461
+ if (/Mobile/i.test(window.navigator.userAgent)) {
462
+ return false;
463
+ }
464
+ }
465
+ return true;
466
+ };
467
+
468
+ AbstractChosen.default_multiple_text = "Select Some Options";
469
+
470
+ AbstractChosen.default_single_text = "Select an Option";
471
+
472
+ AbstractChosen.default_no_result_text = "No results match";
473
+
474
+ return AbstractChosen;
475
+
476
+ })();
477
+
478
+ $ = jQuery;
479
+
480
+ $.fn.extend({
481
+ chosen: function(options) {
482
+ if (!AbstractChosen.browser_is_supported()) {
483
+ return this;
484
+ }
485
+ return this.each(function(input_field) {
486
+ var $this, chosen;
487
+
488
+ $this = $(this);
489
+ chosen = $this.data('chosen');
490
+ if (options === 'destroy' && chosen) {
491
+ chosen.destroy();
492
+ } else if (!chosen) {
493
+ $this.data('chosen', new Chosen(this, options));
494
+ }
495
+ });
496
+ }
497
+ });
498
+
499
+ Chosen = (function(_super) {
500
+ __extends(Chosen, _super);
501
+
502
+ function Chosen() {
503
+ _ref = Chosen.__super__.constructor.apply(this, arguments);
504
+ return _ref;
505
+ }
506
+
507
+ Chosen.prototype.setup = function() {
508
+ this.form_field_jq = $(this.form_field);
509
+ this.current_selectedIndex = this.form_field.selectedIndex;
510
+ return this.is_rtl = this.form_field_jq.hasClass("chosen-rtl");
511
+ };
512
+
513
+ Chosen.prototype.set_up_html = function() {
514
+ var container_classes, container_props;
515
+
516
+ container_classes = ["chosen-container"];
517
+ container_classes.push("chosen-container-" + (this.is_multiple ? "multi" : "single"));
518
+ if (this.inherit_select_classes && this.form_field.className) {
519
+ container_classes.push(this.form_field.className);
520
+ }
521
+ if (this.is_rtl) {
522
+ container_classes.push("chosen-rtl");
523
+ }
524
+ container_props = {
525
+ 'class': container_classes.join(' '),
526
+ 'style': "width: " + (this.container_width()) + ";",
527
+ 'title': this.form_field.title
528
+ };
529
+ if (this.form_field.id.length) {
530
+ container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chosen";
531
+ }
532
+ this.container = $("<div />", container_props);
533
+ if (this.is_multiple) {
534
+ this.container.html('<ul class="chosen-choices"><li class="search-field"><input type="text" value="' + this.default_text + '" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chosen-drop"><ul class="chosen-results"></ul></div>');
535
+ } else {
536
+ this.container.html('<a class="chosen-single chosen-default" tabindex="-1"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chosen-drop"><div class="chosen-search"><input type="text" autocomplete="off" /></div><ul class="chosen-results"></ul></div>');
537
+ }
538
+ this.form_field_jq.hide().after(this.container);
539
+ this.dropdown = this.container.find('div.chosen-drop').first();
540
+ this.search_field = this.container.find('input').first();
541
+ this.search_results = this.container.find('ul.chosen-results').first();
542
+ this.search_field_scale();
543
+ this.search_no_results = this.container.find('li.no-results').first();
544
+ if (this.is_multiple) {
545
+ this.search_choices = this.container.find('ul.chosen-choices').first();
546
+ this.search_container = this.container.find('li.search-field').first();
547
+ } else {
548
+ this.search_container = this.container.find('div.chosen-search').first();
549
+ this.selected_item = this.container.find('.chosen-single').first();
550
+ }
551
+ this.results_build();
552
+ this.set_tab_index();
553
+ this.set_label_behavior();
554
+ return this.form_field_jq.trigger("chosen:ready", {
555
+ chosen: this
556
+ });
557
+ };
558
+
559
+ Chosen.prototype.register_observers = function() {
560
+ var _this = this;
561
+
562
+ this.container.bind('mousedown.chosen', function(evt) {
563
+ _this.container_mousedown(evt);
564
+ });
565
+ this.container.bind('mouseup.chosen', function(evt) {
566
+ _this.container_mouseup(evt);
567
+ });
568
+ this.container.bind('mouseenter.chosen', function(evt) {
569
+ _this.mouse_enter(evt);
570
+ });
571
+ this.container.bind('mouseleave.chosen', function(evt) {
572
+ _this.mouse_leave(evt);
573
+ });
574
+ this.search_results.bind('mouseup.chosen', function(evt) {
575
+ _this.search_results_mouseup(evt);
576
+ });
577
+ this.search_results.bind('mouseover.chosen', function(evt) {
578
+ _this.search_results_mouseover(evt);
579
+ });
580
+ this.search_results.bind('mouseout.chosen', function(evt) {
581
+ _this.search_results_mouseout(evt);
582
+ });
583
+ this.search_results.bind('mousewheel.chosen DOMMouseScroll.chosen', function(evt) {
584
+ _this.search_results_mousewheel(evt);
585
+ });
586
+ this.form_field_jq.bind("chosen:updated.chosen", function(evt) {
587
+ _this.results_update_field(evt);
588
+ });
589
+ this.form_field_jq.bind("chosen:activate.chosen", function(evt) {
590
+ _this.activate_field(evt);
591
+ });
592
+ this.form_field_jq.bind("chosen:open.chosen", function(evt) {
593
+ _this.container_mousedown(evt);
594
+ });
595
+ this.search_field.bind('blur.chosen', function(evt) {
596
+ _this.input_blur(evt);
597
+ });
598
+ this.search_field.bind('keyup.chosen', function(evt) {
599
+ _this.keyup_checker(evt);
600
+ });
601
+ this.search_field.bind('keydown.chosen', function(evt) {
602
+ _this.keydown_checker(evt);
603
+ });
604
+ this.search_field.bind('focus.chosen', function(evt) {
605
+ _this.input_focus(evt);
606
+ });
607
+ if (this.is_multiple) {
608
+ return this.search_choices.bind('click.chosen', function(evt) {
609
+ _this.choices_click(evt);
610
+ });
611
+ } else {
612
+ return this.container.bind('click.chosen', function(evt) {
613
+ evt.preventDefault();
614
+ });
615
+ }
616
+ };
617
+
618
+ Chosen.prototype.destroy = function() {
619
+ $(document).unbind("click.chosen", this.click_test_action);
620
+ if (this.search_field[0].tabIndex) {
621
+ this.form_field_jq[0].tabIndex = this.search_field[0].tabIndex;
622
+ }
623
+ this.container.remove();
624
+ this.form_field_jq.removeData('chosen');
625
+ return this.form_field_jq.show();
626
+ };
627
+
628
+ Chosen.prototype.search_field_disabled = function() {
629
+ this.is_disabled = this.form_field_jq[0].disabled;
630
+ if (this.is_disabled) {
631
+ this.container.addClass('chosen-disabled');
632
+ this.search_field[0].disabled = true;
633
+ if (!this.is_multiple) {
634
+ this.selected_item.unbind("focus.chosen", this.activate_action);
635
+ }
636
+ return this.close_field();
637
+ } else {
638
+ this.container.removeClass('chosen-disabled');
639
+ this.search_field[0].disabled = false;
640
+ if (!this.is_multiple) {
641
+ return this.selected_item.bind("focus.chosen", this.activate_action);
642
+ }
643
+ }
644
+ };
645
+
646
+ Chosen.prototype.container_mousedown = function(evt) {
647
+ if (!this.is_disabled) {
648
+ if (evt && evt.type === "mousedown" && !this.results_showing) {
649
+ evt.preventDefault();
650
+ }
651
+ if (!((evt != null) && ($(evt.target)).hasClass("search-choice-close"))) {
652
+ if (!this.active_field) {
653
+ if (this.is_multiple) {
654
+ this.search_field.val("");
655
+ }
656
+ $(document).bind('click.chosen', this.click_test_action);
657
+ this.results_show();
658
+ } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chosen-single").length)) {
659
+ evt.preventDefault();
660
+ this.results_toggle();
661
+ }
662
+ return this.activate_field();
663
+ }
664
+ }
665
+ };
666
+
667
+ Chosen.prototype.container_mouseup = function(evt) {
668
+ if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
669
+ return this.results_reset(evt);
670
+ }
671
+ };
672
+
673
+ Chosen.prototype.search_results_mousewheel = function(evt) {
674
+ var delta, _ref1, _ref2;
675
+
676
+ delta = -((_ref1 = evt.originalEvent) != null ? _ref1.wheelDelta : void 0) || ((_ref2 = evt.originialEvent) != null ? _ref2.detail : void 0);
677
+ if (delta != null) {
678
+ evt.preventDefault();
679
+ if (evt.type === 'DOMMouseScroll') {
680
+ delta = delta * 40;
681
+ }
682
+ return this.search_results.scrollTop(delta + this.search_results.scrollTop());
683
+ }
684
+ };
685
+
686
+ Chosen.prototype.blur_test = function(evt) {
687
+ if (!this.active_field && this.container.hasClass("chosen-container-active")) {
688
+ return this.close_field();
689
+ }
690
+ };
691
+
692
+ Chosen.prototype.close_field = function() {
693
+ $(document).unbind("click.chosen", this.click_test_action);
694
+ this.active_field = false;
695
+ this.results_hide();
696
+ this.container.removeClass("chosen-container-active");
697
+ this.clear_backstroke();
698
+ this.show_search_field_default();
699
+ return this.search_field_scale();
700
+ };
701
+
702
+ Chosen.prototype.activate_field = function() {
703
+ this.container.addClass("chosen-container-active");
704
+ this.active_field = true;
705
+ this.search_field.val(this.search_field.val());
706
+ return this.search_field.focus();
707
+ };
708
+
709
+ Chosen.prototype.test_active_click = function(evt) {
710
+ if (this.container.is($(evt.target).closest('.chosen-container'))) {
711
+ return this.active_field = true;
712
+ } else {
713
+ return this.close_field();
714
+ }
715
+ };
716
+
717
+ Chosen.prototype.results_build = function() {
718
+ this.parsing = true;
719
+ this.selected_option_count = null;
720
+ this.results_data = SelectParser.select_to_array(this.form_field);
721
+ if (this.is_multiple) {
722
+ this.search_choices.find("li.search-choice").remove();
723
+ } else if (!this.is_multiple) {
724
+ this.single_set_selected_text();
725
+ if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
726
+ this.search_field[0].readOnly = true;
727
+ this.container.addClass("chosen-container-single-nosearch");
728
+ } else {
729
+ this.search_field[0].readOnly = false;
730
+ this.container.removeClass("chosen-container-single-nosearch");
731
+ }
732
+ }
733
+ this.update_results_content(this.results_option_build({
734
+ first: true
735
+ }));
736
+ this.search_field_disabled();
737
+ this.show_search_field_default();
738
+ this.search_field_scale();
739
+ return this.parsing = false;
740
+ };
741
+
742
+ Chosen.prototype.result_do_highlight = function(el) {
743
+ var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
744
+
745
+ if (el.length) {
746
+ this.result_clear_highlight();
747
+ this.result_highlight = el;
748
+ this.result_highlight.addClass("highlighted");
749
+ maxHeight = parseInt(this.search_results.css("maxHeight"), 10);
750
+ visible_top = this.search_results.scrollTop();
751
+ visible_bottom = maxHeight + visible_top;
752
+ high_top = this.result_highlight.position().top + this.search_results.scrollTop();
753
+ high_bottom = high_top + this.result_highlight.outerHeight();
754
+ if (high_bottom >= visible_bottom) {
755
+ return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0);
756
+ } else if (high_top < visible_top) {
757
+ return this.search_results.scrollTop(high_top);
758
+ }
759
+ }
760
+ };
761
+
762
+ Chosen.prototype.result_clear_highlight = function() {
763
+ if (this.result_highlight) {
764
+ this.result_highlight.removeClass("highlighted");
765
+ }
766
+ return this.result_highlight = null;
767
+ };
768
+
769
+ Chosen.prototype.results_show = function() {
770
+ if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
771
+ this.form_field_jq.trigger("chosen:maxselected", {
772
+ chosen: this
773
+ });
774
+ return false;
775
+ }
776
+ this.container.addClass("chosen-with-drop");
777
+ this.form_field_jq.trigger("chosen:showing_dropdown", {
778
+ chosen: this
779
+ });
780
+ this.results_showing = true;
781
+ this.search_field.focus();
782
+ this.search_field.val(this.search_field.val());
783
+ return this.winnow_results();
784
+ };
785
+
786
+ Chosen.prototype.update_results_content = function(content) {
787
+ return this.search_results.html(content);
788
+ };
789
+
790
+ Chosen.prototype.results_hide = function() {
791
+ if (this.results_showing) {
792
+ this.result_clear_highlight();
793
+ this.container.removeClass("chosen-with-drop");
794
+ this.form_field_jq.trigger("chosen:hiding_dropdown", {
795
+ chosen: this
796
+ });
797
+ }
798
+ return this.results_showing = false;
799
+ };
800
+
801
+ Chosen.prototype.set_tab_index = function(el) {
802
+ var ti;
803
+
804
+ if (this.form_field.tabIndex) {
805
+ ti = this.form_field.tabIndex;
806
+ this.form_field.tabIndex = -1;
807
+ return this.search_field[0].tabIndex = ti;
808
+ }
809
+ };
810
+
811
+ Chosen.prototype.set_label_behavior = function() {
812
+ var _this = this;
813
+
814
+ this.form_field_label = this.form_field_jq.parents("label");
815
+ if (!this.form_field_label.length && this.form_field.id.length) {
816
+ this.form_field_label = $("label[for='" + this.form_field.id + "']");
817
+ }
818
+ if (this.form_field_label.length > 0) {
819
+ return this.form_field_label.bind('click.chosen', function(evt) {
820
+ if (_this.is_multiple) {
821
+ return _this.container_mousedown(evt);
822
+ } else {
823
+ return _this.activate_field();
824
+ }
825
+ });
826
+ }
827
+ };
828
+
829
+ Chosen.prototype.show_search_field_default = function() {
830
+ if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
831
+ this.search_field.val(this.default_text);
832
+ return this.search_field.addClass("default");
833
+ } else {
834
+ this.search_field.val("");
835
+ return this.search_field.removeClass("default");
836
+ }
837
+ };
838
+
839
+ Chosen.prototype.search_results_mouseup = function(evt) {
840
+ var target;
841
+
842
+ target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
843
+ if (target.length) {
844
+ this.result_highlight = target;
845
+ this.result_select(evt);
846
+ return this.search_field.focus();
847
+ }
848
+ };
849
+
850
+ Chosen.prototype.search_results_mouseover = function(evt) {
851
+ var target;
852
+
853
+ target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
854
+ if (target) {
855
+ return this.result_do_highlight(target);
856
+ }
857
+ };
858
+
859
+ Chosen.prototype.search_results_mouseout = function(evt) {
860
+ if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) {
861
+ return this.result_clear_highlight();
862
+ }
863
+ };
864
+
865
+ Chosen.prototype.choice_build = function(item) {
866
+ var choice, close_link,
867
+ _this = this;
868
+
869
+ choice = $('<li />', {
870
+ "class": "search-choice"
871
+ }).html("<span>" + item.html + "</span>");
872
+ if (item.disabled) {
873
+ choice.addClass('search-choice-disabled');
874
+ } else {
875
+ close_link = $('<a />', {
876
+ "class": 'search-choice-close',
877
+ 'data-option-array-index': item.array_index
878
+ });
879
+ close_link.bind('click.chosen', function(evt) {
880
+ return _this.choice_destroy_link_click(evt);
881
+ });
882
+ choice.append(close_link);
883
+ }
884
+ return this.search_container.before(choice);
885
+ };
886
+
887
+ Chosen.prototype.choice_destroy_link_click = function(evt) {
888
+ evt.preventDefault();
889
+ evt.stopPropagation();
890
+ if (!this.is_disabled) {
891
+ return this.choice_destroy($(evt.target));
892
+ }
893
+ };
894
+
895
+ Chosen.prototype.choice_destroy = function(link) {
896
+ if (this.result_deselect(link[0].getAttribute("data-option-array-index"))) {
897
+ this.show_search_field_default();
898
+ if (this.is_multiple && this.choices_count() > 0 && this.search_field.val().length < 1) {
899
+ this.results_hide();
900
+ }
901
+ link.parents('li').first().remove();
902
+ return this.search_field_scale();
903
+ }
904
+ };
905
+
906
+ Chosen.prototype.results_reset = function() {
907
+ this.form_field.options[0].selected = true;
908
+ this.selected_option_count = null;
909
+ this.single_set_selected_text();
910
+ this.show_search_field_default();
911
+ this.results_reset_cleanup();
912
+ this.form_field_jq.trigger("change");
913
+ if (this.active_field) {
914
+ return this.results_hide();
915
+ }
916
+ };
917
+
918
+ Chosen.prototype.results_reset_cleanup = function() {
919
+ this.current_selectedIndex = this.form_field.selectedIndex;
920
+ return this.selected_item.find("abbr").remove();
921
+ };
922
+
923
+ Chosen.prototype.result_select = function(evt) {
924
+ var high, item, selected_index;
925
+
926
+ if (this.result_highlight) {
927
+ high = this.result_highlight;
928
+ this.result_clear_highlight();
929
+ if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
930
+ this.form_field_jq.trigger("chosen:maxselected", {
931
+ chosen: this
932
+ });
933
+ return false;
934
+ }
935
+ if (this.is_multiple) {
936
+ high.removeClass("active-result");
937
+ } else {
938
+ if (this.result_single_selected) {
939
+ this.result_single_selected.removeClass("result-selected");
940
+ selected_index = this.result_single_selected[0].getAttribute('data-option-array-index');
941
+ this.results_data[selected_index].selected = false;
942
+ }
943
+ this.result_single_selected = high;
944
+ }
945
+ high.addClass("result-selected");
946
+ item = this.results_data[high[0].getAttribute("data-option-array-index")];
947
+ item.selected = true;
948
+ this.form_field.options[item.options_index].selected = true;
949
+ this.selected_option_count = null;
950
+ if (this.is_multiple) {
951
+ this.choice_build(item);
952
+ } else {
953
+ this.single_set_selected_text(item.text);
954
+ }
955
+ if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
956
+ this.results_hide();
957
+ }
958
+ this.search_field.val("");
959
+ if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
960
+ this.form_field_jq.trigger("change", {
961
+ 'selected': this.form_field.options[item.options_index].value
962
+ });
963
+ }
964
+ this.current_selectedIndex = this.form_field.selectedIndex;
965
+ return this.search_field_scale();
966
+ }
967
+ };
968
+
969
+ Chosen.prototype.single_set_selected_text = function(text) {
970
+ if (text == null) {
971
+ text = this.default_text;
972
+ }
973
+ if (text === this.default_text) {
974
+ this.selected_item.addClass("chosen-default");
975
+ } else {
976
+ this.single_deselect_control_build();
977
+ this.selected_item.removeClass("chosen-default");
978
+ }
979
+ return this.selected_item.find("span").text(text);
980
+ };
981
+
982
+ Chosen.prototype.result_deselect = function(pos) {
983
+ var result_data;
984
+
985
+ result_data = this.results_data[pos];
986
+ if (!this.form_field.options[result_data.options_index].disabled) {
987
+ result_data.selected = false;
988
+ this.form_field.options[result_data.options_index].selected = false;
989
+ this.selected_option_count = null;
990
+ this.result_clear_highlight();
991
+ if (this.results_showing) {
992
+ this.winnow_results();
993
+ }
994
+ this.form_field_jq.trigger("change", {
995
+ deselected: this.form_field.options[result_data.options_index].value
996
+ });
997
+ this.search_field_scale();
998
+ return true;
999
+ } else {
1000
+ return false;
1001
+ }
1002
+ };
1003
+
1004
+ Chosen.prototype.single_deselect_control_build = function() {
1005
+ if (!this.allow_single_deselect) {
1006
+ return;
1007
+ }
1008
+ if (!this.selected_item.find("abbr").length) {
1009
+ this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>");
1010
+ }
1011
+ return this.selected_item.addClass("chosen-single-with-deselect");
1012
+ };
1013
+
1014
+ Chosen.prototype.get_search_text = function() {
1015
+ if (this.search_field.val() === this.default_text) {
1016
+ return "";
1017
+ } else {
1018
+ return $('<div/>').text($.trim(this.search_field.val())).html();
1019
+ }
1020
+ };
1021
+
1022
+ Chosen.prototype.winnow_results_set_highlight = function() {
1023
+ var do_high, selected_results;
1024
+
1025
+ selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
1026
+ do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
1027
+ if (do_high != null) {
1028
+ return this.result_do_highlight(do_high);
1029
+ }
1030
+ };
1031
+
1032
+ Chosen.prototype.no_results = function(terms) {
1033
+ var no_results_html;
1034
+
1035
+ no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
1036
+ no_results_html.find("span").first().html(terms);
1037
+ return this.search_results.append(no_results_html);
1038
+ };
1039
+
1040
+ Chosen.prototype.no_results_clear = function() {
1041
+ return this.search_results.find(".no-results").remove();
1042
+ };
1043
+
1044
+ Chosen.prototype.keydown_arrow = function() {
1045
+ var next_sib;
1046
+
1047
+ if (this.results_showing && this.result_highlight) {
1048
+ next_sib = this.result_highlight.nextAll("li.active-result").first();
1049
+ if (next_sib) {
1050
+ return this.result_do_highlight(next_sib);
1051
+ }
1052
+ } else {
1053
+ return this.results_show();
1054
+ }
1055
+ };
1056
+
1057
+ Chosen.prototype.keyup_arrow = function() {
1058
+ var prev_sibs;
1059
+
1060
+ if (!this.results_showing && !this.is_multiple) {
1061
+ return this.results_show();
1062
+ } else if (this.result_highlight) {
1063
+ prev_sibs = this.result_highlight.prevAll("li.active-result");
1064
+ if (prev_sibs.length) {
1065
+ return this.result_do_highlight(prev_sibs.first());
1066
+ } else {
1067
+ if (this.choices_count() > 0) {
1068
+ this.results_hide();
1069
+ }
1070
+ return this.result_clear_highlight();
1071
+ }
1072
+ }
1073
+ };
1074
+
1075
+ Chosen.prototype.keydown_backstroke = function() {
1076
+ var next_available_destroy;
1077
+
1078
+ if (this.pending_backstroke) {
1079
+ this.choice_destroy(this.pending_backstroke.find("a").first());
1080
+ return this.clear_backstroke();
1081
+ } else {
1082
+ next_available_destroy = this.search_container.siblings("li.search-choice").last();
1083
+ if (next_available_destroy.length && !next_available_destroy.hasClass("search-choice-disabled")) {
1084
+ this.pending_backstroke = next_available_destroy;
1085
+ if (this.single_backstroke_delete) {
1086
+ return this.keydown_backstroke();
1087
+ } else {
1088
+ return this.pending_backstroke.addClass("search-choice-focus");
1089
+ }
1090
+ }
1091
+ }
1092
+ };
1093
+
1094
+ Chosen.prototype.clear_backstroke = function() {
1095
+ if (this.pending_backstroke) {
1096
+ this.pending_backstroke.removeClass("search-choice-focus");
1097
+ }
1098
+ return this.pending_backstroke = null;
1099
+ };
1100
+
1101
+ Chosen.prototype.keydown_checker = function(evt) {
1102
+ var stroke, _ref1;
1103
+
1104
+ stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode;
1105
+ this.search_field_scale();
1106
+ if (stroke !== 8 && this.pending_backstroke) {
1107
+ this.clear_backstroke();
1108
+ }
1109
+ switch (stroke) {
1110
+ case 8:
1111
+ this.backstroke_length = this.search_field.val().length;
1112
+ break;
1113
+ case 9:
1114
+ if (this.results_showing && !this.is_multiple) {
1115
+ this.result_select(evt);
1116
+ }
1117
+ this.mouse_on_container = false;
1118
+ break;
1119
+ case 13:
1120
+ evt.preventDefault();
1121
+ break;
1122
+ case 38:
1123
+ evt.preventDefault();
1124
+ this.keyup_arrow();
1125
+ break;
1126
+ case 40:
1127
+ evt.preventDefault();
1128
+ this.keydown_arrow();
1129
+ break;
1130
+ }
1131
+ };
1132
+
1133
+ Chosen.prototype.search_field_scale = function() {
1134
+ var div, f_width, h, style, style_block, styles, w, _i, _len;
1135
+
1136
+ if (this.is_multiple) {
1137
+ h = 0;
1138
+ w = 0;
1139
+ style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
1140
+ styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
1141
+ for (_i = 0, _len = styles.length; _i < _len; _i++) {
1142
+ style = styles[_i];
1143
+ style_block += style + ":" + this.search_field.css(style) + ";";
1144
+ }
1145
+ div = $('<div />', {
1146
+ 'style': style_block
1147
+ });
1148
+ div.text(this.search_field.val());
1149
+ $('body').append(div);
1150
+ w = div.width() + 25;
1151
+ div.remove();
1152
+ f_width = this.container.outerWidth();
1153
+ if (w > f_width - 10) {
1154
+ w = f_width - 10;
1155
+ }
1156
+ return this.search_field.css({
1157
+ 'width': w + 'px'
1158
+ });
1159
+ }
1160
+ };
1161
+
1162
+ return Chosen;
1163
+
1164
+ })(AbstractChosen);
1165
+
1166
+}).call(this);