aboutsummaryrefslogtreecommitdiffstats
path: root/admin/themes/default/js/addAlbum.js
blob: b30c2bc5037f4fe93305d08d681362dfcb7f8bfb (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
jQuery.fn.pwgAddAlbum = function(options) {
  options = options || {};

  var $popup = jQuery('#addAlbumForm'),
      $albumParent = $popup.find('[name="category_parent"]')
      $button = jQuery(this),
      $target = jQuery('[name="'+ $button.data('addAlbum') +'"]'),
      cache = $target.data('cache');

  if (!$target[0].selectize) {
    jQuery.error('pwgAddAlbum: target must use selectize');
  }
  if (!cache) {
    jQuery.error('pwgAddAlbum: missing categories cache');
  }

  function init() {
    $popup.data('init', true);

    cache.selectize($albumParent, {
      'default': 0,
      'filter': function(categories) {
        categories.push({
          id: 0,
          fullname: '------------',
          global_rank: 0
        });

        if (options.filter) {
          categories = options.filter.call(this, categories);
        }

        return categories;
      }
    });

    $popup.find('form').on('submit', function(e) {
      e.preventDefault();

      var parent_id = $albumParent.val(),
          name = $popup.find('[name=category_name]').val();
          
      jQuery('#categoryNameError').toggle(!name);
      if (!name) {
        return;
      }

      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();
          $button.colorbox.close();

          var newAlbum = {
            id: data.result.id,
            name: name,
            fullname: name,
            global_rank: '0',
            dir: null,
            nb_images: 0,
            pos: 0
          };

          var parentSelectize = $albumParent[0].selectize;

          if (parent_id != 0) {
            var parent = parentSelectize.options[parent_id];
            newAlbum.fullname = parent.fullname + ' / ' + newAlbum.fullname;
            newAlbum.global_rank = parent.global_rank + '.1';
            newAlbum.pos = parent.pos + 1;
          }

          var targetSelectize = $target[0].selectize;
          targetSelectize.addOption(newAlbum);
          targetSelectize.setValue(newAlbum.id);

          parentSelectize.addOption(newAlbum);

          if (options.afterSelect) {
            options.afterSelect();
          }
        },
        error: function(XMLHttpRequest, textStatus, errorThrows) {
            jQuery('#albumCreationLoading').hide();
            alert(errorThrows);
        }
      });
    });
  }

  this.colorbox({
    inline: true,
    href: '#addAlbumForm',
    width: 650, height: 300,
    onComplete: function() {
      if (!$popup.data('init')) {
        init();
      }

      jQuery('#categoryNameError').hide();
      $popup.find('[name=category_name]').val('').focus();
      $albumParent[0].selectize.setValue($target.val() || 0);
    }
  });

  return this;
};