redmine_extensions 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ EasyGem.extend = function (deep, target, source) {
2
+ var copyIsArray;
3
+ if (typeof deep !== "boolean") {
4
+ source = target;
5
+ target = deep;
6
+ deep = false;
7
+ }
8
+ if (!source) return;
9
+ if (typeof target !== "object") {
10
+ target = {};
11
+ }
12
+ for (var name in source) {
13
+ if (!source.hasOwnProperty(name)) continue;
14
+ var trg = target[name];
15
+ var src = source[name];
16
+
17
+ // Prevent never-ending loop
18
+ if (trg === src) continue;
19
+ if (deep && src && (typeof src === "object" ||
20
+ (copyIsArray = Array.isArray(src)))) {
21
+
22
+ if (copyIsArray) {
23
+ copyIsArray = false;
24
+ var clone = trg && Array.isArray(trg) ? trg : [];
25
+
26
+ } else {
27
+ clone = trg && (typeof src === "object") ? trg : {};
28
+ }
29
+
30
+ // Never move original objects, clone them
31
+ target[name] = EASY.extend(deep, trg, src);
32
+
33
+ // Don't bring in undefined values
34
+ } else if (src !== undefined) {
35
+ target[name] = src;
36
+ }
37
+ }
38
+ return target;
39
+ };
40
+ EASY.extend = EasyGem.extend;
@@ -0,0 +1,26 @@
1
+ EasyGem.dynamic = {
2
+ jsTag: function (src) {
3
+ var jsScript = document.createElement("script");
4
+ jsScript.setAttribute("src", src);
5
+ jsScript.setAttribute("defer", "true");
6
+ document.head.appendChild(jsScript);
7
+ },
8
+ cssTag: function (src) {
9
+ var link = document.createElement('link');
10
+ link.rel = "stylesheet";
11
+ link.type = "text/css";
12
+ link.href = src;
13
+ link.media = "all";
14
+ document.head.appendChild(link);
15
+ },
16
+ jsTags: function (array, plugin) {
17
+ for (var i = 0; i < array.length; i++) {
18
+ this.jsTag(array[i], plugin);
19
+ }
20
+ },
21
+ cssTags: function (array, plugin) {
22
+ for (var i = 0; i < array.length; i++) {
23
+ this.cssTag(array[i], plugin);
24
+ }
25
+ }
26
+ };
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Created by hosekp on 11/10/16.
3
+ */
4
+ (function () {
5
+ /**
6
+ * @method fire
7
+ * @method on
8
+ * @method of
9
+ * @constructor
10
+ */
11
+ function EventBus() {
12
+ this.registeredMap = {};
13
+ }
14
+
15
+ /**
16
+ * send event to all of the listeners of specified event
17
+ * @param {String} event
18
+ * @param {...any}
19
+ */
20
+ EventBus.prototype.fire = function (event) {
21
+ var proFunctions = this.registeredMap[event];
22
+ if (!proFunctions) return;
23
+ var slicedArgs = Array.prototype.slice.call(arguments, 1);
24
+ for (var i = 0; i < proFunctions.length; i++) {
25
+ proFunctions[i].apply(this, slicedArgs);
26
+ }
27
+ };
28
+ /**
29
+ * Register listener of specified event
30
+ * @param {String} event
31
+ * @param {Function} func
32
+ */
33
+ EventBus.prototype.on = function (event, func) {
34
+ var eventList = this.registeredMap[event];
35
+ if (!eventList) this.registeredMap[event] = eventList = [];
36
+ for (var i = 0; i < eventList.length; i++) {
37
+ if (eventList[i] === func) {
38
+ return;
39
+ }
40
+ }
41
+ eventList.push(func);
42
+ };
43
+ /**
44
+ *
45
+ * @param {String} event
46
+ * @param {Function} func
47
+ */
48
+ EventBus.prototype.off = function (event, func) {
49
+ var eventList = this.registeredMap[event];
50
+ if (!eventList) return;
51
+ for (var i = 0; i < eventList.length; i++) {
52
+ if (eventList[i] === func) {
53
+ eventList.splice(i, 1);
54
+ return;
55
+ }
56
+ }
57
+ };
58
+ window.EASY = window.EASY || {};
59
+ EASY.eventBusFactory = EventBus;
60
+ EASY.eventBus = new EventBus();
61
+ })();
@@ -1,419 +1,419 @@
1
1
  REDMINE_EXTENSIONS = {
2
2
 
3
- toggleDiv: function(el_or_id) {
3
+ toggleDiv: function (el_or_id) {
4
4
  var el;
5
5
  if (typeof(el_or_id) === 'string') {
6
- el = $('#' + el_or_id);
6
+ el = $('#' + el_or_id);
7
7
  } else {
8
- el = el_or_id;
8
+ el = el_or_id;
9
9
  }
10
10
 
11
11
  el.toggleClass('collapsed').slideToggle('fast');
12
12
  },
13
13
 
14
- toggleDivAndChangeOpen: function(toggleElementId, changeOpenElement) {
14
+ toggleDivAndChangeOpen: function (toggleElementId, changeOpenElement) {
15
15
  REDMINE_EXTENSIONS.toggleDiv(toggleElementId);
16
16
  $(changeOpenElement).toggleClass('open');
17
17
  },
18
18
 
19
- toggleFilterButtons: function(elButtonsID, elFilter1ID, elFilter2ID)
20
- {
21
- var elButtons = $('#' + elButtonsID);
22
- var elFilter1 = $('#' + elFilter1ID);
23
- var elFilter2 = $('#' + elFilter2ID);
19
+ toggleFilterButtons: function (elButtonsID, elFilter1ID, elFilter2ID) {
20
+ var elButtons = $('#' + elButtonsID);
21
+ var elFilter1 = $('#' + elFilter1ID);
22
+ var elFilter2 = $('#' + elFilter2ID);
24
23
 
25
- if (elFilter1.hasClass('collapsed') && elFilter2.hasClass('collapsed')) {
26
- elButtons.slideUp('slow');
27
- } else {
28
- elButtons.slideDown('slow');
29
- }
24
+ if (elFilter1.hasClass('collapsed') && elFilter2.hasClass('collapsed')) {
25
+ elButtons.slideUp('slow');
26
+ } else {
27
+ elButtons.slideDown('slow');
28
+ }
30
29
  }
31
30
 
32
31
  };
33
32
 
34
- window.showFlashMessage = (function(type, message, delay){
35
- var $content = $("#content");
36
- $content.find(".flash").remove();
37
- var element = document.createElement("div");
38
- element.className = 'fixed flash ' + type;
39
- element.style.position = 'fixed';
40
- element.style.zIndex = '10001';
41
- element.style.right = '5px';
42
- element.style.top = '5px';
43
- element.setAttribute("onclick", "closeFlashMessage($(this))");
44
- var close = document.createElement("a");
45
- close.className = 'icon-close close-icon';
46
- close.setAttribute("href", "javascript:void(0)");
47
- close.style.float = 'right';
48
- close.style.marginLeft = '5px';
49
- // close.setAttribute("onclick", "closeFlashMessage($(this))");
50
- var span = document.createElement("span");
51
- span.innerHTML = message;
52
- element.appendChild(close);
53
- element.appendChild(span);
54
- $content.prepend(element);
55
- var $element = $(element);
56
- if(delay){
57
- setTimeout(function(){
58
- window.requestAnimationFrame(function(){
59
- closeFlashMessage($element);
60
- });
61
- }, delay);
62
- }
63
- return $element;
33
+ window.showFlashMessage = (function (type, message, delay) {
34
+ var $content = $("#content");
35
+ $content.find(".flash").remove();
36
+ var element = document.createElement("div");
37
+ element.className = 'fixed flash ' + type;
38
+ element.style.position = 'fixed';
39
+ element.style.zIndex = '10001';
40
+ element.style.right = '5px';
41
+ element.style.top = '5px';
42
+ element.setAttribute("onclick", "closeFlashMessage($(this))");
43
+ var close = document.createElement("a");
44
+ close.className = 'icon-close close-icon';
45
+ close.setAttribute("href", "javascript:void(0)");
46
+ close.style.float = 'right';
47
+ close.style.marginLeft = '5px';
48
+ // close.setAttribute("onclick", "closeFlashMessage($(this))");
49
+ var span = document.createElement("span");
50
+ span.innerHTML = message;
51
+ element.appendChild(close);
52
+ element.appendChild(span);
53
+ $content.prepend(element);
54
+ var $element = $(element);
55
+ if (delay) {
56
+ setTimeout(function () {
57
+ window.requestAnimationFrame(function () {
58
+ closeFlashMessage($element);
59
+ });
60
+ }, delay);
61
+ }
62
+ return $element;
64
63
  });
65
64
 
66
- window.closeFlashMessage = (function($element){
67
- $element.closest('.flash').fadeOut(500, function(){$element.remove();});
65
+ window.closeFlashMessage = (function ($element) {
66
+ $element.closest('.flash').fadeOut(500, function () {
67
+ $element.remove();
68
+ });
68
69
  });
69
70
 
71
+ EasyGem.schedule.require(function () {
72
+ $.widget('easy.easymultiselect', {
73
+ options: {
74
+ source: null,
75
+ rootElement: null, // rootElement in the response from source
76
+ selected: null,
77
+ multiple: true, // multiple values can be selected
78
+ preload: true, // load all possible values
79
+ position: {collision: 'flip'},
80
+ autofocus: false,
81
+ combo: false,
82
+ inputName: null, // defaults to element prop name
83
+ render_item: function (ul, item) {
84
+ return $("<li>")
85
+ .data("item.autocomplete", item)
86
+ .text(item.label)
87
+ .appendTo(ul);
88
+ },
89
+ activate_on_input_click: true,
90
+ load_immediately: false,
91
+ show_toggle_button: true,
92
+ select_first_value: true,
93
+ autocomplete_options: {}
94
+ },
95
+
96
+ _create: function () {
97
+ this.selectedValues = this.options.selected;
98
+ this._createUI();
99
+ this.expanded = false;
100
+ this.valuesLoaded = false;
101
+ this.afterLoaded = [];
102
+ if (Array.isArray(this.options.source)) {
103
+ this.options.preload = true;
104
+ this._initData(this.options.source);
105
+ } else if (this.options.preload && this.options.load_immediately) {
106
+ this.load();
107
+ } else if (this.selectedValues) {
108
+ this.setValue(this.selectedValues);
109
+ }
110
+ },
111
+
112
+ _createUI: function () {
113
+ var that = this;
114
+ this.element.wrap('<span class="easy-autocomplete-tag"></span>');
115
+ this.tag = this.element.parent();
116
+ this.inputName = this.options.inputName || this.element.prop('name');
117
+
118
+ if (this.options.multiple) { // multiple values
119
+ this.valueElement = $('<span></span>');
120
+ this.tag.after(this.valueElement);
121
+
122
+ if (this.options.show_toggle_button)
123
+ this._createToggleButton();
124
+
125
+ this.valueElement.entityArray({
126
+ inputNames: this.inputName,
127
+ afterRemove: function (entity) {
128
+ that.element.trigger('change');
129
+ }
130
+ });
131
+ } else { //single value
132
+ this.valueElement = $('<input>', {type: 'hidden', name: this.inputName});
133
+ this.element.after(this.valueElement);
134
+ }
70
135
 
71
- (function($, undefined) {
72
-
73
- $.widget('easy.easymultiselect', {
74
- options: {
75
- source: null,
76
- rootElement: null, // rootElement in the response from source
77
- selected: null,
78
- multiple: true, // multiple values can be selected
79
- preload: true, // load all possible values
80
- position: {collision: 'flip'},
81
- autofocus: false,
82
- combo: false,
83
- inputName: null, // defaults to element prop name
84
- render_item: function(ul, item) {
85
- return $("<li>")
86
- .data("item.autocomplete", item)
87
- .text(item.label)
88
- .appendTo(ul);
89
- },
90
- activate_on_input_click: true,
91
- load_immediately: false,
92
- show_toggle_button: true,
93
- select_first_value: true,
94
- autocomplete_options: {}
95
- },
96
-
97
- _create: function() {
98
- this.selectedValues = this.options.selected;
99
- this._createUI();
100
- this.expanded = false;
101
- this.valuesLoaded = false;
102
- this.afterLoaded = [];
103
- if ( Array.isArray(this.options.source) ) {
104
- this.options.preload = true;
105
- this._initData(this.options.source);
106
- } else if ( this.options.preload && this.options.load_immediately) {
107
- this.load();
108
- } else if ( this.selectedValues ) {
109
- this.setValue( this.selectedValues );
110
- }
111
- },
112
-
113
- _createUI: function() {
114
- var that = this;
115
- this.element.wrap('<span class="easy-autocomplete-tag"></span>');
116
- this.tag = this.element.parent();
117
- this.inputName = this.options.inputName || this.element.prop('name');
118
-
119
- if( this.options.multiple ) { // multiple values
120
- this.valueElement = $('<span></span>');
121
- this.tag.after(this.valueElement);
122
-
123
- if (this.options.show_toggle_button)
124
- this._createToggleButton();
125
-
126
- this.valueElement.entityArray({
127
- inputNames: this.inputName,
128
- afterRemove: function (entity) {
129
- that.element.trigger('change');
130
- }
131
- });
132
- } else { //single value
133
- this.valueElement = $('<input>', {type: 'hidden', name: this.inputName});
134
- this.element.after(this.valueElement);
135
- }
136
-
137
- this._createAutocomplete();
138
- if( !this.options.multiple ) {
139
- this.element.css('margin-right', 0);
136
+ this._createAutocomplete();
137
+ if (!this.options.multiple) {
138
+ this.element.css('margin-right', 0);
139
+ }
140
+ },
141
+
142
+ _createToggleButton: function () {
143
+ var that = this;
144
+ this.link_ac_toggle = $('<a>').attr('class', 'icon icon-add clear-link');
145
+ this.link_ac_toggle.click(function (evt) {
146
+ var $elem = $(this);
147
+ evt.preventDefault();
148
+ that.load(function () {
149
+ var select = $('<select>').prop('multiple', true).prop('size', 5).prop('name', that.inputName);
150
+ var option;
151
+ $.each(that.possibleValues, function (i, v) {
152
+ option = $('<option>').prop('value', v.id).text(v.value);
153
+ option.prop('selected', that.getValue().indexOf(v.id) > -1);
154
+ select.append(option);
155
+ });
156
+ var $container = $elem.closest('.easy-multiselect-tag-container');
157
+ $container.find(':input').prop('disabled', true);
158
+ $container.children().hide();
159
+ $container.append(select);
160
+ that.valueElement = select;
161
+ that.expanded = true;
162
+ });
163
+ });
164
+ this.element.parent().addClass('input-append');
165
+ this.element.after(this.link_ac_toggle);
166
+ },
167
+
168
+ _createAutocomplete: function () {
169
+ var that = this;
170
+
171
+ that.element.autocomplete($.extend({
172
+ source: function (request, response) {
173
+ if (that.options.preload) {
174
+ that.load(function () {
175
+ var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
176
+ response($.grep(that.possibleValues, function (val, i) {
177
+ return (!request.term || matcher.test(val.value));
178
+ }));
179
+ }, function () {
180
+ response();
181
+ });
182
+ } else { // asking server everytime
183
+ if (typeof that.options.source === 'function') {
184
+ that.options.source(function (json) {
185
+ response(that.options.rootElement ? json[that.options.rootElement] : json);
186
+ });
187
+ } else {
188
+ $.getJSON(that.options.source, {
189
+ term: request.term
190
+ }, function (json) {
191
+ response(that.options.rootElement ? json[that.options.rootElement] : json);
192
+ });
140
193
  }
194
+ }
141
195
  },
142
-
143
- _createToggleButton: function() {
144
- var that = this;
145
- this.link_ac_toggle = $('<a>').attr('class', 'icon icon-add clear-link');
146
- this.link_ac_toggle.click(function(evt) {
147
- var $elem = $(this);
148
- evt.preventDefault();
149
- that.load(function(){
150
- var select = $('<select>').prop('multiple', true).prop('size', 5).prop('name', that.inputName);
151
- var option;
152
- $.each(that.possibleValues, function(i, v) {
153
- option = $('<option>').prop('value', v.id).text(v.value);
154
- option.prop('selected', that.getValue().indexOf(v.id) > -1);
155
- select.append(option);
156
- });
157
- var $container = $elem.closest('.easy-multiselect-tag-container');
158
- $container.find(':input').prop('disabled', true);
159
- $container.children().hide();
160
- $container.append(select);
161
- that.valueElement = select;
162
- that.expanded = true;
163
- });
164
- });
165
- this.element.parent().addClass('input-append');
166
- this.element.after(this.link_ac_toggle);
196
+ minLength: 0,
197
+ select: function (event, ui) {
198
+ that.selectValue(ui.item);
199
+ return false;
167
200
  },
168
-
169
- _createAutocomplete: function() {
170
- var that = this;
171
-
172
- that.element.autocomplete($.extend({
173
- source: function(request, response) {
174
- if( that.options.preload ) {
175
- that.load(function(){
176
- var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
177
- response($.grep(that.possibleValues, function(val, i) {
178
- return ( !request.term || matcher.test(val.value) );
179
- }));
180
- }, function(){
181
- response();
182
- });
183
- } else { // asking server everytime
184
- if( typeof that.options.source === 'function' ) {
185
- that.options.source(function(json){
186
- response(that.options.rootElement ? json[that.options.rootElement] : json);
187
- });
188
- } else {
189
- $.getJSON(that.options.source, {
190
- term: request.term
191
- }, function(json) {
192
- response(that.options.rootElement ? json[that.options.rootElement] : json);
193
- });
194
- }
195
- }
196
- },
197
- minLength: 0,
198
- select: function(event, ui) {
199
- that.selectValue(ui.item);
200
- return false;
201
- },
202
- change: function(event, ui) {
203
- if (!ui.item) {
204
- if (that.options.combo) {
205
- $(this).val(that.element.val());
206
- if (!that.options.multiple) {
207
- that.valueElement.val(that.element.val());
208
- that.valueElement.change();
209
- }
210
- } else {
211
- $(this).val('');
212
- if (!that.options.multiple) {
213
- that.valueElement.val('');
214
- that.valueElement.change();
215
- }
216
- }
217
- }
218
- },
219
- position: this.options.position,
220
- autoFocus: this.options.autofocus
221
- }, this.options.autocomplete_options)).data("ui-autocomplete")._renderItem = this.options.render_item;
222
-
223
- this.element.click(function() {
224
- $(this).select();
225
- });
226
- if( this.options.activate_on_input_click ) {
227
- this.element.on('click', function() {
228
- if(!that.options.preload)
229
- that.element.focus().val('');
230
- that.element.trigger('keydown');
231
- that.element.autocomplete("search", that.element.val());
232
- });
201
+ change: function (event, ui) {
202
+ if (!ui.item) {
203
+ if (that.options.combo) {
204
+ $(this).val(that.element.val());
205
+ if (!that.options.multiple) {
206
+ that.valueElement.val(that.element.val());
207
+ that.valueElement.change();
208
+ }
209
+ } else {
210
+ $(this).val('');
211
+ if (!that.options.multiple) {
212
+ that.valueElement.val('');
213
+ that.valueElement.change();
214
+ }
233
215
  }
234
-
235
- $("<button type='button'>&nbsp;</button>")
236
- .attr("tabIndex", -1)
237
- .insertAfter(that.element)
238
- .button({
239
- icons: {
240
- primary: "ui-icon-triangle-1-s"
241
- },
242
- text: false
243
- })
244
- .removeClass("ui-corner-all")
245
- .addClass("ui-corner-right ui-button-icon")
246
- .css('font-size', '10px')
247
- .css('margin-left', -1)
248
- .click(function() {
249
- if (that.element.autocomplete("widget").is(":visible")) {
250
- that.element.autocomplete("close");
251
- that.element.blur();
252
- return;
253
- }
254
- $(this).blur();
255
- if(!that.options.preload)
256
- that.element.focus().val('');
257
- that.element.trigger('keydown');
258
- that.element.autocomplete("search", that.element.val());
259
- });
216
+ }
260
217
  },
218
+ position: this.options.position,
219
+ autoFocus: this.options.autofocus
220
+ }, this.options.autocomplete_options)).data("ui-autocomplete")._renderItem = this.options.render_item;
221
+
222
+ this.element.click(function () {
223
+ $(this).select();
224
+ });
225
+ if (this.options.activate_on_input_click) {
226
+ this.element.on('click', function () {
227
+ if (!that.options.preload)
228
+ that.element.focus().val('');
229
+ that.element.trigger('keydown');
230
+ that.element.autocomplete("search", that.element.val());
231
+ });
232
+ }
261
233
 
262
- _formatData: function(data) {
263
- return $.map(data, function(elem, i){
264
- var id, value;
265
- if (elem instanceof Array) {
266
- value = elem[0];
267
- id = elem[1];
268
- } else if (elem instanceof Object) {
269
- value = elem.value;
270
- id = elem.id;
271
- } else {
272
- id = value = elem;
273
- }
274
- return {value: value, id: id};
275
- });
276
- },
234
+ $("<button type='button'>&nbsp;</button>")
235
+ .attr("tabIndex", -1)
236
+ .insertAfter(that.element)
237
+ .button({
238
+ icons: {
239
+ primary: "ui-icon-triangle-1-s"
240
+ },
241
+ text: false
242
+ })
243
+ .removeClass("ui-corner-all")
244
+ .addClass("ui-corner-right ui-button-icon")
245
+ .css('font-size', '10px')
246
+ .css('margin-left', -1)
247
+ .click(function () {
248
+ if (that.element.autocomplete("widget").is(":visible")) {
249
+ that.element.autocomplete("close");
250
+ that.element.blur();
251
+ return;
252
+ }
253
+ $(this).blur();
254
+ that.element.focus().val('');
255
+ that.element.trigger('keydown');
256
+ that.element.autocomplete("search", that.element.val());
257
+ });
258
+ },
259
+
260
+ _formatData: function (data) {
261
+ return $.map(data, function (elem, i) {
262
+ var id, value;
263
+ if (elem instanceof Array) {
264
+ value = elem[0];
265
+ id = elem[1];
266
+ } else if (elem instanceof Object) {
267
+ value = elem.value;
268
+ id = elem.id;
269
+ } else {
270
+ id = value = elem;
271
+ }
272
+ return {value: value, id: id};
273
+ });
274
+ },
277
275
 
278
- _initData: function(data) {
279
- this.possibleValues = this._formatData(data);
280
- this.valuesLoaded = true;
276
+ _initData: function (data) {
277
+ this.possibleValues = this._formatData(data);
278
+ this.valuesLoaded = true;
281
279
 
282
- this.selectedValues = this.selectedValues ? this.selectedValues : [];
283
- if( this.selectedValues.length === 0 && this.options.preload && this.options.select_first_value && this.possibleValues.length > 0 ) {
284
- this.selectedValues.push(this.possibleValues[0]['id']);
285
- }
280
+ this.selectedValues = this.selectedValues ? this.selectedValues : [];
281
+ if (this.selectedValues.length === 0 && this.options.preload && this.options.select_first_value && this.possibleValues.length > 0) {
282
+ this.selectedValues.push(this.possibleValues[0]['id']);
283
+ }
286
284
 
287
- this.setValue(this.selectedValues);
288
- },
285
+ this.setValue(this.selectedValues);
286
+ },
289
287
 
290
- load: function(success, fail) {
291
- var that = this;
292
- if( this.valuesLoaded ) {
293
- if( typeof success === 'function' )
294
- success();
295
- return;
296
- }
288
+ load: function (success, fail) {
289
+ var that = this;
290
+ if (this.valuesLoaded) {
291
+ if (typeof success === 'function')
292
+ success();
293
+ return;
294
+ }
297
295
 
298
- if( typeof success === 'function' )
299
- this.afterLoaded.push(success);
300
-
301
- if( this.loading )
302
- return;
303
-
304
- this.loading = true;
305
- function successFce(json, status, xhr) {
306
- var data = that.options.rootElement ? json[that.options.rootElement] : json;
307
- if( !data && window.console ) {
308
- console.warn('Data could not be loaded! Please check the datasource.');
309
- data = [];
310
- }
311
- that._initData(data);
312
- for (var i = that.afterLoaded.length - 1; i >= 0; i--) {
313
- that.afterLoaded[i].call(that);
314
- }
315
- that.loading = false;
316
- }
317
- if( typeof this.options.source === 'function' ) {
318
- this.options.source(successFce);
319
- } else {
320
- $.ajax(this.options.source, {
321
- dataType: 'json',
322
- success: successFce,
323
- error: fail
324
- }).always(function(){
325
- that.loading = false; //even if ajax fails
326
- });
327
- }
328
- },
296
+ if (typeof success === 'function')
297
+ this.afterLoaded.push(success);
329
298
 
330
- selectValue: function(value) {
331
- if( this.options.multiple ) {
332
- this.valueElement.entityArray('add', {
333
- id: value.id,
334
- name: value.value
335
- });
336
- this.element.trigger('change');
337
- this.element.val('');
338
- } else {
339
- this.element.val(value.value);
340
- this.valueElement.val(value.id);
341
- this.valueElement.change();
342
- this.element.change();
343
- }
344
- },
299
+ if (this.loading)
300
+ return;
345
301
 
346
- setValue: function(values) {
347
- var that = this;
348
- if( typeof values === 'undefined' || !values )
349
- return false;
350
-
351
- if( this.options.preload ) {
352
- this.load(function(){
353
- if( that.options.multiple ) {
354
- that.valueElement.entityArray('clear');
355
- }
356
- that._setValues(values);
357
- });
358
- } else {
359
- if( that.options.multiple ) {
360
- that.valueElement.entityArray('clear');
361
- }
362
- that._setValues(values);
363
- }
364
- },
302
+ this.loading = true;
365
303
 
366
- _setValues: function(values) {
367
- var selected = [];
368
-
369
- if( values.length === 0 )
370
- return false;
371
-
372
- // allows the combination of only id values and values with label
373
- for (var i = values.length - 1; i >= 0; i--) {
374
- if( values[i] instanceof Object && !Array.isArray(values[i]) && values[i] !== null ) {
375
- selected.push( values[i] );
376
- } else if( this.options.preload ) {
377
- var that = this;
378
- if( !Array.isArray(that.possibleValues) )
379
- return;
380
- for(var j = that.possibleValues.length - 1; j >= 0; j-- ) {
381
- if ( values[i] === that.possibleValues[j].id || values[i] === that.possibleValues[j].id.toString() ) {
382
- selected.push(that.possibleValues[j]);
383
- break;
384
- }
385
- }
386
- } else {
387
- selected.push( {id: values[i], value: values[i]} );
388
- }
389
- }
390
- for (var i = selected.length - 1; i >= 0; i--) {
391
- if(this.options.multiple) {
392
- this.valueElement.entityArray('add', { id: selected[i].id, name: selected[i].value });
393
- } else {
394
- this.element.val(selected[i].value);
395
- this.valueElement.val(selected[i].id);
396
- }
397
- }
398
- },
304
+ function successFce(json, status, xhr) {
305
+ var data = that.options.rootElement ? json[that.options.rootElement] : json;
306
+ if (!data && window.console) {
307
+ console.warn('Data could not be loaded! Please check the datasource.');
308
+ data = [];
309
+ }
310
+ that._initData(data);
311
+ for (var i = that.afterLoaded.length - 1; i >= 0; i--) {
312
+ that.afterLoaded[i].call(that);
313
+ }
314
+ that.loading = false;
315
+ }
399
316
 
400
- getValue: function(with_label) {
401
- var result;
402
- if ( this.options.multiple && !this.expanded ) {
403
- result = this.valueElement.entityArray('getValue'); // entityArray
404
- } else if ( this.options.multiple ) {
405
- result = this.valueElement.val(); // select multiple=true
406
- } else {
407
- result = [this.valueElement.val()]; // hidden field
408
- }
409
- if( with_label ) {
410
- result = this.possibleValues.filter(function(el) {
411
- return result.indexOf( el.id ) >= 0;
412
- });
317
+ if (typeof this.options.source === 'function') {
318
+ this.options.source(successFce);
319
+ } else {
320
+ $.ajax(this.options.source, {
321
+ dataType: 'json',
322
+ success: successFce,
323
+ error: fail
324
+ }).always(function () {
325
+ that.loading = false; //even if ajax fails
326
+ });
327
+ }
328
+ },
329
+
330
+ selectValue: function (value) {
331
+ if (this.options.multiple) {
332
+ this.valueElement.entityArray('add', {
333
+ id: value.id,
334
+ name: value.value
335
+ });
336
+ this.element.trigger('change');
337
+ this.element.val('');
338
+ } else {
339
+ this.element.val(value.value);
340
+ this.valueElement.val(value.id);
341
+ this.valueElement.change();
342
+ this.element.change();
343
+ }
344
+ },
345
+
346
+ setValue: function (values) {
347
+ var that = this;
348
+ if (typeof values === 'undefined' || !values)
349
+ return false;
350
+
351
+ if (this.options.preload) {
352
+ this.load(function () {
353
+ if (that.options.multiple) {
354
+ that.valueElement.entityArray('clear');
355
+ }
356
+ that._setValues(values);
357
+ });
358
+ } else {
359
+ if (that.options.multiple) {
360
+ that.valueElement.entityArray('clear');
361
+ }
362
+ that._setValues(values);
363
+ }
364
+ },
365
+
366
+ _setValues: function (values) {
367
+ var selected = [];
368
+
369
+ if (values.length === 0)
370
+ return false;
371
+
372
+ // allows the combination of only id values and values with label
373
+ for (var i = values.length - 1; i >= 0; i--) {
374
+ if (values[i] instanceof Object && !Array.isArray(values[i]) && values[i] !== null) {
375
+ selected.push(values[i]);
376
+ } else if (this.options.preload) {
377
+ var that = this;
378
+ if (!Array.isArray(that.possibleValues))
379
+ return;
380
+ for (var j = that.possibleValues.length - 1; j >= 0; j--) {
381
+ if (values[i] === that.possibleValues[j].id || values[i] === that.possibleValues[j].id.toString()) {
382
+ selected.push(that.possibleValues[j]);
383
+ break;
413
384
  }
414
- return result;
385
+ }
386
+ } else {
387
+ selected.push({id: values[i], value: values[i]});
415
388
  }
389
+ }
390
+ for (var i = selected.length - 1; i >= 0; i--) {
391
+ if (this.options.multiple) {
392
+ this.valueElement.entityArray('add', {id: selected[i].id, name: selected[i].value});
393
+ } else {
394
+ this.element.val(selected[i].value);
395
+ this.valueElement.val(selected[i].id);
396
+ }
397
+ }
398
+ },
399
+
400
+ getValue: function (with_label) {
401
+ var result;
402
+ if (this.options.multiple && !this.expanded) {
403
+ result = this.valueElement.entityArray('getValue'); // entityArray
404
+ } else if (this.options.multiple) {
405
+ result = this.valueElement.val(); // select multiple=true
406
+ } else {
407
+ result = [this.valueElement.val()]; // hidden field
408
+ }
409
+ if (with_label) {
410
+ result = this.possibleValues.filter(function (el) {
411
+ return result.indexOf(el.id) >= 0;
412
+ });
413
+ }
414
+ return result;
415
+ }
416
416
 
417
- });
417
+ });
418
418
 
419
- })(jQuery);
419
+ }, 'jQueryUI');