mokio 0.0.15 → 2.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +5 -5
  2. data/app/assets/javascripts/backend.js +6 -3
  3. data/app/assets/javascripts/backend/datatable.js.coffee.erb +28 -26
  4. data/app/assets/javascripts/backend/head.js +1 -0
  5. data/app/assets/javascripts/backend/main.js +69 -44
  6. data/app/assets/javascripts/backend/mokio_dynamic_fields.js +20 -0
  7. data/app/assets/javascripts/backend/vendors/select2/select2.min.js +2 -0
  8. data/app/assets/stylesheets/backend.scss +6 -2
  9. data/app/assets/stylesheets/backend/custom.scss +13 -1
  10. data/app/assets/stylesheets/backend/main.scss +74 -73
  11. data/app/assets/stylesheets/backend/plugins/forms/select2/select2.scss +4 -0
  12. data/app/assets/stylesheets/backend/plugins/tables/dataTables/{jquery.dataTables.scss → jquery.dataTables.css} +7 -7
  13. data/app/assets/stylesheets/backend/theme/main.scss +413 -0
  14. data/app/assets/stylesheets/backend/theme/nice-select/_nice-select-prefixed.scss +190 -0
  15. data/app/assets/stylesheets/backend/theme/nice-select/nice-select.scss +178 -0
  16. data/app/assets/stylesheets/backend/theme/nice-select/style.scss +20 -0
  17. data/app/assets/stylesheets/backend/vendors/select2/select2.min.css +1 -0
  18. data/app/datatables/commons_datatable.rb +56 -36
  19. data/app/helpers/mokio/backend/backend_helper.rb +27 -1
  20. data/app/helpers/mokio/backend/common_helper.rb +9 -4
  21. data/app/helpers/mokio/backend/dynamic_fields_helper.rb +91 -0
  22. data/app/models/devise_custom_failure.rb +9 -0
  23. data/app/models/mokio/article.rb +0 -1
  24. data/app/models/mokio/contact.rb +0 -1
  25. data/app/models/mokio/content.rb +0 -1
  26. data/app/models/mokio/menu.rb +0 -1
  27. data/app/models/mokio/mov_gallery.rb +0 -1
  28. data/app/models/mokio/pic_gallery.rb +0 -1
  29. data/app/models/mokio/seo_tag.rb +5 -0
  30. data/app/models/mokio/support.rb +0 -1
  31. data/app/uploaders/ckeditor_picture_uploader.rb +2 -2
  32. data/app/views/devise/sessions/new.html.haml +2 -1
  33. data/app/views/mokio/articles/_form.html.slim +6 -3
  34. data/app/views/mokio/common/_form_content.html.slim +4 -4
  35. data/app/views/mokio/common/edit.html.slim +1 -5
  36. data/app/views/mokio/common/index.html.slim +3 -0
  37. data/app/views/mokio/common/modals/_modal_seo_tags.html.slim +47 -0
  38. data/app/views/mokio/common/new.html.slim +1 -4
  39. data/app/views/mokio/contacts/_form.html.slim +9 -7
  40. data/app/views/mokio/contents/new.html.slim +1 -2
  41. data/app/views/mokio/data_files/_file_upload.html.erb +1 -6
  42. data/app/views/mokio/layout/sidebar.html.slim +6 -13
  43. data/app/views/mokio/menus/_form.html.slim +97 -0
  44. data/app/views/mokio/menus/{_form.html.haml → _old_form.html.haml} +5 -6
  45. data/app/views/mokio/menus/{_form_slim.html.slim → _old_form_slim.html.slim} +8 -10
  46. data/app/views/mokio/menus/new_menu_position.slim +1 -1
  47. data/app/views/mokio/mov_galleries/_form_inputs.html.slim +3 -3
  48. data/app/views/mokio/pic_galleries/_form_inputs.html.slim +3 -3
  49. data/app/views/mokio/static_modules/_form.html.slim +2 -1
  50. data/config/initializers/frontend.rb +3 -0
  51. data/config/initializers/simple_form.rb +9 -0
  52. data/config/locales/en.yml +16 -26
  53. data/config/locales/pl.yml +11 -20
  54. data/db/migrate/20140422135850_add_mokio_to_application.rb +22 -24
  55. data/db/migrate/20191217135611_create_mokio_seo_tags.rb +11 -0
  56. data/lib/mokio.rb +37 -10
  57. data/lib/mokio/concerns.rb +3 -13
  58. data/lib/mokio/concerns/common/controller_functions.rb +1 -15
  59. data/lib/mokio/concerns/common/controller_object.rb +2 -2
  60. data/lib/mokio/concerns/common/seo_tags.rb +32 -0
  61. data/lib/mokio/concerns/common/services/sitemap_service.rb +196 -0
  62. data/lib/mokio/concerns/controllers/application.rb +5 -5
  63. data/lib/mokio/concerns/controllers/base.rb +12 -12
  64. data/lib/mokio/concerns/controllers/common.rb +6 -4
  65. data/lib/mokio/concerns/controllers/data_files.rb +3 -3
  66. data/lib/mokio/concerns/controllers/menus.rb +11 -13
  67. data/lib/mokio/concerns/controllers/mov_galleries.rb +1 -2
  68. data/lib/mokio/concerns/controllers/photos.rb +10 -10
  69. data/lib/mokio/concerns/controllers/pic_galleries.rb +2 -3
  70. data/lib/mokio/concerns/controllers/users.rb +4 -3
  71. data/lib/mokio/concerns/controllers/youtubes.rb +7 -7
  72. data/lib/mokio/concerns/models/available_module.rb +3 -3
  73. data/lib/mokio/concerns/models/common.rb +1 -29
  74. data/lib/mokio/concerns/models/contact_template.rb +0 -4
  75. data/lib/mokio/concerns/models/content.rb +15 -7
  76. data/lib/mokio/concerns/models/data_file.rb +3 -9
  77. data/lib/mokio/concerns/models/lang.rb +11 -7
  78. data/lib/mokio/concerns/models/menu.rb +4 -4
  79. data/lib/mokio/concerns/models/module_position.rb +1 -1
  80. data/lib/mokio/concerns/models/recipient.rb +0 -2
  81. data/lib/mokio/concerns/models/seo_tag.rb +118 -0
  82. data/lib/mokio/concerns/models/static_module.rb +2 -2
  83. data/lib/mokio/concerns/models/user.rb +1 -1
  84. data/lib/mokio/engine.rb +7 -17
  85. data/lib/mokio/frontend_helpers.rb +1 -0
  86. data/lib/mokio/frontend_helpers/seo_tag_helper.rb +33 -0
  87. data/lib/mokio/simple-form-wrappers.rb +11 -0
  88. data/lib/mokio/version.rb +1 -1
  89. data/lib/tasks/mokio_install.rake +37 -9
  90. data/spec/dummy/Rakefile +1 -3
  91. data/spec/dummy/app/controllers/application_controller.rb +1 -1
  92. data/spec/dummy/config.ru +1 -2
  93. data/spec/dummy/config/application.rb +9 -12
  94. data/spec/dummy/config/database.yml +18 -0
  95. data/spec/dummy/config/environment.rb +1 -1
  96. data/spec/dummy/log/development.log +0 -0
  97. data/spec/dummy/log/development_mokio.log +9 -0
  98. data/spec/models/content_spec.rb +0 -1
  99. data/spec/models/menu_spec.rb +8 -9
  100. data/spec/models/mov_gallery_spec.rb +0 -1
  101. data/spec/models/pic_gallery_spec.rb +0 -1
  102. metadata +212 -407
  103. data/README.rdoc +0 -47
  104. data/app/assets/javascripts/backend/plugins/forms/select/select2.js +0 -2195
  105. data/app/assets/javascripts/backend/plugins/forms/select/select2.min.js +0 -76
  106. data/app/assets/javascripts/backend/plugins/tables/dataTables/jquery.dataTables.js +0 -12099
  107. data/app/assets/javascripts/backend/plugins/tables/dataTables/jquery.dataTables.min.js +0 -155
  108. data/app/controllers/mokio/base_articles_controller.rb +0 -3
  109. data/app/controllers/mokio/base_contacts_controller.rb +0 -5
  110. data/app/controllers/mokio/base_contents_controller.rb +0 -3
  111. data/app/controllers/mokio/base_mov_galleries_controller.rb +0 -3
  112. data/app/controllers/mokio/base_pic_galleries_controller.rb +0 -4
  113. data/app/controllers/passwords_controller.rb +0 -14
  114. data/app/models/mokio/base_article.rb +0 -29
  115. data/app/models/mokio/base_contact.rb +0 -30
  116. data/app/models/mokio/base_content.rb +0 -5
  117. data/app/models/mokio/base_mov_gallery.rb +0 -30
  118. data/app/models/mokio/base_pic_gallery.rb +0 -30
  119. data/app/models/mokio/meta.rb +0 -24
  120. data/app/views/mokio/backend/articles/_form.html.haml +0 -9
  121. data/app/views/mokio/backend/common/_form_content.html.haml +0 -36
  122. data/app/views/mokio/backend/common/_gmap.html.haml +0 -39
  123. data/app/views/mokio/backend/common/_main_pic.html.haml +0 -4
  124. data/app/views/mokio/backend/common/_meta.html.haml +0 -51
  125. data/app/views/mokio/backend/common/_notice.html.haml +0 -3
  126. data/app/views/mokio/backend/common/_obj.html.haml +0 -16
  127. data/app/views/mokio/backend/common/copy.html.haml +0 -5
  128. data/app/views/mokio/backend/common/edit.html.haml +0 -6
  129. data/app/views/mokio/backend/common/index.html.haml +0 -19
  130. data/app/views/mokio/backend/common/new.html.haml +0 -6
  131. data/app/views/mokio/backend/contacts/_form.html.haml +0 -42
  132. data/app/views/mokio/backend/contents/_content_info_widget.html.haml +0 -22
  133. data/app/views/mokio/backend/dashboard/_content.html.haml +0 -9
  134. data/app/views/mokio/backend/dashboard/show.html.haml +0 -35
  135. data/app/views/mokio/backend/dashboard/tbl_content.html.haml +0 -24
  136. data/app/views/mokio/backend/data_files/_file_upload.html.erb +0 -71
  137. data/app/views/mokio/backend/data_files/uploader.html.haml +0 -15
  138. data/app/views/mokio/backend/errors/404.html.haml +0 -45
  139. data/app/views/mokio/backend/errors/unauthorized.html.haml +0 -11
  140. data/app/views/mokio/backend/layout/_user_widget.html.haml +0 -8
  141. data/app/views/mokio/backend/layout/header.html.haml +0 -14
  142. data/app/views/mokio/backend/layout/sidebar.html.haml +0 -59
  143. data/app/views/mokio/backend/menus/_form.html.haml +0 -66
  144. data/app/views/mokio/backend/menus/_menu.html.haml +0 -24
  145. data/app/views/mokio/backend/menus/index.html.haml +0 -53
  146. data/app/views/mokio/backend/menus/index.json.jbuilder +0 -4
  147. data/app/views/mokio/backend/menus/lang_changed.js.haml +0 -6
  148. data/app/views/mokio/backend/menus/sort.html.haml +0 -1
  149. data/app/views/mokio/backend/menus/update_menu_breadcrumps.js.haml +0 -1
  150. data/app/views/mokio/backend/mov_galleries/_form.html.haml +0 -17
  151. data/app/views/mokio/backend/mov_galleries/_form_inputs.html.haml +0 -8
  152. data/app/views/mokio/backend/photos/_gallery.html.haml +0 -25
  153. data/app/views/mokio/backend/photos/create.js.haml +0 -1
  154. data/app/views/mokio/backend/photos/crop_photo.js.haml +0 -10
  155. data/app/views/mokio/backend/photos/crop_thumb.js.haml +0 -11
  156. data/app/views/mokio/backend/photos/destroy.js.haml +0 -1
  157. data/app/views/mokio/backend/photos/from_external_link.html.haml +0 -19
  158. data/app/views/mokio/backend/photos/get_photo.js.haml +0 -1
  159. data/app/views/mokio/backend/photos/get_thumb.js.haml +0 -1
  160. data/app/views/mokio/backend/photos/photo.html.haml +0 -54
  161. data/app/views/mokio/backend/photos/remove_thumb.js.haml +0 -10
  162. data/app/views/mokio/backend/photos/rotate_photo.js.haml +0 -10
  163. data/app/views/mokio/backend/photos/rotate_thumb.js.haml +0 -11
  164. data/app/views/mokio/backend/photos/thumb.html.haml +0 -9
  165. data/app/views/mokio/backend/photos/update_thumb.js.haml +0 -11
  166. data/app/views/mokio/backend/photos/upload_external_links.js.haml +0 -4
  167. data/app/views/mokio/backend/pic_galleries/_form.html.haml +0 -16
  168. data/app/views/mokio/backend/pic_galleries/_form_inputs.html.haml +0 -9
  169. data/app/views/mokio/backend/site_helper/first_login.html.haml +0 -14
  170. data/app/views/mokio/backend/static_modules/_form.html.haml +0 -9
  171. data/app/views/mokio/backend/users/_form.html.haml +0 -11
  172. data/app/views/mokio/backend/users/edit_password.html.haml +0 -13
  173. data/app/views/mokio/backend/youtubes/_edit.html.haml +0 -49
  174. data/app/views/mokio/backend/youtubes/_gallery.html.haml +0 -24
  175. data/app/views/mokio/backend/youtubes/_new.html.haml +0 -4
  176. data/app/views/mokio/backend/youtubes/_preview_movie.html.haml +0 -1
  177. data/app/views/mokio/backend/youtubes/_search_yt.html.haml +0 -8
  178. data/app/views/mokio/backend/youtubes/_search_yt_body.html.haml +0 -11
  179. data/app/views/mokio/backend/youtubes/find.js.haml +0 -1
  180. data/app/views/mokio/backend/youtubes/preview.html.haml +0 -36
  181. data/app/views/mokio/backend/youtubes/preview_movie.js.haml +0 -5
  182. data/app/views/mokio/backend/youtubes/thumb.html.haml +0 -14
  183. data/app/views/mokio/backend/youtubes/uploader.html.haml +0 -10
  184. data/app/views/mokio/backend/youtubes/youtube.html.haml +0 -12
  185. data/app/views/mokio/base_articles/_multi_lang_form.html.slim +0 -18
  186. data/app/views/mokio/base_contacts/_multi_lang_form.html.slim +0 -48
  187. data/app/views/mokio/base_contents/update_content_form.js.slim +0 -1
  188. data/app/views/mokio/base_pic_galleries/_multi_lang_form.html.slim +0 -13
  189. data/app/views/mokio/common/_meta.html.slim +0 -50
  190. data/app/views/mokio/common/_multi_lang_gmap.html.slim +0 -41
  191. data/app/views/mokio/common/_multi_lang_meta.html.slim +0 -50
  192. data/app/views/mokio/common/multi_lang_form_content.html.slim +0 -56
  193. data/db/migrate/20140731061227_create_mokio_external_scripts.rb +0 -12
  194. data/db/migrate/20141009125027_alter_external_scripts.rb +0 -13
  195. data/db/migrate/20141024131942_migrate_external_codes_to_external_scripts.rb +0 -25
  196. data/db/migrate/20141209072051_change_data_type_for_display_dates.rb +0 -8
  197. data/db/migrate/20141209101853_add_menu_css_options.rb +0 -6
  198. data/db/migrate/20150220071158_add_subtitle_to_contents.rb +0 -7
  199. data/db/migrate/20150220091326_add_main_pic_to_mokio_menu.rb +0 -5
  200. data/db/migrate/20150408074951_create_mokio_base_contents.rb +0 -17
  201. data/db/migrate/20150408075454_add_base_content_id_to_mokio_contents.mokio.rb +0 -6
  202. data/db/migrate/20150414054139_add_name_to_mokio_users.rb +0 -6
  203. data/db/migrate/20150414070406_add_author_and_editor_to_mokio_content.rb +0 -6
  204. data/db/migrate/20150528104851_add_base_content_id_to_mokio_data_files.rb +0 -5
  205. data/lib/mokio/concerns/controllers/base_articles.rb +0 -31
  206. data/lib/mokio/concerns/controllers/base_contacts.rb +0 -32
  207. data/lib/mokio/concerns/controllers/base_contents.rb +0 -167
  208. data/lib/mokio/concerns/controllers/base_mov_galleries.rb +0 -48
  209. data/lib/mokio/concerns/controllers/base_pic_galleries.rb +0 -32
  210. data/lib/mokio/concerns/models/base_article.rb +0 -21
  211. data/lib/mokio/concerns/models/base_contact.rb +0 -36
  212. data/lib/mokio/concerns/models/base_content.rb +0 -51
  213. data/lib/mokio/concerns/models/base_mov_gallery.rb +0 -19
  214. data/lib/mokio/concerns/models/base_pic_gallery.rb +0 -22
  215. data/lib/mokio/concerns/models/meta.rb +0 -39
  216. data/spec/factories/meta.rb +0 -37
  217. data/spec/models/meta_spec.rb +0 -26
@@ -1,47 +0,0 @@
1
- = Mokio
2
-
3
- == Licence
4
-
5
- GNU AGPLv3: http://www.gnu.org/licenses/agpl-3.0.html
6
-
7
- == Description
8
-
9
- Mokio is a Content Management System that allows creation of
10
- sophisticated websites. It consists maily of administration panel for your
11
- desired website.
12
- It provides the following types of content: Article (piece of text with
13
- pictures, lists, links, etc.), Picture Gallery (easily managed article with
14
- a number of photos - thumbs and edition provided), Movie Gallery
15
- (article with links to Dailymotion, Vimeo and Youtube movies - thumbs
16
- and edition provided), Contact Page with Google map and contact form.
17
-
18
- Apart from content it is possible to manage menu structure of your
19
- website for each language and defined position (part of the screen).
20
-
21
- If this is not enough for what you need, you can also include pieces of
22
- HTML code that should be shared by some (or all) subpages - called
23
- HTML Blocks. You can define on which part of the page, these blocks
24
- should be displayed.
25
- Mokio provides also administration panel for javascripts, languages
26
- and backend users.
27
-
28
- Beside Mokio itself, there are various gems dedicated for Mokio, that
29
- extends its core functionality.
30
-
31
- == Sample app using Mokio CMS
32
-
33
- See https://github.com/versoft/mokio_sample_app
34
-
35
- == Gems dedicated for Mokio CMS
36
-
37
- mokio_skins https://github.com/versoft/mokio_skins
38
-
39
- == Tutorials, documentation & useful tips
40
-
41
- See http://www.mokio.org/support
42
-
43
- == Known issues
44
-
45
- To use Mokio::MovGallery you have to add this to your Gemfile:
46
- gem 'youtube_it', github: 'LiveWorld/youtube_it'
47
- because original version of `youtube_it` gem is not maintained anymore and uncompatibile with Rails 5.
@@ -1,2195 +0,0 @@
1
- /*
2
- Copyright 2012 Igor Vaynberg
3
-
4
- Version: 3.0 Timestamp: Tue Jul 31 21:09:16 PDT 2012
5
-
6
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in
7
- compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
8
-
9
- http://www.apache.org/licenses/LICENSE-2.0
10
-
11
- Unless required by applicable law or agreed to in writing, software distributed under the License is
12
- distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and limitations under the License.
14
- */
15
- (function ($) {
16
- if(typeof $.fn.each2 == "undefined"){
17
- $.fn.extend({
18
- /*
19
- * 4-10 times faster .each replacement
20
- * use it carefully, as it overrides jQuery context of element on each iteration
21
- */
22
- each2 : function (c) {
23
- var j = $([0]), i = -1, l = this.length;
24
- while (
25
- ++i < l
26
- && (j.context = j[0] = this[i])
27
- && c.call(j[0], i, j) !== false //"this"=DOM, i=index, j=jQuery object
28
- );
29
- return this;
30
- }
31
- });
32
- }
33
- })(jQuery);
34
-
35
- (function ($, undefined) {
36
- "use strict";
37
- /*global document, window, jQuery, console */
38
-
39
- if (window.Select2 !== undefined) {
40
- return;
41
- }
42
-
43
- var KEY, AbstractSelect2, SingleSelect2, MultiSelect2, nextUid, sizer;
44
-
45
- KEY = {
46
- TAB: 9,
47
- ENTER: 13,
48
- ESC: 27,
49
- SPACE: 32,
50
- LEFT: 37,
51
- UP: 38,
52
- RIGHT: 39,
53
- DOWN: 40,
54
- SHIFT: 16,
55
- CTRL: 17,
56
- ALT: 18,
57
- PAGE_UP: 33,
58
- PAGE_DOWN: 34,
59
- HOME: 36,
60
- END: 35,
61
- BACKSPACE: 8,
62
- DELETE: 46,
63
- isArrow: function (k) {
64
- k = k.which ? k.which : k;
65
- switch (k) {
66
- case KEY.LEFT:
67
- case KEY.RIGHT:
68
- case KEY.UP:
69
- case KEY.DOWN:
70
- return true;
71
- }
72
- return false;
73
- },
74
- isControl: function (k) {
75
- k = k.which ? k.which : k;
76
- switch (k) {
77
- case KEY.SHIFT:
78
- case KEY.CTRL:
79
- case KEY.ALT:
80
- return true;
81
- }
82
-
83
- if (k.metaKey) return true;
84
-
85
- return false;
86
- },
87
- isFunctionKey: function (k) {
88
- k = k.which ? k.which : k;
89
- return k >= 112 && k <= 123;
90
- }
91
- };
92
-
93
- nextUid=(function() { var counter=1; return function() { return counter++; }; }());
94
-
95
- function escapeMarkup(markup) {
96
- if (markup && typeof(markup) === "string") {
97
- return markup.replace("&", "&amp;");
98
- } else {
99
- return markup;
100
- }
101
- }
102
-
103
- function indexOf(value, array) {
104
- var i = 0, l = array.length, v;
105
-
106
- if (typeof value === "undefined") {
107
- return -1;
108
- }
109
-
110
- if (value.constructor === String) {
111
- for (; i < l; i = i + 1) if (value.localeCompare(array[i]) === 0) return i;
112
- } else {
113
- for (; i < l; i = i + 1) {
114
- v = array[i];
115
- if (v.constructor === String) {
116
- if (v.localeCompare(value) === 0) return i;
117
- } else {
118
- if (v === value) return i;
119
- }
120
- }
121
- }
122
- return -1;
123
- }
124
-
125
- /**
126
- * Compares equality of a and b taking into account that a and b may be strings, in which case localeCompare is used
127
- * @param a
128
- * @param b
129
- */
130
- function equal(a, b) {
131
- if (a === b) return true;
132
- if (a === undefined || b === undefined) return false;
133
- if (a === null || b === null) return false;
134
- if (a.constructor === String) return a.localeCompare(b) === 0;
135
- if (b.constructor === String) return b.localeCompare(a) === 0;
136
- return false;
137
- }
138
-
139
- /**
140
- * Splits the string into an array of values, trimming each value. An empty array is returned for nulls or empty
141
- * strings
142
- * @param string
143
- * @param separator
144
- */
145
- function splitVal(string, separator) {
146
- var val, i, l;
147
- if (string === null || string.length < 1) return [];
148
- val = string.split(separator);
149
- for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
150
- return val;
151
- }
152
-
153
- function getSideBorderPadding(element) {
154
- return element.outerWidth() - element.width();
155
- }
156
-
157
- function installKeyUpChangeEvent(element) {
158
- var key="keyup-change-value";
159
- element.bind("keydown", function () {
160
- if ($.data(element, key) === undefined) {
161
- $.data(element, key, element.val());
162
- }
163
- });
164
- element.bind("keyup", function () {
165
- var val= $.data(element, key);
166
- if (val !== undefined && element.val() !== val) {
167
- $.removeData(element, key);
168
- element.trigger("keyup-change");
169
- }
170
- });
171
- }
172
-
173
- $(document).delegate("*", "mousemove", function (e) {
174
- $.data(document, "select2-lastpos", {x: e.pageX, y: e.pageY});
175
- });
176
-
177
- /**
178
- * filters mouse events so an event is fired only if the mouse moved.
179
- *
180
- * filters out mouse events that occur when mouse is stationary but
181
- * the elements under the pointer are scrolled.
182
- */
183
- function installFilteredMouseMove(element) {
184
- element.bind("mousemove", function (e) {
185
- var lastpos = $.data(document, "select2-lastpos");
186
- if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) {
187
- $(e.target).trigger("mousemove-filtered", e);
188
- }
189
- });
190
- }
191
-
192
- /**
193
- * Debounces a function. Returns a function that calls the original fn function only if no invocations have been made
194
- * within the last quietMillis milliseconds.
195
- *
196
- * @param quietMillis number of milliseconds to wait before invoking fn
197
- * @param fn function to be debounced
198
- * @return debounced version of fn
199
- */
200
- function debounce(quietMillis, fn) {
201
- var timeout;
202
- return function () {
203
- window.clearTimeout(timeout);
204
- timeout = window.setTimeout(fn, quietMillis);
205
- };
206
- }
207
-
208
- /**
209
- * A simple implementation of a thunk
210
- * @param formula function used to lazily initialize the thunk
211
- * @return {Function}
212
- */
213
- function thunk(formula) {
214
- var evaluated = false,
215
- value;
216
- return function() {
217
- if (evaluated === false) { value = formula(); evaluated = true; }
218
- return value;
219
- };
220
- };
221
-
222
- function installDebouncedScroll(threshold, element) {
223
- var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);});
224
- element.bind("scroll", function (e) {
225
- if (indexOf(e.target, element.get()) >= 0) notify(e);
226
- });
227
- }
228
-
229
- function killEvent(event) {
230
- event.preventDefault();
231
- event.stopPropagation();
232
- }
233
-
234
- function measureTextWidth(e) {
235
- if (!sizer){
236
- var style = e[0].currentStyle || window.getComputedStyle(e[0], null);
237
- sizer = $("<div></div>").css({
238
- position: "absolute",
239
- left: "-10000px",
240
- top: "-10000px",
241
- display: "none",
242
- fontSize: style.fontSize,
243
- fontFamily: style.fontFamily,
244
- fontStyle: style.fontStyle,
245
- fontWeight: style.fontWeight,
246
- letterSpacing: style.letterSpacing,
247
- textTransform: style.textTransform,
248
- whiteSpace: "nowrap"
249
- });
250
- $("body").append(sizer);
251
- }
252
- sizer.text(e.val());
253
- return sizer.width();
254
- }
255
-
256
- function markMatch(text, term, markup) {
257
- var match=text.toUpperCase().indexOf(term.toUpperCase()),
258
- tl=term.length;
259
-
260
- if (match<0) {
261
- markup.push(text);
262
- return;
263
- }
264
-
265
- markup.push(text.substring(0, match));
266
- markup.push("<span class='select2-match'>");
267
- markup.push(text.substring(match, match + tl));
268
- markup.push("</span>");
269
- markup.push(text.substring(match + tl, text.length));
270
- }
271
-
272
- /**
273
- * Produces an ajax-based query function
274
- *
275
- * @param options object containing configuration paramters
276
- * @param options.transport function that will be used to execute the ajax request. must be compatible with parameters supported by $.ajax
277
- * @param options.url url for the data
278
- * @param options.data a function(searchTerm, pageNumber, context) that should return an object containing query string parameters for the above url.
279
- * @param options.dataType request data type: ajax, jsonp, other datatatypes supported by jQuery's $.ajax function or the transport function if specified
280
- * @param options.quietMillis (optional) milliseconds to wait before making the ajaxRequest, helps debounce the ajax function if invoked too often
281
- * @param options.results a function(remoteData, pageNumber) that converts data returned form the remote request to the format expected by Select2.
282
- * The expected format is an object containing the following keys:
283
- * results array of objects that will be used as choices
284
- * more (optional) boolean indicating whether there are more results available
285
- * Example: {results:[{id:1, text:'Red'},{id:2, text:'Blue'}], more:true}
286
- */
287
- function ajax(options) {
288
- var timeout, // current scheduled but not yet executed request
289
- requestSequence = 0, // sequence used to drop out-of-order responses
290
- handler = null,
291
- quietMillis = options.quietMillis || 100;
292
-
293
- return function (query) {
294
- window.clearTimeout(timeout);
295
- timeout = window.setTimeout(function () {
296
- requestSequence += 1; // increment the sequence
297
- var requestNumber = requestSequence, // this request's sequence number
298
- data = options.data, // ajax data function
299
- transport = options.transport || $.ajax,
300
- type = options.type || 'GET'; // set type of request (GET or POST)
301
-
302
- data = data.call(this, query.term, query.page, query.context);
303
-
304
- if( null !== handler) { handler.abort(); }
305
-
306
- handler = transport.call(null, {
307
- url: options.url,
308
- dataType: options.dataType,
309
- data: data,
310
- type: type,
311
- success: function (data) {
312
- if (requestNumber < requestSequence) {
313
- return;
314
- }
315
- // TODO 3.0 - replace query.page with query so users have access to term, page, etc.
316
- var results = options.results(data, query.page);
317
- query.callback(results);
318
- }
319
- });
320
- }, quietMillis);
321
- };
322
- }
323
-
324
- /**
325
- * Produces a query function that works with a local array
326
- *
327
- * @param options object containing configuration parameters. The options parameter can either be an array or an
328
- * object.
329
- *
330
- * If the array form is used it is assumed that it contains objects with 'id' and 'text' keys.
331
- *
332
- * If the object form is used ti is assumed that it contains 'data' and 'text' keys. The 'data' key should contain
333
- * an array of objects that will be used as choices. These objects must contain at least an 'id' key. The 'text'
334
- * key can either be a String in which case it is expected that each element in the 'data' array has a key with the
335
- * value of 'text' which will be used to match choices. Alternatively, text can be a function(item) that can extract
336
- * the text.
337
- */
338
- function local(options) {
339
- var data = options, // data elements
340
- dataText,
341
- text = function (item) { return ""+item.text; }; // function used to retrieve the text portion of a data item that is matched against the search
342
-
343
- if (!$.isArray(data)) {
344
- text = data.text;
345
- // if text is not a function we assume it to be a key name
346
- if (!$.isFunction(text)) {
347
- dataText = data.text; // we need to store this in a separate variable because in the next step data gets reset and data.text is no longer available
348
- text = function (item) { return item[dataText]; };
349
- }
350
- data = data.results;
351
- }
352
-
353
- return function (query) {
354
- var t = query.term, filtered = {};
355
- if (t === "") {
356
- query.callback({results: data});
357
- return;
358
- }
359
- filtered.results = $(data)
360
- .filter(function () {return query.matcher(t, text(this));})
361
- .get();
362
- query.callback(filtered);
363
- };
364
- }
365
-
366
- // TODO javadoc
367
- function tags(data) {
368
- // TODO even for a function we should probably return a wrapper that does the same object/string check as
369
- // the function for arrays. otherwise only functions that return objects are supported.
370
- if ($.isFunction(data)) {
371
- return data;
372
- }
373
-
374
- // if not a function we assume it to be an array
375
-
376
- return function (query) {
377
- var t = query.term, filtered = {results: []};
378
- $(data).each(function () {
379
- var isObject = this.text !== undefined,
380
- text = isObject ? this.text : this;
381
- if (t === "" || query.matcher(t, text)) {
382
- filtered.results.push(isObject ? this : {id: this, text: this});
383
- }
384
- });
385
- query.callback(filtered);
386
- };
387
- }
388
-
389
- /**
390
- * Checks if the formatter function should be used.
391
- *
392
- * Throws an error if it is not a function. Returns true if it should be used,
393
- * false if no formatting should be performed.
394
- *
395
- * @param formatter
396
- */
397
- function checkFormatter(formatter, formatterName) {
398
- if ($.isFunction(formatter)) return true;
399
- if (!formatter) return fasle;
400
- throw new Error("formatterName must be a function or a falsy value");
401
- }
402
-
403
- /**
404
- * blurs any Select2 container that has focus when an element outside them was clicked or received focus
405
- *
406
- * also takes care of clicks on label tags that point to the source element
407
- */
408
- $(document).ready(function () {
409
- $(document).delegate("*", "mousedown touchend", function (e) {
410
- var target = $(e.target).closest("div.select2-container").get(0), attr;
411
- if (target) {
412
- $(document).find("div.select2-container-active").each(function () {
413
- if (this !== target) $(this).data("select2").blur();
414
- });
415
- } else {
416
- target = $(e.target).closest("div.select2-drop").get(0);
417
- $(document).find("div.select2-drop-active").each(function () {
418
- if (this !== target) $(this).data("select2").blur();
419
- });
420
- }
421
-
422
- target=$(e.target);
423
- attr = target.attr("for");
424
- if ("LABEL" === e.target.tagName && attr && attr.length > 0) {
425
- target = $("#"+attr);
426
- target = target.data("select2");
427
- if (target !== undefined) { target.focus(); e.preventDefault();}
428
- }
429
- });
430
- });
431
-
432
- function evaluate(val) {
433
- return $.isFunction(val) ? val() : val;
434
- }
435
-
436
- /**
437
- * Creates a new class
438
- *
439
- * @param superClass
440
- * @param methods
441
- */
442
- function clazz(SuperClass, methods) {
443
- var constructor = function () {};
444
- constructor.prototype = new SuperClass;
445
- constructor.prototype.constructor = constructor;
446
- constructor.prototype.parent = SuperClass.prototype;
447
- constructor.prototype = $.extend(constructor.prototype, methods);
448
- return constructor;
449
- }
450
-
451
- AbstractSelect2 = clazz(Object, {
452
-
453
- // abstract
454
- bind: function (func) {
455
- var self = this;
456
- return function () {
457
- func.apply(self, arguments);
458
- };
459
- },
460
-
461
- // abstract
462
- init: function (opts) {
463
- var results, search, resultsSelector = ".select2-results";
464
-
465
- // prepare options
466
- this.opts = opts = this.prepareOpts(opts);
467
-
468
- this.id=opts.id;
469
-
470
- // destroy if called on an existing component
471
- if (opts.element.data("select2") !== undefined &&
472
- opts.element.data("select2") !== null) {
473
- this.destroy();
474
- }
475
-
476
- this.enabled=true;
477
- this.container = this.createContainer();
478
-
479
- // cache the body so future lookups are cheap
480
- this.body = thunk(function() { return opts.element.closest("body"); });
481
-
482
- if (opts.element.attr("class") !== undefined) {
483
- this.container.addClass(opts.element.attr("class"));
484
- }
485
-
486
- this.container.css(evaluate(opts.containerCss));
487
- this.container.addClass(evaluate(opts.containerCssClass));
488
-
489
- // swap container for the element
490
- this.opts.element
491
- .data("select2", this)
492
- .hide()
493
- .after(this.container);
494
- this.container.data("select2", this);
495
-
496
- this.dropdown = this.container.find(".select2-drop");
497
- this.dropdown.css(evaluate(opts.dropdownCss));
498
- this.dropdown.addClass(evaluate(opts.dropdownCssClass));
499
- this.dropdown.data("select2", this);
500
-
501
- this.results = results = this.container.find(resultsSelector);
502
- this.search = search = this.container.find("input.select2-input");
503
-
504
- search.attr("tabIndex", this.opts.element.attr("tabIndex"));
505
-
506
- this.resultsPage = 0;
507
- this.context = null;
508
-
509
- // initialize the container
510
- this.initContainer();
511
- this.initContainerWidth();
512
-
513
- installFilteredMouseMove(this.results);
514
- this.dropdown.delegate(resultsSelector, "mousemove-filtered", this.bind(this.highlightUnderEvent));
515
-
516
- installDebouncedScroll(80, this.results);
517
- this.dropdown.delegate(resultsSelector, "scroll-debounced", this.bind(this.loadMoreIfNeeded));
518
-
519
- // if jquery.mousewheel plugin is installed we can prevent out-of-bounds scrolling of results via mousewheel
520
- if ($.fn.mousewheel) {
521
- results.mousewheel(function (e, delta, deltaX, deltaY) {
522
- var top = results.scrollTop(), height;
523
- if (deltaY > 0 && top - deltaY <= 0) {
524
- results.scrollTop(0);
525
- killEvent(e);
526
- } else if (deltaY < 0 && results.get(0).scrollHeight - results.scrollTop() + deltaY <= results.height()) {
527
- results.scrollTop(results.get(0).scrollHeight - results.height());
528
- killEvent(e);
529
- }
530
- });
531
- }
532
-
533
- installKeyUpChangeEvent(search);
534
- search.bind("keyup-change", this.bind(this.updateResults));
535
- search.bind("focus", function () { search.addClass("select2-focused"); if (search.val() === " ") search.val(""); });
536
- search.bind("blur", function () { search.removeClass("select2-focused");});
537
-
538
- this.dropdown.delegate(resultsSelector, "mouseup", this.bind(function (e) {
539
- if ($(e.target).closest(".select2-result-selectable:not(.select2-disabled)").length > 0) {
540
- this.highlightUnderEvent(e);
541
- this.selectHighlighted(e);
542
- } else {
543
- this.focusSearch();
544
- }
545
- killEvent(e);
546
- }));
547
-
548
- // trap all mouse events from leaving the dropdown. sometimes there may be a modal that is listening
549
- // for mouse events outside of itself so it can close itself. since the dropdown is now outside the select2's
550
- // dom it will trigger the popup close, which is not what we want
551
- this.dropdown.bind("click mouseup mousedown", function (e) { e.stopPropagation(); });
552
-
553
- if ($.isFunction(this.opts.initSelection)) {
554
- // initialize selection based on the current value of the source element
555
- this.initSelection();
556
-
557
- // if the user has provided a function that can set selection based on the value of the source element
558
- // we monitor the change event on the element and trigger it, allowing for two way synchronization
559
- this.monitorSource();
560
- }
561
-
562
- if (opts.element.is(":disabled")) this.disable();
563
- },
564
-
565
- // abstract
566
- destroy: function () {
567
- var select2 = this.opts.element.data("select2");
568
- if (select2 !== undefined) {
569
- select2.container.remove();
570
- select2.dropdown.remove();
571
- select2.opts.element
572
- .removeData("select2")
573
- .unbind(".select2")
574
- .show();
575
- }
576
- },
577
-
578
- // abstract
579
- prepareOpts: function (opts) {
580
- var element, select, idKey, ajaxUrl;
581
-
582
- element = opts.element;
583
-
584
- if (element.get(0).tagName.toLowerCase() === "select") {
585
- this.select = select = opts.element;
586
- }
587
-
588
- //Custom tags separator.
589
- opts.separator = opts.separator || ",";
590
-
591
- if (select) {
592
- // these options are not allowed when attached to a select because they are picked up off the element itself
593
- $.each(["id", "multiple", "ajax", "query", "createSearchChoice", "initSelection", "data", "tags"], function () {
594
- if (this in opts) {
595
- throw new Error("Option '" + this + "' is not allowed for Select2 when attached to a <select> element.");
596
- }
597
- });
598
- }
599
-
600
- opts = $.extend({}, {
601
- populateResults: function(container, results, query) {
602
- var populate, data, result, children, id=this.opts.id;
603
-
604
- populate=function(results, container, depth) {
605
-
606
- var i, l, result, selectable, compound, node, label, innerContainer, formatted;
607
- for (i = 0, l = results.length; i < l; i = i + 1) {
608
-
609
- result=results[i];
610
- selectable=id(result) !== undefined;
611
- compound=("children" in result) && result.children.length > 0;
612
-
613
- node=$("<li></li>");
614
- node.addClass("select2-results-dept-"+depth);
615
- node.addClass("select2-result");
616
- node.addClass(selectable ? "select2-result-selectable" : "select2-result-unselectable");
617
- if (compound) { node.addClass("select2-result-with-children"); }
618
-
619
- label=$("<div></div>");
620
- label.addClass("select2-result-label");
621
-
622
- formatted=opts.formatResult(result, label, query);
623
- if (formatted!==undefined) {
624
- label.html(escapeMarkup(formatted));
625
- }
626
-
627
- node.append(label);
628
-
629
- if (compound) {
630
-
631
- innerContainer=$("<ul></ul>");
632
- innerContainer.addClass("select2-result-sub");
633
- populate(result.children, innerContainer, depth+1);
634
- node.append(innerContainer);
635
- }
636
-
637
- node.data("select2-data", result);
638
- container.append(node);
639
- }
640
- };
641
-
642
- populate(results, container, 0);
643
- }
644
- }, $.fn.select2.defaults, opts);
645
-
646
- if (typeof(opts.id) !== "function") {
647
- idKey = opts.id;
648
- opts.id = function (e) { return e[idKey]; };
649
- }
650
-
651
- if (select) {
652
- opts.query = this.bind(function (query) {
653
- var data = { results: [], more: false },
654
- term = query.term,
655
- children, firstChild, process;
656
-
657
- process=function(element, collection) {
658
- var group;
659
- if (element.is("option")) {
660
- if (query.matcher(term, element.text(), element)) {
661
- collection.push({id:element.attr("value"), text:element.text(), element: element.get()});
662
- }
663
- } else if (element.is("optgroup")) {
664
- group={text:element.attr("label"), children:[], element: element.get()};
665
- element.children().each2(function(i, elm) { process(elm, group.children); });
666
- if (group.children.length>0) {
667
- collection.push(group);
668
- }
669
- }
670
- };
671
-
672
- children=element.children();
673
-
674
- // ignore the placeholder option if there is one
675
- if (this.getPlaceholder() !== undefined && children.length > 0) {
676
- firstChild = children[0];
677
- if ($(firstChild).text() === "") {
678
- children=children.not(firstChild);
679
- }
680
- }
681
-
682
- children.each2(function(i, elm) { process(elm, data.results); });
683
-
684
- query.callback(data);
685
- });
686
- // this is needed because inside val() we construct choices from options and there id is hardcoded
687
- opts.id=function(e) { return e.id; };
688
- } else {
689
- if (!("query" in opts)) {
690
- if ("ajax" in opts) {
691
- ajaxUrl = opts.element.data("ajax-url");
692
- if (ajaxUrl && ajaxUrl.length > 0) {
693
- opts.ajax.url = ajaxUrl;
694
- }
695
- opts.query = ajax(opts.ajax);
696
- } else if ("data" in opts) {
697
- opts.query = local(opts.data);
698
- } else if ("tags" in opts) {
699
- opts.query = tags(opts.tags);
700
- opts.createSearchChoice = function (term) { return {id: term, text: term}; };
701
- opts.initSelection = function (element, callback) {
702
- var data = [];
703
- $(splitVal(element.val(), opts.separator)).each(function () {
704
- var id = this, text = this, tags=opts.tags;
705
- if ($.isFunction(tags)) tags=tags();
706
- $(tags).each(function() { if (equal(this.id, id)) { text = this.text; return false; } });
707
- data.push({id: id, text: text});
708
- });
709
-
710
- callback(data);
711
- };
712
- }
713
- }
714
- }
715
- if (typeof(opts.query) !== "function") {
716
- throw "query function not defined for Select2 " + opts.element.attr("id");
717
- }
718
-
719
- return opts;
720
- },
721
-
722
- /**
723
- * Monitor the original element for changes and update select2 accordingly
724
- */
725
- // abstract
726
- monitorSource: function () {
727
- this.opts.element.bind("change.select2", this.bind(function (e) {
728
- if (this.opts.element.data("select2-change-triggered") !== true) {
729
- this.initSelection();
730
- }
731
- }));
732
- },
733
-
734
- /**
735
- * Triggers the change event on the source element
736
- */
737
- // abstract
738
- triggerChange: function (details) {
739
-
740
- details = details || {};
741
- details= $.extend({}, details, { type: "change", val: this.val() });
742
- // prevents recursive triggering
743
- this.opts.element.data("select2-change-triggered", true);
744
- this.opts.element.trigger(details);
745
- this.opts.element.data("select2-change-triggered", false);
746
-
747
- // some validation frameworks ignore the change event and listen instead to keyup, click for selects
748
- // so here we trigger the click event manually
749
- this.opts.element.click();
750
- },
751
-
752
-
753
- // abstract
754
- enable: function() {
755
- if (this.enabled) return;
756
-
757
- this.enabled=true;
758
- this.container.removeClass("select2-container-disabled");
759
- },
760
-
761
- // abstract
762
- disable: function() {
763
- if (!this.enabled) return;
764
-
765
- this.close();
766
-
767
- this.enabled=false;
768
- this.container.addClass("select2-container-disabled");
769
- },
770
-
771
- // abstract
772
- opened: function () {
773
- return this.container.hasClass("select2-dropdown-open");
774
- },
775
-
776
- // abstract
777
- positionDropdown: function() {
778
- var offset = this.container.offset(),
779
- height = this.container.outerHeight(),
780
- width = this.container.outerWidth(),
781
- dropHeight = this.dropdown.outerHeight(),
782
- viewportBottom = $(window).scrollTop() + document.documentElement.clientHeight,
783
- dropTop = offset.top + height,
784
- enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
785
- enoughRoomAbove = (offset.top - dropHeight) >= this.body().scrollTop(),
786
- aboveNow = this.dropdown.hasClass("select2-drop-above"),
787
- above,
788
- css;
789
-
790
- // console.log("below/ droptop:", dropTop, "dropHeight", dropHeight, "sum", (dropTop+dropHeight)+" viewport bottom", viewportBottom, "enough?", enoughRoomBelow);
791
- // console.log("above/ offset.top", offset.top, "dropHeight", dropHeight, "top", (offset.top-dropHeight), "scrollTop", this.body().scrollTop(), "enough?", enoughRoomAbove);
792
-
793
- // always prefer the current above/below alignment, unless there is not enough room
794
-
795
- if (aboveNow) {
796
- above = true;
797
- if (!enoughRoomAbove && enoughRoomBelow) above = false;
798
- } else {
799
- above = false;
800
- if (!enoughRoomBelow && enoughRoomAbove) above = true;
801
- }
802
-
803
- if (above) {
804
- dropTop = offset.top - dropHeight;
805
- this.container.addClass("select2-drop-above");
806
- this.dropdown.addClass("select2-drop-above");
807
- }
808
- else {
809
- this.container.removeClass("select2-drop-above");
810
- this.dropdown.removeClass("select2-drop-above");
811
- }
812
-
813
- css = {
814
- top:dropTop,
815
- left:offset.left,
816
- width:width
817
- };
818
-
819
- this.dropdown.css(css);
820
- },
821
-
822
- // abstract
823
- shouldOpen: function() {
824
- var event;
825
-
826
- if (this.opened()) return false;
827
-
828
- event = jQuery.Event("open");
829
- this.opts.element.trigger(event);
830
- return !event.isDefaultPrevented();
831
- },
832
-
833
- // abstract
834
- clearDropdownAlignmentPreference: function() {
835
- // clear the classes used to figure out the preference of where the dropdown should be opened
836
- this.container.removeClass("select2-drop-above");
837
- this.dropdown.removeClass("select2-drop-above");
838
- },
839
-
840
- /**
841
- * Opens the dropdown
842
- *
843
- * @return {Boolean} whether or not dropdown was opened. This method will return false if, for example,
844
- * the dropdown is already open, or if the 'open' event listener on the element called preventDefault().
845
- */
846
- // abstract
847
- open: function () {
848
-
849
- if (!this.shouldOpen()) return false;
850
-
851
- window.setTimeout(this.bind(this.opening), 1);
852
-
853
- return true;
854
- },
855
-
856
- /**
857
- * Performs the opening of the dropdown
858
- */
859
- // abstract
860
- opening: function() {
861
- this.clearDropdownAlignmentPreference();
862
-
863
- if (this.search.val() === " ") { this.search.val(""); }
864
-
865
- this.dropdown.addClass("select2-drop-active");
866
- this.container.addClass("select2-dropdown-open").addClass("select2-container-active");
867
-
868
- this.updateResults(true);
869
-
870
- if(this.dropdown[0] !== this.body().children().last()[0]) {
871
- this.dropdown.detach().appendTo(this.body());
872
- }
873
-
874
- this.dropdown.show();
875
- this.ensureHighlightVisible();
876
-
877
- this.positionDropdown();
878
-
879
- this.focusSearch();
880
- },
881
-
882
- // abstract
883
- close: function () {
884
- if (!this.opened()) return;
885
-
886
- this.clearDropdownAlignmentPreference();
887
-
888
- this.dropdown.hide();
889
- this.container.removeClass("select2-dropdown-open").removeClass("select2-container-active");
890
- this.results.empty();
891
- this.clearSearch();
892
-
893
- this.opts.element.trigger(jQuery.Event("close"));
894
- },
895
-
896
- // abstract
897
- clearSearch: function () {
898
-
899
- },
900
-
901
- // abstract
902
- ensureHighlightVisible: function () {
903
- var results = this.results, children, index, child, hb, rb, y, more;
904
-
905
- index = this.highlight();
906
-
907
- if (index < 0) return;
908
-
909
- if (index == 0) {
910
-
911
- // if the first element is highlighted scroll all the way to the top,
912
- // that way any unselectable headers above it will also be scrolled
913
- // into view
914
-
915
- results.scrollTop(0);
916
- return;
917
- }
918
-
919
- children = results.find(".select2-result-selectable");
920
-
921
- child = $(children[index]);
922
-
923
- hb = child.offset().top + child.outerHeight();
924
-
925
- // if this is the last child lets also make sure select2-more-results is visible
926
- if (index === children.length - 1) {
927
- more = results.find("li.select2-more-results");
928
- if (more.length > 0) {
929
- hb = more.offset().top + more.outerHeight();
930
- }
931
- }
932
-
933
- rb = results.offset().top + results.outerHeight();
934
- if (hb > rb) {
935
- results.scrollTop(results.scrollTop() + (hb - rb));
936
- }
937
- y = child.offset().top - results.offset().top;
938
-
939
- // make sure the top of the element is visible
940
- if (y < 0) {
941
- results.scrollTop(results.scrollTop() + y); // y is negative
942
- }
943
- },
944
-
945
- // abstract
946
- moveHighlight: function (delta) {
947
- var choices = this.results.find(".select2-result-selectable"),
948
- index = this.highlight();
949
-
950
- while (index > -1 && index < choices.length) {
951
- index += delta;
952
- var choice = $(choices[index]);
953
- if (choice.hasClass("select2-result-selectable") && !choice.hasClass("select2-disabled")) {
954
- this.highlight(index);
955
- break;
956
- }
957
- }
958
- },
959
-
960
- // abstract
961
- highlight: function (index) {
962
- var choices = this.results.find(".select2-result-selectable").not(".select2-disabled");
963
-
964
- if (arguments.length === 0) {
965
- return indexOf(choices.filter(".select2-highlighted")[0], choices.get());
966
- }
967
-
968
- if (index >= choices.length) index = choices.length - 1;
969
- if (index < 0) index = 0;
970
-
971
- choices.removeClass("select2-highlighted");
972
-
973
- $(choices[index]).addClass("select2-highlighted");
974
- this.ensureHighlightVisible();
975
-
976
- },
977
-
978
- // abstract
979
- countSelectableResults: function() {
980
- return this.results.find(".select2-result-selectable").not(".select2-disabled").length;
981
- },
982
-
983
- // abstract
984
- highlightUnderEvent: function (event) {
985
- var el = $(event.target).closest(".select2-result-selectable");
986
- if (el.length > 0 && !el.is(".select2-highlighted")) {
987
- var choices = this.results.find('.select2-result-selectable');
988
- this.highlight(choices.index(el));
989
- } else if (el.length == 0) {
990
- // if we are over an unselectable item remove al highlights
991
- this.results.find(".select2-highlighted").removeClass("select2-highlighted");
992
- }
993
- },
994
-
995
- // abstract
996
- loadMoreIfNeeded: function () {
997
- var results = this.results,
998
- more = results.find("li.select2-more-results"),
999
- below, // pixels the element is below the scroll fold, below==0 is when the element is starting to be visible
1000
- offset = -1, // index of first element without data
1001
- page = this.resultsPage + 1,
1002
- self=this,
1003
- term=this.search.val(),
1004
- context=this.context;
1005
-
1006
- if (more.length === 0) return;
1007
- below = more.offset().top - results.offset().top - results.height();
1008
-
1009
- if (below <= 0) {
1010
- more.addClass("select2-active");
1011
- this.opts.query({
1012
- term: term,
1013
- page: page,
1014
- context: context,
1015
- matcher: this.opts.matcher,
1016
- callback: this.bind(function (data) {
1017
-
1018
- self.opts.populateResults.call(this, results, data.results, {term: term, page: page, context:context});
1019
-
1020
- if (data.more===true) {
1021
- more.detach().appendTo(results.children(":last")).text(self.opts.formatLoadMore(page+1));
1022
- window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
1023
- } else {
1024
- more.remove();
1025
- }
1026
- self.positionDropdown();
1027
- self.resultsPage = page;
1028
- })});
1029
- }
1030
- },
1031
-
1032
- /**
1033
- * @param initial whether or not this is the call to this method right after the dropdown has been opened
1034
- */
1035
- // abstract
1036
- updateResults: function (initial) {
1037
- var search = this.search, results = this.results, opts = this.opts, data, self=this;
1038
-
1039
- // if the search is currently hidden we do not alter the results
1040
- if (initial !== true && (this.showSearchInput === false || !this.opened())) {
1041
- return;
1042
- }
1043
-
1044
- search.addClass("select2-active");
1045
-
1046
- function postRender() {
1047
- results.scrollTop(0);
1048
- search.removeClass("select2-active");
1049
- self.positionDropdown();
1050
- }
1051
-
1052
- function render(html) {
1053
- results.html(escapeMarkup(html));
1054
- postRender();
1055
- }
1056
-
1057
- if (opts.maximumSelectionSize >=1) {
1058
- data = this.data();
1059
- if ($.isArray(data) && data.length >= opts.maximumSelectionSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) {
1060
- render("<li class='select2-selection-limit'>" + opts.formatSelectionTooBig(opts.maximumSelectionSize) + "</li>");
1061
- return;
1062
- }
1063
- }
1064
-
1065
- if (search.val().length < opts.minimumInputLength && checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) {
1066
- render("<li class='select2-no-results'>" + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "</li>");
1067
- return;
1068
- }
1069
-
1070
- this.resultsPage = 1;
1071
- opts.query({
1072
- term: search.val(),
1073
- page: this.resultsPage,
1074
- context: null,
1075
- matcher: opts.matcher,
1076
- callback: this.bind(function (data) {
1077
- var def; // default choice
1078
-
1079
- // save context, if any
1080
- this.context = (data.context===undefined) ? null : data.context;
1081
-
1082
- // create a default choice and prepend it to the list
1083
- if (this.opts.createSearchChoice && search.val() !== "") {
1084
- def = this.opts.createSearchChoice.call(null, search.val(), data.results);
1085
- if (def !== undefined && def !== null && self.id(def) !== undefined && self.id(def) !== null) {
1086
- if ($(data.results).filter(
1087
- function () {
1088
- return equal(self.id(this), self.id(def));
1089
- }).length === 0) {
1090
- data.results.unshift(def);
1091
- }
1092
- }
1093
- }
1094
-
1095
- if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) {
1096
- render("<li class='select2-no-results'>" + opts.formatNoMatches(search.val()) + "</li>");
1097
- return;
1098
- }
1099
-
1100
- results.empty();
1101
- self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null});
1102
-
1103
- if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) {
1104
- results.children().filter(":last").append("<li class='select2-more-results'>" + escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "</li>");
1105
- window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
1106
- }
1107
-
1108
- this.postprocessResults(data, initial);
1109
-
1110
- postRender();
1111
- })});
1112
- },
1113
-
1114
- // abstract
1115
- cancel: function () {
1116
- this.close();
1117
- },
1118
-
1119
- // abstract
1120
- blur: function () {
1121
- this.close();
1122
- this.container.removeClass("select2-container-active");
1123
- this.dropdown.removeClass("select2-drop-active");
1124
- // synonymous to .is(':focus'), which is available in jquery >= 1.6
1125
- if (this.search[0] === document.activeElement) { this.search.blur(); }
1126
- this.clearSearch();
1127
- this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
1128
- },
1129
-
1130
- // abstract
1131
- focusSearch: function () {
1132
- /* we do this in a timeout so that current event processing can complete before this code is executed.
1133
- this makes sure the search field is focussed even if the current event would blur it */
1134
- window.setTimeout(this.bind(function () {
1135
- this.search.focus();
1136
- // reset the value so IE places the cursor at the end of the input box
1137
- this.search.val(this.search.val());
1138
- }), 10);
1139
- },
1140
-
1141
- // abstract
1142
- selectHighlighted: function () {
1143
- var index=this.highlight(),
1144
- highlighted=this.results.find(".select2-highlighted").not(".select2-disabled"),
1145
- data = highlighted.closest('.select2-result-selectable').data("select2-data");
1146
- if (data) {
1147
- highlighted.addClass("select2-disabled");
1148
- this.highlight(index);
1149
- this.onSelect(data);
1150
- }
1151
- },
1152
-
1153
- // abstract
1154
- getPlaceholder: function () {
1155
- return this.opts.element.attr("placeholder") ||
1156
- this.opts.element.attr("data-placeholder") || // jquery 1.4 compat
1157
- this.opts.element.data("placeholder") ||
1158
- this.opts.placeholder;
1159
- },
1160
-
1161
- /**
1162
- * Get the desired width for the container element. This is
1163
- * derived first from option `width` passed to select2, then
1164
- * the inline 'style' on the original element, and finally
1165
- * falls back to the jQuery calculated element width.
1166
- */
1167
- // abstract
1168
- initContainerWidth: function () {
1169
- function resolveContainerWidth() {
1170
- var style, attrs, matches, i, l;
1171
-
1172
- if (this.opts.width === "off") {
1173
- return null;
1174
- } else if (this.opts.width === "element"){
1175
- return this.opts.element.outerWidth() === 0 ? 'auto' : this.opts.element.outerWidth() + 'px';
1176
- } else if (this.opts.width === "copy" || this.opts.width === "resolve") {
1177
- // check if there is inline style on the element that contains width
1178
- style = this.opts.element.attr('style');
1179
- if (style !== undefined) {
1180
- attrs = style.split(';');
1181
- for (i = 0, l = attrs.length; i < l; i = i + 1) {
1182
- matches = attrs[i].replace(/\s/g, '')
1183
- .match(/width:(([-+]?([0-9]*\.)?[0-9]+)(px|em|ex|%|in|cm|mm|pt|pc))/);
1184
- if (matches !== null && matches.length >= 1)
1185
- return matches[1];
1186
- }
1187
- }
1188
-
1189
- if (this.opts.width === "resolve") {
1190
- // next check if css('width') can resolve a width that is percent based, this is sometimes possible
1191
- // when attached to input type=hidden or elements hidden via css
1192
- style = this.opts.element.css('width');
1193
- if (style.indexOf("%") > 0) return style;
1194
-
1195
- // finally, fallback on the calculated width of the element
1196
- return (this.opts.element.outerWidth() === 0 ? 'auto' : this.opts.element.outerWidth() + 'px');
1197
- }
1198
-
1199
- return null;
1200
- } else if ($.isFunction(this.opts.width)) {
1201
- return this.opts.width();
1202
- } else {
1203
- return this.opts.width;
1204
- }
1205
- };
1206
-
1207
- var width = resolveContainerWidth.call(this);
1208
- if (width !== null) {
1209
- this.container.attr("style", "width: "+width);
1210
- }
1211
- }
1212
- });
1213
-
1214
- SingleSelect2 = clazz(AbstractSelect2, {
1215
-
1216
- // single
1217
-
1218
- createContainer: function () {
1219
- var container = $("<div></div>", {
1220
- "class": "select2-container"
1221
- }).html([
1222
- " <a href='javascript:void(0)' class='select2-choice'>",
1223
- " <span></span><abbr class='select2-search-choice-close' style='display:none;'></abbr>",
1224
- " <div><b></b></div>" ,
1225
- "</a>",
1226
- " <div class='select2-drop select2-offscreen'>" ,
1227
- " <div class='select2-search'>" ,
1228
- " <input type='text' autocomplete='off' class='select2-input'/>" ,
1229
- " </div>" ,
1230
- " <ul class='select2-results'>" ,
1231
- " </ul>" ,
1232
- "</div>"].join(""));
1233
- return container;
1234
- },
1235
-
1236
- // single
1237
- opening: function () {
1238
- this.search.show();
1239
- this.parent.opening.apply(this, arguments);
1240
- this.dropdown.removeClass("select2-offscreen");
1241
- },
1242
-
1243
- // single
1244
- close: function () {
1245
- if (!this.opened()) return;
1246
- this.parent.close.apply(this, arguments);
1247
- this.dropdown.removeAttr("style").addClass("select2-offscreen").insertAfter(this.selection).show();
1248
- },
1249
-
1250
- // single
1251
- focus: function () {
1252
- this.close();
1253
- this.selection.focus();
1254
- },
1255
-
1256
- // single
1257
- isFocused: function () {
1258
- return this.selection[0] === document.activeElement;
1259
- },
1260
-
1261
- // single
1262
- cancel: function () {
1263
- this.parent.cancel.apply(this, arguments);
1264
- this.selection.focus();
1265
- },
1266
-
1267
- // single
1268
- initContainer: function () {
1269
-
1270
- var selection,
1271
- container = this.container,
1272
- dropdown = this.dropdown,
1273
- clickingInside = false;
1274
-
1275
- this.selection = selection = container.find(".select2-choice");
1276
-
1277
- this.search.bind("keydown", this.bind(function (e) {
1278
- if (!this.enabled) return;
1279
-
1280
- if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
1281
- // prevent the page from scrolling
1282
- killEvent(e);
1283
- return;
1284
- }
1285
-
1286
- if (this.opened()) {
1287
- switch (e.which) {
1288
- case KEY.UP:
1289
- case KEY.DOWN:
1290
- this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
1291
- killEvent(e);
1292
- return;
1293
- case KEY.TAB:
1294
- case KEY.ENTER:
1295
- this.selectHighlighted();
1296
- killEvent(e);
1297
- return;
1298
- case KEY.ESC:
1299
- this.cancel(e);
1300
- killEvent(e);
1301
- return;
1302
- }
1303
- } else {
1304
-
1305
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
1306
- return;
1307
- }
1308
-
1309
- this.open();
1310
-
1311
- if (e.which === KEY.ENTER) {
1312
- // do not propagate the event otherwise we open, and propagate enter which closes
1313
- return;
1314
- }
1315
- }
1316
- }));
1317
-
1318
- this.search.bind("focus", this.bind(function() {
1319
- this.selection.attr("tabIndex", "-1");
1320
- }));
1321
- this.search.bind("blur", this.bind(function() {
1322
- if (!this.opened()) this.container.removeClass("select2-container-active");
1323
- window.setTimeout(this.bind(function() { this.selection.attr("tabIndex", this.opts.element.attr("tabIndex")); }), 10);
1324
- }));
1325
-
1326
- selection.bind("mousedown", this.bind(function (e) {
1327
- clickingInside = true;
1328
-
1329
- if (this.opened()) {
1330
- this.close();
1331
- this.selection.focus();
1332
- } else if (this.enabled) {
1333
- this.open();
1334
- }
1335
- killEvent(e);
1336
-
1337
- clickingInside = false;
1338
- }));
1339
-
1340
- dropdown.bind("mousedown", this.bind(function() { this.search.focus(); }));
1341
-
1342
- selection.bind("focus", this.bind(function() {
1343
- this.container.addClass("select2-container-active");
1344
- // hide the search so the tab key does not focus on it
1345
- this.search.attr("tabIndex", "-1");
1346
- }));
1347
-
1348
- selection.bind("blur", this.bind(function() {
1349
- this.container.removeClass("select2-container-active");
1350
- window.setTimeout(this.bind(function() { this.search.attr("tabIndex", this.opts.element.attr("tabIndex")); }), 10);
1351
- }));
1352
-
1353
- selection.bind("keydown", this.bind(function(e) {
1354
- if (!this.enabled) return;
1355
-
1356
- if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
1357
- // prevent the page from scrolling
1358
- killEvent(e);
1359
- return;
1360
- }
1361
-
1362
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
1363
- return;
1364
- }
1365
-
1366
- this.open();
1367
-
1368
- if (e.which === KEY.ENTER) {
1369
- // do not propagate the event otherwise we open, and propagate enter which closes
1370
- killEvent(e);
1371
- return;
1372
- }
1373
-
1374
- // do not set the search input value for non-alpha-numeric keys
1375
- // otherwise pressing down results in a '(' being set in the search field
1376
- if (e.which < 48 ) { // '0' == 48
1377
- killEvent(e);
1378
- return;
1379
- }
1380
-
1381
- var keyWritten = String.fromCharCode(e.which).toLowerCase();
1382
-
1383
- if (e.shiftKey) {
1384
- keyWritten = keyWritten.toUpperCase();
1385
- }
1386
-
1387
- this.search.val(keyWritten);
1388
-
1389
- // prevent event propagation so it doesnt replay on the now focussed search field and result in double key entry
1390
- killEvent(e);
1391
- }));
1392
-
1393
- selection.delegate("abbr", "mousedown", this.bind(function (e) {
1394
- if (!this.enabled) return;
1395
- this.clear();
1396
- killEvent(e);
1397
- this.close();
1398
- this.triggerChange();
1399
- this.selection.focus();
1400
- }));
1401
-
1402
- this.setPlaceholder();
1403
-
1404
- this.search.bind("focus", this.bind(function() {
1405
- this.container.addClass("select2-container-active");
1406
- }));
1407
- },
1408
-
1409
- // single
1410
- clear: function() {
1411
- this.opts.element.val("");
1412
- this.selection.find("span").empty();
1413
- this.selection.removeData("select2-data");
1414
- this.setPlaceholder();
1415
- },
1416
-
1417
- /**
1418
- * Sets selection based on source element's value
1419
- */
1420
- // single
1421
- initSelection: function () {
1422
- var selected;
1423
- if (this.opts.element.val() === "") {
1424
- this.close();
1425
- this.setPlaceholder();
1426
- } else {
1427
- var self = this;
1428
- this.opts.initSelection.call(null, this.opts.element, function(selected){
1429
- if (selected !== undefined && selected !== null) {
1430
- self.updateSelection(selected);
1431
- self.close();
1432
- self.setPlaceholder();
1433
- }
1434
- });
1435
- }
1436
- },
1437
-
1438
- // single
1439
- prepareOpts: function () {
1440
- var opts = this.parent.prepareOpts.apply(this, arguments);
1441
-
1442
- if (opts.element.get(0).tagName.toLowerCase() === "select") {
1443
- // install the selection initializer
1444
- opts.initSelection = function (element, callback) {
1445
- var selected = element.find(":selected");
1446
- // a single select box always has a value, no need to null check 'selected'
1447
- if ($.isFunction(callback))
1448
- callback({id: selected.attr("value"), text: selected.text()});
1449
- };
1450
- }
1451
-
1452
- return opts;
1453
- },
1454
-
1455
- // single
1456
- setPlaceholder: function () {
1457
- var placeholder = this.getPlaceholder();
1458
-
1459
- if (this.opts.element.val() === "" && placeholder !== undefined) {
1460
-
1461
- // check for a first blank option if attached to a select
1462
- if (this.select && this.select.find("option:first").text() !== "") return;
1463
-
1464
- this.selection.find("span").html(escapeMarkup(placeholder));
1465
-
1466
- this.selection.addClass("select2-default");
1467
-
1468
- this.selection.find("abbr").hide();
1469
- }
1470
- },
1471
-
1472
- // single
1473
- postprocessResults: function (data, initial) {
1474
- var selected = 0, self = this, showSearchInput = true;
1475
-
1476
- // find the selected element in the result list
1477
-
1478
- this.results.find(".select2-result-selectable").each2(function (i, elm) {
1479
- if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) {
1480
- selected = i;
1481
- return false;
1482
- }
1483
- });
1484
-
1485
- // and highlight it
1486
-
1487
- this.highlight(selected);
1488
-
1489
- // hide the search box if this is the first we got the results and there are a few of them
1490
-
1491
- if (initial === true) {
1492
- // TODO below we use data.results.length, but what we really need is something recursive to calc the length
1493
- // TODO in case there are optgroups
1494
- showSearchInput = this.showSearchInput = data.results.length >= this.opts.minimumResultsForSearch;
1495
- this.dropdown.find(".select2-search")[showSearchInput ? "removeClass" : "addClass"]("select2-search-hidden");
1496
-
1497
- //add "select2-with-searchbox" to the container if search box is shown
1498
- $(this.dropdown, this.container)[showSearchInput ? "addClass" : "removeClass"]("select2-with-searchbox");
1499
- }
1500
-
1501
- },
1502
-
1503
- // single
1504
- onSelect: function (data) {
1505
- var old = this.opts.element.val();
1506
-
1507
- this.opts.element.val(this.id(data));
1508
- this.updateSelection(data);
1509
- this.close();
1510
- this.selection.focus();
1511
-
1512
- if (!equal(old, this.id(data))) { this.triggerChange(); }
1513
- },
1514
-
1515
- // single
1516
- updateSelection: function (data) {
1517
-
1518
- var container=this.selection.find("span"), formatted;
1519
-
1520
- this.selection.data("select2-data", data);
1521
-
1522
- container.empty();
1523
- formatted=this.opts.formatSelection(data, container);
1524
- if (formatted !== undefined) {
1525
- container.append(escapeMarkup(formatted));
1526
- }
1527
-
1528
- this.selection.removeClass("select2-default");
1529
-
1530
- if (this.opts.allowClear && this.getPlaceholder() !== undefined) {
1531
- this.selection.find("abbr").show();
1532
- }
1533
- },
1534
-
1535
- // single
1536
- val: function () {
1537
- var val, data = null, self = this;
1538
-
1539
- if (arguments.length === 0) {
1540
- return this.opts.element.val();
1541
- }
1542
-
1543
- val = arguments[0];
1544
-
1545
- if (this.select) {
1546
- this.select
1547
- .val(val)
1548
- .find(":selected").each2(function (i, elm) {
1549
- data = {id: elm.attr("value"), text: elm.text()};
1550
- return false;
1551
- });
1552
- this.updateSelection(data);
1553
- this.setPlaceholder();
1554
- } else {
1555
- if (this.opts.initSelection === undefined) {
1556
- throw new Error("cannot call val() if initSelection() is not defined");
1557
- }
1558
- // val is an id. !val is true for [undefined,null,'']
1559
- if (!val) {
1560
- this.clear();
1561
- return;
1562
- }
1563
- this.opts.initSelection(this.opts.element, function(data){
1564
- self.opts.element.val(!data ? "" : self.id(data));
1565
- self.updateSelection(data);
1566
- self.setPlaceholder();
1567
- });
1568
- }
1569
- },
1570
-
1571
- // single
1572
- clearSearch: function () {
1573
- this.search.val("");
1574
- },
1575
-
1576
- // single
1577
- data: function(value) {
1578
- var data;
1579
-
1580
- if (arguments.length === 0) {
1581
- data = this.selection.data("select2-data");
1582
- if (data == undefined) data = null;
1583
- return data;
1584
- } else {
1585
- if (!value || value === "") {
1586
- this.clear();
1587
- } else {
1588
- this.opts.element.val(!value ? "" : this.id(value));
1589
- this.updateSelection(value);
1590
- }
1591
- }
1592
- }
1593
- });
1594
-
1595
- MultiSelect2 = clazz(AbstractSelect2, {
1596
-
1597
- // multi
1598
- createContainer: function () {
1599
- var container = $("<div></div>", {
1600
- "class": "select2-container select2-container-multi"
1601
- }).html([
1602
- " <ul class='select2-choices'>",
1603
- //"<li class='select2-search-choice'><span>California</span><a href="javascript:void(0)" class="select2-search-choice-close"></a></li>" ,
1604
- " <li class='select2-search-field'>" ,
1605
- " <input type='text' autocomplete='off' style='width: 25px;' class='select2-input'>" ,
1606
- " </li>" ,
1607
- "</ul>" ,
1608
- "<div class='select2-drop select2-drop-multi' style='display:none;'>" ,
1609
- " <ul class='select2-results'>" ,
1610
- " </ul>" ,
1611
- "</div>"].join(""));
1612
- return container;
1613
- },
1614
-
1615
- // multi
1616
- prepareOpts: function () {
1617
- var opts = this.parent.prepareOpts.apply(this, arguments);
1618
-
1619
- // TODO validate placeholder is a string if specified
1620
-
1621
- if (opts.element.get(0).tagName.toLowerCase() === "select") {
1622
- // install sthe selection initializer
1623
- opts.initSelection = function (element,callback) {
1624
-
1625
- var data = [];
1626
- element.find(":selected").each2(function (i, elm) {
1627
- data.push({id: elm.attr("value"), text: elm.text()});
1628
- });
1629
-
1630
- if ($.isFunction(callback))
1631
- callback(data);
1632
- };
1633
- }
1634
-
1635
- return opts;
1636
- },
1637
-
1638
- // multi
1639
- initContainer: function () {
1640
-
1641
- var selector = ".select2-choices", selection;
1642
-
1643
- this.searchContainer = this.container.find(".select2-search-field");
1644
- this.selection = selection = this.container.find(selector);
1645
-
1646
- this.search.bind("keydown", this.bind(function (e) {
1647
- if (!this.enabled) return;
1648
-
1649
- if (e.which === KEY.BACKSPACE && this.search.val() === "") {
1650
- this.close();
1651
-
1652
- var choices,
1653
- selected = selection.find(".select2-search-choice-focus");
1654
- if (selected.length > 0) {
1655
- this.unselect(selected.first());
1656
- this.search.width(10);
1657
- killEvent(e);
1658
- return;
1659
- }
1660
-
1661
- choices = selection.find(".select2-search-choice");
1662
- if (choices.length > 0) {
1663
- choices.last().addClass("select2-search-choice-focus");
1664
- }
1665
- } else {
1666
- selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
1667
- }
1668
-
1669
- if (this.opened()) {
1670
- switch (e.which) {
1671
- case KEY.UP:
1672
- case KEY.DOWN:
1673
- this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
1674
- killEvent(e);
1675
- return;
1676
- case KEY.ENTER:
1677
- case KEY.TAB:
1678
- this.selectHighlighted();
1679
- killEvent(e);
1680
- return;
1681
- case KEY.ESC:
1682
- this.cancel(e);
1683
- killEvent(e);
1684
- return;
1685
- }
1686
- }
1687
-
1688
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.BACKSPACE || e.which === KEY.ESC) {
1689
- return;
1690
- }
1691
-
1692
- this.open();
1693
-
1694
- if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
1695
- // prevent the page from scrolling
1696
- killEvent(e);
1697
- }
1698
- }));
1699
-
1700
- this.search.bind("keyup", this.bind(this.resizeSearch));
1701
-
1702
- this.search.bind("blur", this.bind(function() {
1703
- this.container.removeClass("select2-container-active");
1704
- }));
1705
-
1706
- this.container.delegate(selector, "mousedown", this.bind(function (e) {
1707
- if (!this.enabled) return;
1708
- this.clearPlaceholder();
1709
- this.open();
1710
- this.focusSearch();
1711
- e.preventDefault();
1712
- }));
1713
-
1714
- this.container.delegate(selector, "focus", this.bind(function () {
1715
- if (!this.enabled) return;
1716
- this.container.addClass("select2-container-active");
1717
- this.dropdown.addClass("select2-drop-active");
1718
- this.clearPlaceholder();
1719
- }));
1720
-
1721
- // set the placeholder if necessary
1722
- this.clearSearch();
1723
- },
1724
-
1725
- // multi
1726
- enable: function() {
1727
- if (this.enabled) return;
1728
-
1729
- this.parent.enable.apply(this, arguments);
1730
-
1731
- this.search.removeAttr("disabled");
1732
- },
1733
-
1734
- // multi
1735
- disable: function() {
1736
- if (!this.enabled) return;
1737
-
1738
- this.parent.disable.apply(this, arguments);
1739
-
1740
- this.search.attr("disabled", true);
1741
- },
1742
-
1743
- // multi
1744
- initSelection: function () {
1745
- var data;
1746
- if (this.opts.element.val() === "") {
1747
- this.updateSelection([]);
1748
- this.close();
1749
- // set the placeholder if necessary
1750
- this.clearSearch();
1751
- }
1752
- if (this.select || this.opts.element.val() !== "") {
1753
- var self = this;
1754
- this.opts.initSelection.call(null, this.opts.element, function(data){
1755
- if (data !== undefined && data !== null) {
1756
- self.updateSelection(data);
1757
- self.close();
1758
- // set the placeholder if necessary
1759
- self.clearSearch();
1760
- }
1761
- });
1762
- }
1763
- },
1764
-
1765
- // multi
1766
- clearSearch: function () {
1767
- var placeholder = this.getPlaceholder();
1768
-
1769
- if (placeholder !== undefined && this.getVal().length === 0 && this.search.hasClass("select2-focused") === false) {
1770
- this.search.val(placeholder).addClass("select2-default");
1771
- // stretch the search box to full width of the container so as much of the placeholder is visible as possible
1772
- this.resizeSearch();
1773
- } else {
1774
- // we set this to " " instead of "" and later clear it on focus() because there is a firefox bug
1775
- // that does not properly render the caret when the field starts out blank
1776
- this.search.val(" ").width(10);
1777
- }
1778
- },
1779
-
1780
- // multi
1781
- clearPlaceholder: function () {
1782
- if (this.search.hasClass("select2-default")) {
1783
- this.search.val("").removeClass("select2-default");
1784
- } else {
1785
- // work around for the space character we set to avoid firefox caret bug
1786
- if (this.search.val() === " ") this.search.val("");
1787
- }
1788
- },
1789
-
1790
- // multi
1791
- opening: function () {
1792
- this.parent.opening.apply(this, arguments);
1793
-
1794
- this.clearPlaceholder();
1795
- this.resizeSearch();
1796
- this.focusSearch();
1797
- },
1798
-
1799
- // multi
1800
- close: function () {
1801
- if (!this.opened()) return;
1802
- this.parent.close.apply(this, arguments);
1803
- },
1804
-
1805
- // multi
1806
- focus: function () {
1807
- this.close();
1808
- this.search.focus();
1809
- },
1810
-
1811
- // multi
1812
- isFocused: function () {
1813
- return this.search.hasClass("select2-focused");
1814
- },
1815
-
1816
- // multi
1817
- updateSelection: function (data) {
1818
- var ids = [], filtered = [], self = this;
1819
-
1820
- // filter out duplicates
1821
- $(data).each(function () {
1822
- if (indexOf(self.id(this), ids) < 0) {
1823
- ids.push(self.id(this));
1824
- filtered.push(this);
1825
- }
1826
- });
1827
- data = filtered;
1828
-
1829
- this.selection.find(".select2-search-choice").remove();
1830
- $(data).each(function () {
1831
- self.addSelectedChoice(this);
1832
- });
1833
- self.postprocessResults();
1834
- },
1835
-
1836
- // multi
1837
- onSelect: function (data) {
1838
- this.addSelectedChoice(data);
1839
- if (this.select) { this.postprocessResults(); }
1840
-
1841
- if (this.opts.closeOnSelect) {
1842
- this.close();
1843
- this.search.width(10);
1844
- } else {
1845
- this.search.width(10);
1846
- this.resizeSearch();
1847
-
1848
- if (this.countSelectableResults()>0) {
1849
- this.positionDropdown();
1850
- } else {
1851
- // if nothing left to select close
1852
- this.close();
1853
- }
1854
- }
1855
-
1856
- // since its not possible to select an element that has already been
1857
- // added we do not need to check if this is a new element before firing change
1858
- this.triggerChange({ added: data });
1859
-
1860
- this.focusSearch();
1861
- },
1862
-
1863
- // multi
1864
- cancel: function () {
1865
- this.close();
1866
- this.focusSearch();
1867
- },
1868
-
1869
- // multi
1870
- addSelectedChoice: function (data) {
1871
- var choice=$(
1872
- "<li class='select2-search-choice'>" +
1873
- " <div></div>" +
1874
- " <a href='javascript:void(0)' class='select2-search-choice-close' tabindex='-1'></a>" +
1875
- "</li>"),
1876
- id = this.id(data),
1877
- val = this.getVal(),
1878
- formatted;
1879
-
1880
- formatted=this.opts.formatSelection(data, choice);
1881
- choice.find("div").replaceWith("<div>"+escapeMarkup(formatted)+"</div>");
1882
- choice.find(".select2-search-choice-close")
1883
- .bind("click dblclick", this.bind(function (e) {
1884
- if (!this.enabled) return;
1885
-
1886
- $(e.target).closest(".select2-search-choice").fadeOut('fast').animate({width: "hide"}, 50, this.bind(function(){
1887
- this.unselect($(e.target));
1888
- this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
1889
- this.close();
1890
- this.focusSearch();
1891
- })).dequeue();
1892
- killEvent(e);
1893
- })).bind("focus", this.bind(function () {
1894
- if (!this.enabled) return;
1895
- this.container.addClass("select2-container-active");
1896
- this.dropdown.addClass("select2-drop-active");
1897
- }));
1898
-
1899
- choice.data("select2-data", data);
1900
- choice.insertBefore(this.searchContainer);
1901
-
1902
- val.push(id);
1903
- this.setVal(val);
1904
- },
1905
-
1906
- // multi
1907
- unselect: function (selected) {
1908
- var val = this.getVal(),
1909
- data,
1910
- index;
1911
-
1912
- selected = selected.closest(".select2-search-choice");
1913
-
1914
- if (selected.length === 0) {
1915
- throw "Invalid argument: " + selected + ". Must be .select2-search-choice";
1916
- }
1917
-
1918
- data = selected.data("select2-data");
1919
-
1920
- index = indexOf(this.id(data), val);
1921
-
1922
- if (index >= 0) {
1923
- val.splice(index, 1);
1924
- this.setVal(val);
1925
- if (this.select) this.postprocessResults();
1926
- }
1927
- selected.remove();
1928
- this.triggerChange({ removed: data });
1929
- },
1930
-
1931
- // multi
1932
- postprocessResults: function () {
1933
- var val = this.getVal(),
1934
- choices = this.results.find(".select2-result-selectable"),
1935
- compound = this.results.find(".select2-result-with-children"),
1936
- self = this;
1937
-
1938
- choices.each2(function (i, choice) {
1939
- var id = self.id(choice.data("select2-data"));
1940
- if (indexOf(id, val) >= 0) {
1941
- choice.addClass("select2-disabled").removeClass("select2-result-selectable");
1942
- } else {
1943
- choice.removeClass("select2-disabled").addClass("select2-result-selectable");
1944
- }
1945
- });
1946
-
1947
- compound.each2(function(i, e) {
1948
- if (e.find(".select2-result-selectable").length==0) {
1949
- e.addClass("select2-disabled");
1950
- } else {
1951
- e.removeClass("select2-disabled");
1952
- }
1953
- });
1954
-
1955
- choices.each2(function (i, choice) {
1956
- if (!choice.hasClass("select2-disabled") && choice.hasClass("select2-result-selectable")) {
1957
- self.highlight(0);
1958
- return false;
1959
- }
1960
- });
1961
-
1962
- },
1963
-
1964
- // multi
1965
- resizeSearch: function () {
1966
-
1967
- var minimumWidth, left, maxWidth, containerLeft, searchWidth,
1968
- sideBorderPadding = getSideBorderPadding(this.search);
1969
-
1970
- minimumWidth = measureTextWidth(this.search) + 10;
1971
-
1972
- left = this.search.offset().left;
1973
-
1974
- maxWidth = this.selection.width();
1975
- containerLeft = this.selection.offset().left;
1976
-
1977
- searchWidth = maxWidth - (left - containerLeft) - sideBorderPadding;
1978
-
1979
- if (searchWidth < minimumWidth) {
1980
- searchWidth = maxWidth - sideBorderPadding;
1981
- }
1982
-
1983
- if (searchWidth < 40) {
1984
- searchWidth = maxWidth - sideBorderPadding;
1985
- }
1986
- this.search.width(searchWidth);
1987
- },
1988
-
1989
- // multi
1990
- getVal: function () {
1991
- var val;
1992
- if (this.select) {
1993
- val = this.select.val();
1994
- return val === null ? [] : val;
1995
- } else {
1996
- val = this.opts.element.val();
1997
- return splitVal(val, this.opts.separator);
1998
- }
1999
- },
2000
-
2001
- // multi
2002
- setVal: function (val) {
2003
- var unique;
2004
- if (this.select) {
2005
- this.select.val(val);
2006
- } else {
2007
- unique = [];
2008
- // filter out duplicates
2009
- $(val).each(function () {
2010
- if (indexOf(this, unique) < 0) unique.push(this);
2011
- });
2012
- this.opts.element.val(unique.length === 0 ? "" : unique.join(this.opts.separator));
2013
- }
2014
- },
2015
-
2016
- // multi
2017
- val: function () {
2018
- var val, data = [], self=this;
2019
-
2020
- if (arguments.length === 0) {
2021
- return this.getVal();
2022
- }
2023
-
2024
- val = arguments[0];
2025
-
2026
- if (!val) {
2027
- this.opts.element.val("");
2028
- this.updateSelection([]);
2029
- this.clearSearch();
2030
- return;
2031
- }
2032
-
2033
- // val is a list of ids
2034
- this.setVal(val);
2035
-
2036
- if (this.select) {
2037
- this.select.find(":selected").each(function () {
2038
- data.push({id: $(this).attr("value"), text: $(this).text()});
2039
- });
2040
- this.updateSelection(data);
2041
- } else {
2042
- if (this.opts.initSelection === undefined) {
2043
- throw new Error("val() cannot be called if initSelection() is not defined")
2044
- }
2045
-
2046
- this.opts.initSelection(this.opts.element, function(data){
2047
- var ids=$(data).map(self.id);
2048
- self.setVal(ids);
2049
- self.updateSelection(data);
2050
- self.clearSearch();
2051
- });
2052
- }
2053
- this.clearSearch();
2054
- },
2055
-
2056
- // multi
2057
- onSortStart: function() {
2058
- if (this.select) {
2059
- throw new Error("Sorting of elements is not supported when attached to <select>. Attach to <input type='hidden'/> instead.");
2060
- }
2061
-
2062
- // collapse search field into 0 width so its container can be collapsed as well
2063
- this.search.width(0);
2064
- // hide the container
2065
- this.searchContainer.hide();
2066
- },
2067
-
2068
- // multi
2069
- onSortEnd:function() {
2070
-
2071
- var val=[], self=this;
2072
-
2073
- // show search and move it to the end of the list
2074
- this.searchContainer.show();
2075
- // make sure the search container is the last item in the list
2076
- this.searchContainer.appendTo(this.searchContainer.parent());
2077
- // since we collapsed the width in dragStarted, we resize it here
2078
- this.resizeSearch();
2079
-
2080
- // update selection
2081
-
2082
- this.selection.find(".select2-search-choice").each(function() {
2083
- val.push(self.opts.id($(this).data("select2-data")));
2084
- });
2085
- this.setVal(val);
2086
- this.triggerChange();
2087
- },
2088
-
2089
- // multi
2090
- data: function(values) {
2091
- var self=this, ids;
2092
- if (arguments.length === 0) {
2093
- return this.selection
2094
- .find(".select2-search-choice")
2095
- .map(function() { return $(this).data("select2-data"); })
2096
- .get();
2097
- } else {
2098
- if (!values) { values = []; }
2099
- ids = $.map(values, function(e) { return self.opts.id(e)});
2100
- this.setVal(ids);
2101
- this.updateSelection(values);
2102
- this.clearSearch();
2103
- }
2104
- }
2105
- });
2106
-
2107
- $.fn.select2 = function () {
2108
-
2109
- var args = Array.prototype.slice.call(arguments, 0),
2110
- opts,
2111
- select2,
2112
- value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown", "data"];
2113
-
2114
- this.each(function () {
2115
- if (args.length === 0 || typeof(args[0]) === "object") {
2116
- opts = args.length === 0 ? {} : $.extend({}, args[0]);
2117
- opts.element = $(this);
2118
-
2119
- if (opts.element.get(0).tagName.toLowerCase() === "select") {
2120
- multiple = opts.element.attr("multiple");
2121
- } else {
2122
- multiple = opts.multiple || false;
2123
- if ("tags" in opts) {opts.multiple = multiple = true;}
2124
- }
2125
-
2126
- select2 = multiple ? new MultiSelect2() : new SingleSelect2();
2127
- select2.init(opts);
2128
- } else if (typeof(args[0]) === "string") {
2129
-
2130
- if (indexOf(args[0], allowedMethods) < 0) {
2131
- throw "Unknown method: " + args[0];
2132
- }
2133
-
2134
- value = undefined;
2135
- select2 = $(this).data("select2");
2136
- if (select2 === undefined) return;
2137
- if (args[0] === "container") {
2138
- value=select2.container;
2139
- } else {
2140
- value = select2[args[0]].apply(select2, args.slice(1));
2141
- }
2142
- if (value !== undefined) {return false;}
2143
- } else {
2144
- throw "Invalid arguments to select2 plugin: " + args;
2145
- }
2146
- });
2147
- return (value === undefined) ? this : value;
2148
- };
2149
-
2150
- // plugin defaults, accessible to users
2151
- $.fn.select2.defaults = {
2152
- width: "copy",
2153
- closeOnSelect: true,
2154
- containerCss: {},
2155
- dropdownCss: {},
2156
- containerCssClass: "",
2157
- dropdownCssClass: "",
2158
- formatResult: function(result, container, query) {
2159
- var markup=[];
2160
- markMatch(result.text, query.term, markup);
2161
- return markup.join("");
2162
- },
2163
- formatSelection: function (data, container) {
2164
- return data.text;
2165
- },
2166
- formatNoMatches: function () { return "No matches found"; },
2167
- formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
2168
- formatSelectionTooBig: function (limit) { return "You can only select " + limit + " items"; },
2169
- formatLoadMore: function (pageNumber) { return "Loading more results..."; },
2170
- minimumResultsForSearch: 0,
2171
- minimumInputLength: 0,
2172
- maximumSelectionSize: 0,
2173
- id: function (e) { return e.id; },
2174
- matcher: function(term, text) {
2175
- return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;
2176
- }
2177
- };
2178
-
2179
- // exports
2180
- window.Select2 = {
2181
- query: {
2182
- ajax: ajax,
2183
- local: local,
2184
- tags: tags
2185
- }, util: {
2186
- debounce: debounce,
2187
- markMatch: markMatch
2188
- }, "class": {
2189
- "abstract": AbstractSelect2,
2190
- "single": SingleSelect2,
2191
- "multi": MultiSelect2
2192
- }
2193
- };
2194
-
2195
- }(jQuery));