aboutsummaryrefslogtreecommitdiffstats
path: root/admin/themes/default/js/addAlbum.js
blob: 02fb25f0d9c5282b021cbc8e2b59ddca48f61243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
jQuery.fn.pwgAddAlbum = function(options) {
  if (!options.cache) {
    jQuery.error('pwgAddAlbum: missing categories cache');
  }
  
  var $popup = jQuery('#addAlbumForm');
  
  function init() {
    if ($popup.data('init')) {
      return;
    }
    $popup.data('init', true);
    
    options.cache.selectize($popup.find('[name="category_parent"]'), {
      'default': 0,
      'filter': function(categories) {
        categories.push({
          id: 0,
          fullname: '------------',
          global_rank: 0
        });
        
        return categories;
      }
    });
    
    $popup.find('form').on('submit', function(e) {
      e.preventDefault();
      
      jQuery('#categoryNameError').text('');
      
      var albumParent = $popup.find('[name="category_parent"]'),
          parent_id = albumParent.val(),
          name = $popup.find('[name=category_name]').val(),
          target = $popup.data('target');

      jQuery.ajax({
        url: 'ws.php?format=json',
        type: 'POST',
        dataType: 'json',
        data: {
          method: 'pwg.categories.add',
          parent: parent_id,
          name: name
        },
        beforeSend: function() {
          jQuery('#albumCreationLoading').show();
        },
        success: function(data) {
          jQuery('#albumCreationLoading').hide();
          jQuery('[data-add-album="'+ target +'"]').colorbox.close();

          var newAlbum = data.result.id,
              newAlbum_name = '',
              newAlbum_rank = '0';
              
          if (parent_id != 0) {
            newAlbum_name = albumParent[0].selectize.options[parent_id].fullname +' / ';
            newAlbum_rank = albumParent[0].selectize.options[parent_id].global_rank +'.1';
          }
          newAlbum_name+= name;
          
          var $albumSelect = jQuery('[name="'+ target +'"]');
          
          // target is a normal select
          if (!$albumSelect[0].selectize) {
            var new_option = jQuery('<option/>')
                .attr('value', newAlbum)
                .attr('selected', 'selected')
                .text(newAlbum_name);

            $albumSelect.find('option').removeAttr('selected');
            
            if (parent_id==0) {
              $albumSelect.prepend(new_option);
            }
            else {
              $albumSelect.find('option[value='+ parent_id +']').after(new_option);
            }
          }
          // target is selectize
          else {
            var selectize = $albumSelect[0].selectize;
            
            if (jQuery.isEmptyObject(selectize.options)) {
              options.cache.clear();
              options.cache.selectize($albumSelect, {
                'default': newAlbum,
                'value': newAlbum
              });
            }
            else {
              $albumSelect[0].selectize.addOption({
                id: newAlbum,
                fullname: newAlbum_name,
                global_rank: newAlbum_rank
              });
              
              $albumSelect[0].selectize.setValue(newAlbum);
            }
          }

          albumParent.val('');
          jQuery('#albumSelection, .selectFiles, .showFieldset').show();
        },
        error: function(XMLHttpRequest, textStatus, errorThrows) {
            jQuery('#albumCreationLoading').hide();
            jQuery('#categoryNameError').text(errorThrows).css('color', 'red');
        }
      });
    });
  }
  
  this.colorbox({
    inline: true,
    href: '#addAlbumForm',
    width: 350, height: 300,
    onComplete: function() {
      init();
      $popup.data('target', jQuery(this).data('addAlbum'));
      $popup.find('[name=category_name]').focus();
    }
  });
  
  return this;
};