redmine_extensions 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/redmine_extensions/redmine_extensions.js.orig +648 -0
  3. data/app/views/easy_entity_assignments/_query_index.html.erb.orig +16 -0
  4. data/config/locales/cs.yml +4 -1
  5. data/config/locales/en.yml +1 -0
  6. data/lib/generators/redmine_extensions/entity/templates/context_menu.html.erb.erb +14 -1
  7. data/lib/generators/redmine_extensions/entity/templates/controller.rb.erb +40 -6
  8. data/lib/generators/redmine_extensions/entity/templates/edit.js.erb.erb +5 -5
  9. data/lib/generators/redmine_extensions/entity/templates/index.js.erb.erb +3 -3
  10. data/lib/generators/redmine_extensions/entity/templates/new.js.erb.erb +5 -5
  11. data/lib/generators/redmine_extensions/entity/templates/show.js.erb.erb +3 -3
  12. data/lib/redmine_extensions/easy_query_helpers/outputs.rb +18 -2
  13. data/lib/redmine_extensions/query_output.rb +30 -11
  14. data/lib/redmine_extensions/redmine_patches/controllers/issues_controller_patch.rb +9 -0
  15. data/lib/redmine_extensions/version.rb +1 -1
  16. data/lib/redmine_extensions/version.rb.orig +7 -0
  17. data/spec/redmine/app/controllers/custom_field_enumerations_controller.rb +75 -0
  18. data/spec/redmine/app/controllers/imports_controller.rb +121 -0
  19. data/spec/redmine/app/helpers/imports_helper.rb +47 -0
  20. data/spec/redmine/app/models/custom_field_enumeration.rb +90 -0
  21. data/spec/redmine/app/models/import.rb +249 -0
  22. data/spec/redmine/app/models/import_item.rb +22 -0
  23. data/spec/redmine/app/models/issue_import.rb +186 -0
  24. data/spec/redmine/app/views/attachments/image.html.erb +3 -0
  25. data/spec/redmine/app/views/attachments/other.html.erb +3 -0
  26. data/spec/redmine/app/views/common/_image.html.erb +1 -0
  27. data/spec/redmine/app/views/common/_other.html.erb +1 -0
  28. data/spec/redmine/app/views/custom_field_enumerations/create.js.erb +2 -0
  29. data/spec/redmine/app/views/custom_field_enumerations/destroy.html.erb +14 -0
  30. data/spec/redmine/app/views/custom_field_enumerations/index.html.erb +47 -0
  31. data/spec/redmine/app/views/custom_fields/formats/_enumeration.erb +12 -0
  32. data/spec/redmine/app/views/imports/_fields_mapping.html.erb +90 -0
  33. data/spec/redmine/app/views/imports/mapping.html.erb +52 -0
  34. data/spec/redmine/app/views/imports/mapping.js.erb +1 -0
  35. data/spec/redmine/app/views/imports/new.html.erb +15 -0
  36. data/spec/redmine/app/views/imports/run.html.erb +20 -0
  37. data/spec/redmine/app/views/imports/run.js.erb +11 -0
  38. data/spec/redmine/app/views/imports/settings.html.erb +30 -0
  39. data/spec/redmine/app/views/imports/show.html.erb +38 -0
  40. data/spec/redmine/app/views/layouts/_file.html.erb +15 -0
  41. data/spec/redmine/app/views/mailer/security_notification.html.erb +13 -0
  42. data/spec/redmine/app/views/mailer/security_notification.text.erb +8 -0
  43. data/spec/redmine/app/views/mailer/settings_updated.html.erb +14 -0
  44. data/spec/redmine/app/views/mailer/settings_updated.text.erb +12 -0
  45. data/spec/redmine/app/views/queries/_query_form.html.erb +62 -0
  46. data/spec/redmine/app/views/repositories/_changeset.html.erb +40 -0
  47. data/spec/redmine/app/views/search/index.api.rsb +12 -0
  48. data/spec/redmine/app/views/settings/_api.html.erb +10 -0
  49. data/spec/redmine/app/views/settings/_attachments.html.erb +21 -0
  50. data/spec/redmine/app/views/wiki/_new_modal.html.erb +21 -0
  51. data/spec/redmine/app/views/wiki/new.html.erb +17 -0
  52. data/spec/redmine/app/views/wiki/new.js.erb +2 -0
  53. data/spec/redmine/appveyor.yml +36 -0
  54. data/spec/redmine/db/migrate/20150725112753_insert_allowed_statuses_for_new_issues.rb +23 -0
  55. data/spec/redmine/db/migrate/20150730122707_create_imports.rb +13 -0
  56. data/spec/redmine/db/migrate/20150730122735_create_import_items.rb +10 -0
  57. data/spec/redmine/db/migrate/20150921204850_change_time_entries_comments_limit_to_1024.rb +9 -0
  58. data/spec/redmine/db/migrate/20150921210243_change_wiki_contents_comments_limit_to_1024.rb +11 -0
  59. data/spec/redmine/db/migrate/20151020182334_change_attachments_filesize_limit_to_8.rb +9 -0
  60. data/spec/redmine/db/migrate/20151020182731_fix_comma_in_user_format_setting_value.rb +13 -0
  61. data/spec/redmine/db/migrate/20151021184614_change_issue_categories_name_limit_to_60.rb +9 -0
  62. data/spec/redmine/db/migrate/20151021185456_change_auth_sources_filter_to_text.rb +9 -0
  63. data/spec/redmine/db/migrate/20151021190616_change_user_preferences_hide_mail_default_to_true.rb +9 -0
  64. data/spec/redmine/db/migrate/20151024082034_add_tokens_updated_on.rb +10 -0
  65. data/spec/redmine/db/migrate/20151025072118_create_custom_field_enumerations.rb +10 -0
  66. data/spec/redmine/db/migrate/20151031095005_add_projects_default_version_id.rb +12 -0
  67. data/spec/redmine/db/migrate/20160404080304_force_password_reset_during_setup.rb +9 -0
  68. data/spec/redmine/db/migrate/20160416072926_remove_position_defaults.rb +13 -0
  69. data/spec/redmine/db/migrate/20160529063352_add_roles_settings.rb +5 -0
  70. data/spec/redmine/lib/redmine/acts/positioned.rb +118 -0
  71. data/spec/redmine/lib/redmine/helpers/url.rb +35 -0
  72. data/spec/redmine/lib/redmine/hook/listener.rb +32 -0
  73. data/spec/redmine/lib/redmine/hook/view_listener.rb +78 -0
  74. data/spec/redmine/log/test.log +2 -0
  75. data/spec/redmine/plugins/dummy_plugin/Gemfile +1 -0
  76. data/spec/redmine/plugins/dummy_plugin/app/controllers/dummy_autocompletes_controller.rb +4 -0
  77. data/spec/redmine/plugins/dummy_plugin/app/views/dummy_autocompletes/index.html.erb +3 -0
  78. data/spec/redmine/plugins/dummy_plugin/config/locales/en.yml +2 -0
  79. data/spec/redmine/plugins/dummy_plugin/config/routes.rb +1 -0
  80. data/spec/redmine/plugins/dummy_plugin/init.rb +34 -0
  81. data/spec/redmine/plugins/dummy_plugin/lib/dummy_plugin/easy_patch/redmine/controllers/issues_controller_patch.example +30 -0
  82. data/spec/redmine/plugins/dummy_plugin/lib/dummy_plugin/easy_patch/redmine/helpers/issues_helper_patch.example +30 -0
  83. data/spec/redmine/plugins/dummy_plugin/lib/dummy_plugin/easy_patch/redmine/models/issue_patch.example +30 -0
  84. data/spec/redmine/plugins/dummy_plugin/lib/dummy_plugin/hooks.rb +5 -0
  85. data/spec/redmine/plugins/dummy_plugin/lib/dummy_plugin/internals.rb +4 -0
  86. data/spec/redmine/public/images/jstoolbar/bt_precode.png +0 -0
  87. data/spec/redmine/public/images/reorder.png +0 -0
  88. data/spec/redmine/public/javascripts/jquery-1.11.1-ui-1.11.0-ujs-3.1.4.js +21 -0
  89. data/spec/redmine/public/javascripts/redmine_extensions/application.js +13 -0
  90. data/spec/redmine/public/javascripts/redmine_extensions/easy_togglers.js +58 -0
  91. data/spec/redmine/public/javascripts/redmine_extensions/jquery.entityarray.js +130 -0
  92. data/spec/redmine/public/javascripts/redmine_extensions/redmine_extensions.js +642 -0
  93. data/spec/redmine/public/javascripts/responsive.js +83 -0
  94. data/spec/redmine/public/stylesheets/responsive.css +799 -0
  95. data/spec/redmine/test/fixtures/files/import_dates.csv +4 -0
  96. data/spec/redmine/test/fixtures/files/import_iso8859-1.csv +3 -0
  97. data/spec/redmine/test/fixtures/files/import_issues.csv +4 -0
  98. data/spec/redmine/test/fixtures/ldap/slapd.centos6.conf +24 -0
  99. data/spec/redmine/test/fixtures/ldap/slapd.ubuntu.12.04.conf +23 -0
  100. data/spec/redmine/test/fixtures/mail_handler/issue_update_with_cc.eml +19 -0
  101. data/spec/redmine/test/fixtures/mail_handler/ticket_on_project_given_by_to_header.eml +60 -0
  102. data/spec/redmine/test/fixtures/mail_handler/ticket_with_text_attachment_iso-8859-2.eml +31 -0
  103. data/spec/redmine/test/functional/custom_field_enumerations_controller_test.rb +115 -0
  104. data/spec/redmine/test/functional/imports_controller_test.rb +205 -0
  105. data/spec/redmine/test/functional/sessions_controller_test.rb +138 -0
  106. data/spec/redmine/test/integration/api_test/search_test.rb +92 -0
  107. data/spec/redmine/test/integration/routing/imports_test.rb +36 -0
  108. data/spec/redmine/test/integration/sessions_test.rb +97 -0
  109. data/spec/redmine/test/ui/custom_fields_test_ui.rb +38 -0
  110. data/spec/redmine/test/unit/attachment_transaction_test.rb +76 -0
  111. data/spec/redmine/test/unit/helpers/journals_helper_test.rb +48 -0
  112. data/spec/redmine/test/unit/helpers/wiki_helper_test.rb +45 -0
  113. data/spec/redmine/test/unit/issue_import_test.rb +178 -0
  114. data/spec/redmine/test/unit/lib/redmine/acts/positioned_with_scope_test.rb +53 -0
  115. data/spec/redmine/test/unit/lib/redmine/acts/positioned_without_scope_test.rb +55 -0
  116. data/spec/redmine/test/unit/lib/redmine/field_format/enumeration_format_test.rb +91 -0
  117. data/spec/support/easy_queries_helpers.rb +0 -0
  118. metadata +216 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51dcad00b5efb1ea9c707d9e06d040ebe017106c
4
- data.tar.gz: 2dc9c7cff300292d0ca6ccda7628ea0806dbd4c8
3
+ metadata.gz: 2b7495846c82f712c1753c94dbf41cdedb9d1d78
4
+ data.tar.gz: 9195fa39143a338c916132cdb4bcf11db76b4320
5
5
  SHA512:
6
- metadata.gz: 774834037c521abc40ea86661b503069105d1a0eb71e0bfafd00af055c8c35ef90cb81aa294250f22cab452a60f5ab26d957d683244100d3d557bd1ce8687985
7
- data.tar.gz: 3309f376782ac951b5f2bb16bba2a653d06bef46a4f1c3defadac89f7bbec3f624feb0fd27a7ca7c0ba2d84d468240dbcd63b1ba9c5b4a65fa589bb4808d1783
6
+ metadata.gz: 00f1c48572a81fda0a7d3bf5e9eeae51ecb2827ef5e26de29dcf9285e8aca65950c0ba17664fe5a627c3ce6752abb810eb968278efbd6873a5a638789c283d2b
7
+ data.tar.gz: b33b94bd8821b076b4bfef9dbb0a57008f87ea8570af100a76eb6fcfe88cac3bdde487c015d11d8ae1eeb95fbf28290b7ff3a03a9611d89da81c3c33b6509de8
@@ -0,0 +1,648 @@
1
+ REDMINE_EXTENSIONS = {
2
+
3
+ toggleDiv: function(el_or_id) {
4
+ var el;
5
+ if (typeof(el_or_id) === 'string') {
6
+ el = $('#' + el_or_id);
7
+ } else {
8
+ el = el_or_id;
9
+ }
10
+
11
+ el.toggleClass('collapsed').slideToggle('fast');
12
+ },
13
+
14
+ toggleDivAndChangeOpen: function(toggleElementId, changeOpenElement) {
15
+ REDMINE_EXTENSIONS.toggleDiv(toggleElementId);
16
+ $(changeOpenElement).toggleClass('open');
17
+ },
18
+
19
+ toggleFilterButtons: function(elButtonsID, elFilter1ID, elFilter2ID)
20
+ {
21
+ var elButtons = $('#' + elButtonsID);
22
+ var elFilter1 = $('#' + elFilter1ID);
23
+ var elFilter2 = $('#' + elFilter2ID);
24
+
25
+ if (elFilter1.hasClass('collapsed') && elFilter2.hasClass('collapsed')) {
26
+ elButtons.slideUp('slow');
27
+ } else {
28
+ elButtons.slideDown('slow');
29
+ }
30
+ }
31
+
32
+ };
33
+
34
+
35
+ // TODO delete - it will be added to extensions
36
+ (function($, undefined) {
37
+
38
+ var plugin = 'easygrouploader';
39
+ if( $.fn[plugin] )
40
+ return;
41
+ var defaults = {
42
+ next_button_cols: 1,
43
+ load_opened: false,
44
+ load_limit: 25,
45
+ texts: {
46
+ 'next': 'Next',
47
+ }
48
+ };
49
+
50
+ $.fn[plugin] = function(options, methodParams) {
51
+ $.each($(this), function(idx) {
52
+ var instance = $(this).data('plugin_' + plugin);
53
+ if (!instance) {
54
+ instance = new EasyGroupLoader(this, options);
55
+ $(this).data('plugin_' + plugin, instance);
56
+ } else if (typeof options === 'string') {
57
+ switch (options) {
58
+ case 'load_groups':
59
+ if (instance.options.load_opened)
60
+ instance.load_all_groups();
61
+ }
62
+ }
63
+ });
64
+ return $(this);
65
+ };
66
+
67
+
68
+ function EasyGroupLoader(elem, options) {
69
+ this.groupsContainer = $(elem);
70
+ this.options = $.extend({}, defaults, options);
71
+ this.loadUrl = options.loadUrl || elem.data('url');
72
+ this.callback = options.callback;
73
+ this.texts = this.options.texts;
74
+
75
+ this.init();
76
+ }
77
+
78
+ EasyGroupLoader.prototype.init = function()
79
+ {
80
+ var self = this;
81
+ this.groupsContainer.on('click', '.group .expander', function(evt) {
82
+ var $row = $(this).closest('tr.group');
83
+ var group = $row.data('group') || new Group(self, $row);
84
+
85
+ if (!group.loaded) {
86
+ if (!group.header.hasClass('group-loaded')) {
87
+ group.load();
88
+ group.toggle();
89
+ }
90
+ } else {
91
+ group.toggle();
92
+ if(typeof(self.callback) === 'function')
93
+ self.callback();
94
+ }
95
+
96
+ });
97
+ if (this.options.load_opened)
98
+ this.load_all_groups();
99
+ };
100
+
101
+ EasyGroupLoader.prototype.initInlineEdit = function()
102
+ {
103
+ $('.multieditable-container:not(.multieditable-initialized)', this.groupsContainer).each(function() {
104
+ initInlineEditForContainer(this);
105
+ });
106
+ initProjectEdit();
107
+ initEasyAutocomplete();
108
+ };
109
+
110
+ EasyGroupLoader.prototype.load_all_groups = function()
111
+ {
112
+ var group;
113
+ var self = this;
114
+ var groups_to_load = [];
115
+ var entity_count = 0;
116
+ $('.group', this.groupsContainer).not('.group-loaded').each(function() {
117
+ group = $(this).data('group') || new Group(self, $(this));
118
+ if (!group.loaded) {
119
+ groups_to_load.push(group);
120
+ entity_count += group.count;
121
+ }
122
+ if (entity_count >= self.options.load_limit) {
123
+ self.load_groups(groups_to_load);
124
+ entity_count = 0;
125
+ groups_to_load = [];
126
+ }
127
+ });
128
+ if (groups_to_load.length > 0) {
129
+ this.load_groups(groups_to_load);
130
+ }
131
+ };
132
+
133
+ EasyGroupLoader.prototype.load_groups = function(groups_to_load) {
134
+ var self = this;
135
+ var group_names = groups_to_load.map(function(group) {
136
+ return group.group_value;
137
+ });
138
+ // var url = EPExtensions.setAttrToUrl(, 'group_to_load', group_names);
139
+ $.ajax(this.loadUrl, {
140
+ method: 'GET',
141
+ data: { group_to_load: group_names },
142
+ success: function(data, textStatus, request) {
143
+ var parsed = (typeof data === 'object') ? data : $.parseJSON(data);
144
+
145
+ $.each(groups_to_load, function(idx, group) {
146
+ group.parseData(parsed[group.group_name]);
147
+ group.toggle();
148
+ });
149
+ self.initInlineEdit();
150
+ }
151
+ });
152
+ };
153
+
154
+ function Group(loader, header)
155
+ {
156
+ this.loader = loader;
157
+ this.header = header;
158
+ this.header.data('group', this);
159
+ this.group_name = this.header.data('group-name');
160
+ this.group_value = this.group_name;
161
+ if( $.isArray(this.group_name) ) {
162
+ // potencialne nebezpecne - TODO: vymyslet spravny oddelovac
163
+ this.group_name = '["' + this.group_name.join('", "') + '"]';
164
+ }
165
+ this.count = parseInt(this.header.data('entity-count'));
166
+ this.pages = this.header.data('pages') || 1;
167
+ this.loaded = this.header.hasClass('preloaded');
168
+ }
169
+
170
+ Group.prototype.toggle = function() {
171
+ EPExtensions.issuesToggleRowGroup(this.header);
172
+ };
173
+
174
+ Group.prototype.load = function() {
175
+ var $hrow = this.header;
176
+ var self = this;
177
+
178
+ if (!$hrow.hasClass('group-loaded')) {
179
+ $hrow.addClass('group-loaded');
180
+ $.ajax(this.loader.loadUrl, {
181
+ method: 'GET',
182
+ data: {
183
+ group_to_load: this.group_value
184
+ },
185
+ success: function(data, textStatus, request) {
186
+ self.parseData(data);
187
+ self.loader.initInlineEdit();
188
+ if(typeof(self.loader.callback) === 'function')
189
+ self.loader.callback();
190
+ }
191
+ });
192
+ }
193
+ };
194
+
195
+ Group.prototype.parseData = function(data) {
196
+ var $hrow = this.header;
197
+
198
+ this.rows = $(data);
199
+ $hrow.after(this.rows);
200
+ $hrow.data('group-page', 1);
201
+ this.loaded = true;
202
+ if (this.pages > 1) {
203
+ this.createNextButton();
204
+ // .find doesn't work on this set
205
+ this.rows.filter("tr:last").after(this.next_button);
206
+ }
207
+ };
208
+
209
+ Group.prototype.loadNext = function() {
210
+ var $hrow = this.header;
211
+ var page = $hrow.data('group-page') + 1;
212
+ var self = this;
213
+
214
+ if (page <= this.pages) {
215
+ $.ajax(this.loader.loadUrl, {
216
+ method: 'GET',
217
+ data: {
218
+ page: page,
219
+ group_to_load: this.group_value
220
+ },
221
+ success: function(data, textStatus, request) {
222
+ self.next_button.before(data);
223
+
224
+ self.loader.initInlineEdit();
225
+ $hrow.data('group-page', page);
226
+ if (self.pages === page) {
227
+ self.next_button.remove();
228
+ }
229
+ }
230
+ });
231
+ }
232
+ };
233
+
234
+ Group.prototype.createNextButton = function() {
235
+ //var next_link_url = EPExtensions.setAttrToUrl(this.loader.loadUrl, 'group_to_load', this.group_value);
236
+ var next_link_url = this.loader.loadUrl + ( this.loader.loadUrl.indexOf('?') >= 0 ? '&' : '?' ) + $.param({group_to_load: this.group_value});
237
+ this.next_link = $('<a>', {href: next_link_url, 'class': 'button'}).text(this.loader.texts['next']).append($("<i>", {"class": "icon-arrow"}));
238
+ this.next_button = $('<tr/>', {'class': 'easy-next-button'}).html($('<td>', {colspan: this.loader.options.next_button_cols, "class": "text-center"}).html(this.next_link));
239
+
240
+ var self = this;
241
+
242
+ this.next_link.click(function(evt) {
243
+ evt.preventDefault();
244
+ self.loadNext();
245
+ });
246
+ };
247
+
248
+ })(jQuery);
249
+
250
+ window.cancelAnimFrame = ( function() {
251
+ return window.cancelAnimationFrame ||
252
+ window.webkitCancelRequestAnimationFrame ||
253
+ window.mozCancelRequestAnimationFrame ||
254
+ window.oCancelRequestAnimationFrame ||
255
+ window.msCancelRequestAnimationFrame ||
256
+ clearTimeout;
257
+ } )();
258
+
259
+ window.requestAnimFrame = (function(){
260
+ return window.requestAnimationFrame ||
261
+ window.webkitRequestAnimationFrame ||
262
+ window.mozRequestAnimationFrame ||
263
+ window.oRequestAnimationFrame ||
264
+ window.msRequestAnimationFrame ||
265
+ function(callback){
266
+ return window.setTimeout(callback, 1000 / 60);
267
+ };
268
+ })();
269
+
270
+ window.showFlashMessage = (function(type, message, delay){
271
+ var $content = $("#content");
272
+ $content.find(".flash").remove();
273
+ var element = document.createElement("div");
274
+ element.className = 'fixed flash ' + type;
275
+ element.style.position = 'fixed';
276
+ element.style.zIndex = '10001';
277
+ element.style.right = '5px';
278
+ element.style.top = '5px';
279
+ element.setAttribute("onclick", "closeFlashMessage($(this))");
280
+ var close = document.createElement("a");
281
+ close.className = 'icon-close close-icon';
282
+ close.setAttribute("href", "javascript:void(0)");
283
+ close.style.float = 'right';
284
+ close.style.marginLeft = '5px';
285
+ // close.setAttribute("onclick", "closeFlashMessage($(this))");
286
+ var span = document.createElement("span");
287
+ span.innerHTML = message;
288
+ element.appendChild(close);
289
+ element.appendChild(span);
290
+ $content.prepend(element);
291
+ var $element = $(element);
292
+ if(delay){
293
+ setTimeout(function(){
294
+ requestAnimFrame(function(){
295
+ closeFlashMessage($element);
296
+ });
297
+ }, delay);
298
+ }
299
+ return $element;
300
+ });
301
+
302
+ window.closeFlashMessage = (function($element){
303
+ $element.closest('.flash').fadeOut(500, function(){$element.remove();});
304
+ });
305
+
306
+
307
+ (function($, undefined) {
308
+
309
+ $.widget('easy.easymultiselect', {
310
+ options: {
311
+ source: null,
312
+ rootElement: null, // rootElement in the response from source
313
+ selected: null,
314
+ multiple: true, // multiple values can be selected
315
+ preload: true, // load all possible values
316
+ position: {collision: 'flip'},
317
+ autofocus: false,
318
+ inputName: null, // defaults to element prop name
319
+ render_item: function(ul, item) {
320
+ return $("<li>")
321
+ .data("item.autocomplete", item)
322
+ .append(item.label)
323
+ .appendTo(ul);
324
+ },
325
+ activate_on_input_click: true,
326
+ load_immediately: false,
327
+ show_toggle_button: true,
328
+ select_first_value: true,
329
+ autocomplete_options: {}
330
+ },
331
+
332
+ _create: function() {
333
+ this.selectedValues = this.options.selected;
334
+ this._createUI();
335
+ this.expanded = false;
336
+ this.valuesLoaded = false;
337
+ this.afterLoaded = [];
338
+ if ( Array.isArray(this.options.source) ) {
339
+ this.options.preload = true;
340
+ this._initData(this.options.source);
341
+ } else if ( this.options.preload && this.options.load_immediately) {
342
+ this.load();
343
+ } else if ( this.selectedValues ) {
344
+ this.setValue( this.selectedValues );
345
+ }
346
+ },
347
+
348
+ _createUI: function() {
349
+ var that = this;
350
+ this.element.wrap('<span class="easy-autocomplete-tag"></span>');
351
+ this.tag = this.element.parent();
352
+ this.inputName = this.options.inputName || this.element.prop('name');
353
+
354
+ if( this.options.multiple ) { // multiple values
355
+ this.valueElement = $('<span></span>');
356
+ this.tag.after(this.valueElement);
357
+
358
+ if (this.options.show_toggle_button)
359
+ this._createToggleButton();
360
+
361
+ this.valueElement.entityArray({
362
+ inputNames: this.inputName,
363
+ afterRemove: function (entity) {
364
+ that.element.trigger('change');
365
+ }
366
+ });
367
+ } else { //single value
368
+ this.valueElement = $('<input>', {type: 'hidden', name: this.inputName});
369
+ this.element.after(this.valueElement);
370
+ }
371
+
372
+ this._createAutocomplete();
373
+ if( !this.options.multiple ) {
374
+ this.element.css('margin-right', 0);
375
+ }
376
+ },
377
+
378
+ _createToggleButton: function() {
379
+ var that = this;
380
+ this.link_ac_toggle = $('<a>').attr('class', 'icon icon-add clear-link');
381
+ this.link_ac_toggle.click(function(evt) {
382
+ var $elem = $(this);
383
+ evt.preventDefault();
384
+ that.load(function(){
385
+ select = $('<select>').prop('multiple', true).prop('size', 5).prop('name', that.inputName);
386
+ $.each(that.possibleValues, function(i, v) {
387
+ option = $('<option>').prop('value', v.id).text(v.value);
388
+ option.prop('selected', that.getValue().indexOf(v.id) > -1);
389
+ select.append(option);
390
+ });
391
+ $container = $elem.closest('.easy-multiselect-tag-container');
392
+ $container.find(':input').prop('disabled', true);
393
+ $container.children().hide();
394
+ $container.append(select);
395
+ that.valueElement = select;
396
+ that.expanded = true;
397
+ });
398
+ });
399
+ this.element.parent().addClass('input-append');
400
+ this.element.after(this.link_ac_toggle);
401
+ },
402
+
403
+ _createAutocomplete: function() {
404
+ var that = this;
405
+
406
+ that.element.autocomplete($.extend({
407
+ source: function(request, response) {
408
+ if( that.options.preload ) {
409
+ that.load(function(){
410
+ var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
411
+ response($.grep(that.possibleValues, function(val, i) {
412
+ return ( !that.options.multiple || !request.term || matcher.test(val.value));
413
+ }));
414
+ }, function(){
415
+ response();
416
+ });
417
+ } else { // asking server everytime
418
+ $.getJSON(that.options.source, {
419
+ term: request.term
420
+ }, function(json) {
421
+ response(that.options.rootElement ? json[that.options.rootElement] : json);
422
+ });
423
+ }
424
+ },
425
+ minLength: 0,
426
+ select: function(event, ui) {
427
+ that.selectValue(ui.item)
428
+ return false;
429
+ },
430
+ change: function(event, ui) {
431
+ if (!ui.item) {
432
+ $(this).val('');
433
+ if( !that.options.multiple ) {
434
+ that.valueElement.val('');
435
+ that.valueElement.change();
436
+ }
437
+ }
438
+ },
439
+ position: this.options.position,
440
+ autoFocus: this.options.autofocus
441
+ }, this.options.autocomplete_options)).data("ui-autocomplete")._renderItem = this.options.render_item;
442
+
443
+ this.element.click(function() {
444
+ $(this).select();
445
+ });
446
+ if( this.options.activate_on_input_click ) {
447
+ this.element.on('click', function() {
448
+ if(!that.options.preload)
449
+ that.element.focus().val('');
450
+ that.element.trigger('keydown');
451
+ that.element.autocomplete("search", that.element.val());
452
+ });
453
+ }
454
+
455
+ $("<button type='button'>&nbsp;</button>")
456
+ .attr("tabIndex", -1)
457
+ .insertAfter(that.element)
458
+ .button({
459
+ icons: {
460
+ primary: "ui-icon-triangle-1-s"
461
+ },
462
+ text: false
463
+ })
464
+ .removeClass("ui-corner-all")
465
+ .addClass("ui-corner-right ui-button-icon")
466
+ .css('font-size', '10px')
467
+ .css('margin-left', -1)
468
+ .click(function() {
469
+ if (that.element.autocomplete("widget").is(":visible")) {
470
+ that.element.autocomplete("close");
471
+ that.element.blur();
472
+ return;
473
+ }
474
+ $(this).blur();
475
+ if(!that.options.preload)
476
+ that.element.focus().val('');
477
+ that.element.trigger('keydown');
478
+ that.element.autocomplete("search", that.element.val());
479
+ });
480
+ },
481
+
482
+ _formatData: function(data) {
483
+ return $.map(data, function(elem, i){
484
+ var id, value;
485
+ if (elem instanceof Array) {
486
+ value = elem[0];
487
+ id = elem[1];
488
+ } else if (elem instanceof Object) {
489
+ value = elem.value;
490
+ id = elem.id;
491
+ } else {
492
+ id = value = elem;
493
+ }
494
+ return {value: value, id: id};
495
+ });
496
+ },
497
+
498
+ _initData: function(data) {
499
+ this.possibleValues = this._formatData(data);
500
+ this.valuesLoaded = true;
501
+
502
+ this.selectedValues = this.selectedValues ? this.selectedValues : [];
503
+ if( this.selectedValues.length == 0 && this.options.preload && this.options.select_first_value && this.possibleValues.length > 0 ) {
504
+ this.selectedValues.push(this.possibleValues[0]['id'])
505
+ }
506
+
507
+ this.setValue(this.selectedValues);
508
+ },
509
+
510
+ load: function(success, fail) {
511
+ var that = this;
512
+ if( this.valuesLoaded ) {
513
+ if( typeof success === 'function' )
514
+ success();
515
+ return;
516
+ }
517
+
518
+ if( typeof success === 'function' )
519
+ this.afterLoaded.push(success);
520
+
521
+ if( this.loading )
522
+ return;
523
+
524
+ this.loading = true;
525
+ $.ajax(this.options.source, {
526
+ dataType: 'json',
527
+ success: function(json, status, xhr) {
528
+ data = that.options.rootElement ? json[that.options.rootElement] : json
529
+ that._initData(data);
530
+ for (var i = that.afterLoaded.length - 1; i >= 0; i--) {
531
+ that.afterLoaded[i].call();
532
+ }
533
+ },
534
+ error: fail
535
+ }).always(function(){
536
+ that.loading = false;
537
+ });
538
+ },
539
+
540
+ selectValue: function(value) {
541
+ if( this.options.multiple ) {
542
+ this.valueElement.entityArray('add', {
543
+ id: value.id,
544
+ name: value.value
545
+ });
546
+ this.element.trigger('change');
547
+ this.element.val('');
548
+ } else {
549
+ this.element.val(value.value);
550
+ this.valueElement.val(value.id);
551
+ this.valueElement.change();
552
+ }
553
+ },
554
+
555
+ setValue: function(values) {
556
+ var that = this;
557
+ if( typeof values === 'undefined' || !values )
558
+ return false;
559
+
560
+ if( that.options.multiple ) {
561
+ that.valueElement.entityArray('clear');
562
+ }
563
+ if( this.options.preload ) {
564
+ this.load(function(){
565
+ <<<<<<< HEAD
566
+ that._setValues(values)
567
+ });
568
+ } else {
569
+ =======
570
+ if( that.options.multiple ) {
571
+ that.valueElement.entityArray('clear');
572
+ }
573
+ that._setValues(values);
574
+ });
575
+ } else {
576
+ if( that.options.multiple ) {
577
+ that.valueElement.entityArray('clear');
578
+ }
579
+ >>>>>>> stable
580
+ that._setValues(values);
581
+ }
582
+ },
583
+
584
+ _setValues: function(values) {
585
+ var selected = [];
586
+
587
+ if( values.length == 0 )
588
+ return false;
589
+
590
+ // allows the combination of only id values and values with label
591
+ for (var i = values.length - 1; i >= 0; i--) {
592
+ var identifier, label;
593
+ if( values[i] instanceof Object && !Array.isArray(values[i]) && values[i] !== null ) {
594
+ selected.push( values[i] );
595
+ } else if( this.options.preload && Array.isArray(this.possibleValues) ) {
596
+ for(var j = this.possibleValues.length - 1; j >= 0; j-- ) {
597
+ if ( values[i] == this.possibleValues[j].id || values[i] == this.possibleValues[j].id.toString() ) {
598
+ selected.push(this.possibleValues[j]);
599
+ break;
600
+ }
601
+ }
602
+ } else {
603
+ selected.push( {id: values[i], value: values[i]} );
604
+ }
605
+ }
606
+ for (var i = selected.length - 1; i >= 0; i--) {
607
+ if(this.options.multiple) {
608
+ this.valueElement.entityArray('add', { id: selected[i].id, name: selected[i].value });
609
+ } else {
610
+ this.element.val(selected[i].value);
611
+ this.valueElement.val(selected[i].id);
612
+ }
613
+ }
614
+ },
615
+
616
+ getValue: function(with_label) {
617
+ var result;
618
+ if ( this.options.multiple && !this.expanded ) {
619
+ <<<<<<< HEAD
620
+ result = this.valueElement.entityArray('getValue'); // entityArray
621
+ } else if ( this.options.multiple ) {
622
+ result = this.valueElement.val(); // select multiple=true
623
+ } else {
624
+ result = [this.valueElement.val()]; // hidden field
625
+ }
626
+ if( with_label ) {
627
+ result = this.possibleValues.filter(function(el) {
628
+ return result.indexOf( el.id ) >= 0;
629
+ });
630
+ =======
631
+ result = this.valueElement.entityArray('getValue'); // entityArray
632
+ } else if ( this.options.multiple ) {
633
+ result = this.valueElement.val(); // select multiple=true
634
+ } else {
635
+ result = [this.valueElement.val()]; // hidden field
636
+ }
637
+ if( with_label ) {
638
+ result = this.possibleValues.filter(function(el) {
639
+ return result.indexOf( el.id ) >= 0;
640
+ });
641
+ >>>>>>> stable
642
+ }
643
+ return result;
644
+ }
645
+
646
+ });
647
+
648
+ })(jQuery);