1
|
|
|
/** |
2
|
|
|
* @package assets |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
(function($) { |
6
|
|
|
'use strict'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Pickable allows to show and hide elements based on the value of a select box. |
10
|
|
|
* |
11
|
|
|
* Each option is mapped to its associated content by matching the option `value` |
12
|
|
|
* with the content `id`. If the option value is numeric, Pickable prefices it |
13
|
|
|
* with `choice`. Only the content of the currently selected option is |
14
|
|
|
* shown, all other elements associated with the given select box are hidden. |
15
|
|
|
* |
16
|
|
|
* If no content element of the given `id` is found, Pickable checks for a |
17
|
|
|
* `data-request` attribute on the selected option. If a `data-request` URL is set, |
18
|
|
|
* Pickable tries to fetch the content remotely and expects an content element with |
19
|
|
|
* no additional markup in return. |
20
|
|
|
* |
21
|
|
|
* @name $.symphonyPickable |
22
|
|
|
* @class |
23
|
|
|
* |
24
|
|
|
* @param {Object} options An object containing the element selectors specified below |
25
|
|
|
* @param {String} [options.content='#contents'] Selector to find the container that wraps all pickable elements |
26
|
|
|
* @param {String} [options.pickables='.pickable'] Selector used to find pickable elements |
27
|
|
|
* |
28
|
|
|
* @example |
29
|
|
|
|
30
|
|
|
$('.picker').symphonyPickable(); |
31
|
|
|
*/ |
32
|
|
|
$.fn.symphonyPickable = function(options) { |
33
|
|
|
var objects = $(this), |
34
|
|
|
settings = { |
35
|
|
|
content: '#contents', |
36
|
|
|
pickables: '.pickable' |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
$.extend(settings, options); |
40
|
|
|
|
41
|
|
|
/*------------------------------------------------------------------------- |
42
|
|
|
Events |
43
|
|
|
-------------------------------------------------------------------------*/ |
44
|
|
|
|
45
|
|
|
// Switch content |
46
|
|
|
objects.on('change.pickable', function pick() { |
47
|
|
|
var object = $(this), |
48
|
|
|
choice = object.val(), |
49
|
|
|
relation = object.attr('id') || object.attr('name'), |
50
|
|
|
related = $(settings.pickables + '[data-relation="' + relation + '"]'), |
51
|
|
|
picked, request; |
52
|
|
|
|
53
|
|
|
// Handle numeric values |
54
|
|
|
if($.isNumeric(choice) === true) { |
55
|
|
|
choice = 'choice' + choice; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Hide all choices |
59
|
|
|
object.trigger('pickstart.pickable'); |
60
|
|
|
related.hide().find('input, select, textarea').prop('readonly', true); |
61
|
|
|
|
62
|
|
|
// Selection found |
63
|
|
|
picked = $('#' + choice); |
64
|
|
|
if(picked.length > 0) { |
65
|
|
|
picked.show().find('input, select, textarea').prop('readonly', false).trigger('pick.pickable'); |
66
|
|
|
object.trigger('pickstop.pickable'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Selection not found |
70
|
|
|
else { |
71
|
|
|
request = object.data('request'); |
72
|
|
|
|
73
|
|
|
// Fetch picked element |
74
|
|
|
if(request) { |
75
|
|
|
$.ajax({ |
76
|
|
|
type: 'GET', |
77
|
|
|
url: request, |
78
|
|
|
data: { 'choice': choice }, |
79
|
|
|
dataType: 'html', |
80
|
|
|
success: function(remote) { |
81
|
|
|
content.append(remote); |
82
|
|
|
remote.trigger('pick.pickable'); |
83
|
|
|
object.trigger('pickstop.pickable'); |
84
|
|
|
} |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
/*------------------------------------------------------------------------- |
91
|
|
|
Initialisation |
92
|
|
|
-------------------------------------------------------------------------*/ |
93
|
|
|
|
94
|
|
|
var content = $(settings.content); |
95
|
|
|
|
96
|
|
|
// Set up relationships |
97
|
|
|
objects.each(function init() { |
98
|
|
|
var object = $(this), |
99
|
|
|
relation = object.attr('id') || object.attr('name'); |
100
|
|
|
|
101
|
|
|
object.find('option').each(function() { |
102
|
|
|
$('#' + $(this).val()).attr('data-relation', relation); |
103
|
|
|
}); |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
// Show picked content |
107
|
|
|
objects.trigger('change.pickable'); |
108
|
|
|
|
109
|
|
|
/*-----------------------------------------------------------------------*/ |
110
|
|
|
|
111
|
|
|
return objects; |
112
|
|
|
}; |
113
|
|
|
|
114
|
|
|
})(window.jQuery); |
115
|
|
|
|