1 | jQuery.fn.pwgAddAlbum = function(options) { |
---|
2 | if (!options.cache) { |
---|
3 | jQuery.error('pwgAddAlbum: missing categories cache'); |
---|
4 | } |
---|
5 | |
---|
6 | var $popup = jQuery('#addAlbumForm'); |
---|
7 | if (!$popup.data('init')) { |
---|
8 | $popup.find('[name="category_parent"]').selectize({ |
---|
9 | valueField: 'id', |
---|
10 | labelField: 'fullname', |
---|
11 | sortField: 'fullname', |
---|
12 | searchField: ['fullname'], |
---|
13 | plugins: ['remove_button'], |
---|
14 | onInitialize: function() { |
---|
15 | this.on('dropdown_close', function() { |
---|
16 | if (this.getValue() == '') { |
---|
17 | this.setValue(0); |
---|
18 | } |
---|
19 | }); |
---|
20 | } |
---|
21 | }); |
---|
22 | |
---|
23 | $popup.find('form').on('submit', function(e) { |
---|
24 | e.preventDefault(); |
---|
25 | |
---|
26 | jQuery('#categoryNameError').text(''); |
---|
27 | |
---|
28 | var albumParent = $popup.find('[name="category_parent"]'), |
---|
29 | parent_id = albumParent.val(), |
---|
30 | name = $popup.find('[name=category_name]').val(), |
---|
31 | target = $popup.data('target'); |
---|
32 | |
---|
33 | jQuery.ajax({ |
---|
34 | url: 'ws.php?format=json', |
---|
35 | type: 'POST', |
---|
36 | dataType: 'json', |
---|
37 | data: { |
---|
38 | method: 'pwg.categories.add', |
---|
39 | parent: parent_id, |
---|
40 | name: name |
---|
41 | }, |
---|
42 | beforeSend: function() { |
---|
43 | jQuery('#albumCreationLoading').show(); |
---|
44 | }, |
---|
45 | success: function(data) { |
---|
46 | jQuery('#albumCreationLoading').hide(); |
---|
47 | jQuery('[data-add-album="'+ target +'"]').colorbox.close(); |
---|
48 | |
---|
49 | var newAlbum = data.result.id, |
---|
50 | newAlbum_name = ''; |
---|
51 | |
---|
52 | if (parent_id != 0) { |
---|
53 | newAlbum_name = albumParent[0].selectize.options[parent_id].fullname +' / '; |
---|
54 | } |
---|
55 | newAlbum_name+= name; |
---|
56 | |
---|
57 | var $albumSelect = jQuery('[name="'+ target +'"]'); |
---|
58 | |
---|
59 | // target is a normal select |
---|
60 | if (!$albumSelect[0].selectize) { |
---|
61 | var new_option = jQuery('<option/>') |
---|
62 | .attr('value', newAlbum) |
---|
63 | .attr('selected', 'selected') |
---|
64 | .text(newAlbum_name); |
---|
65 | |
---|
66 | $albumSelect.find('option').removeAttr('selected'); |
---|
67 | |
---|
68 | if (parent_id==0) { |
---|
69 | $albumSelect.prepend(new_option); |
---|
70 | } |
---|
71 | else { |
---|
72 | $albumSelect.find('option[value='+ parent_id +']').after(new_option); |
---|
73 | } |
---|
74 | } |
---|
75 | // target is selectize |
---|
76 | else { |
---|
77 | $albumSelect[0].selectize.addOption({ |
---|
78 | id: newAlbum, |
---|
79 | fullname: newAlbum_name |
---|
80 | }); |
---|
81 | |
---|
82 | $albumSelect[0].selectize.setValue(newAlbum); |
---|
83 | } |
---|
84 | |
---|
85 | albumParent.val(''); |
---|
86 | jQuery('#albumSelection').show(); |
---|
87 | }, |
---|
88 | error: function(XMLHttpRequest, textStatus, errorThrows) { |
---|
89 | jQuery('#albumCreationLoading').hide(); |
---|
90 | jQuery('#categoryNameError').text(errorThrows).css('color', 'red'); |
---|
91 | } |
---|
92 | }); |
---|
93 | }); |
---|
94 | } |
---|
95 | |
---|
96 | this.colorbox({ |
---|
97 | inline: true, |
---|
98 | href: '#addAlbumForm', |
---|
99 | width: 350, height: 300, |
---|
100 | onComplete: function() { |
---|
101 | var albumParent = $popup.find('[name="category_parent"]')[0]; |
---|
102 | |
---|
103 | $popup.data('target', jQuery(this).data('addAlbum')); |
---|
104 | |
---|
105 | albumParent.selectize.clearOptions(); |
---|
106 | |
---|
107 | options.cache.get(function(categories) { |
---|
108 | categories.push({ |
---|
109 | id: 0, |
---|
110 | fullname: '------------' |
---|
111 | }); |
---|
112 | |
---|
113 | albumParent.selectize.load(function(callback) { |
---|
114 | callback(categories); |
---|
115 | }); |
---|
116 | |
---|
117 | albumParent.selectize.setValue(0); |
---|
118 | }); |
---|
119 | } |
---|
120 | }); |
---|
121 | |
---|
122 | return this; |
---|
123 | }; |
---|