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