aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/AdminTools/template/admin_controller.js
blob: 73f7b1096e5191caac64bc45a2d088ab0455a8d5 (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
var AdminTools = function($) {
  var __this = this;

  this.urlWS;
  this.urlSelf;
  this.multiView;

  var $ato = $('#ato_container');

  // fill multiview selects
  // data came from AJAX request or sessionStorage
  function populateMultiView() {
    var $multiview = $ato.find('.multiview');

    if ($multiview.data('init')) return;

    var render = function(data) {
      var html = '';
      $.each(data.users, function(i, user) {
        if (user.status == 'webmaster' || user.status == 'admin') {
          html+= '<option value="'+ user.id +'">'+ user.username +'</option>';
        }
      });
      $multiview.find('select[data-type="view_as"]').html(html)
        .val(__this.multiView.view_as);

      html = '';
      $.each(['clear','roma'], function(i, theme) {
        html+= '<option value="'+ theme +'">'+ theme +'</option>';
      });
      $multiview.find('select[data-type="theme"]').html(html)
        .val(__this.multiView.theme);

      html = '';
      $.each(data.languages, function(i, language) {
        html+= '<option value="'+ language.id +'">'+ language.name +'</option>';
      });
      $multiview.find('select[data-type="lang"]').html(html)
        .val(__this.multiView.lang);

      $multiview.data('init', true);

      $multiview.find('.switcher').show();
    };

    if ('sessionStorage' in window && window.sessionStorage.multiView != undefined) {
      render(JSON.parse(window.sessionStorage.multiView));
    }
    else {
      $.ajax({
        method: 'POST',
        url: __this.urlWS + 'multiView.getData',
        dataType: 'json',
        success: function(data) {
          render(data.result);
          if ('sessionStorage' in window) {
            window.sessionStorage.multiView = JSON.stringify(data.result);
          }
        },
        error: function(xhr, text, error) {
          alert(text + ' ' + error);
        }
      });
    }
  }

  // delete session cache
  this.deleteCache = function() {
    if ('sessionStorage' in window) {
      window.sessionStorage.removeItem('multiView');
    }
  };

  // attach jquery handlers
  this.init = function(open) {
    $('.multiview').appendTo($ato);

    /* <!-- sub menus --> */
    $ato.on({
      'click': function(e) {
        populateMultiView();
        $(this).find('ul').toggle();
      },
      'mouseleave': function(e) {
        if (e.target.tagName.toLowerCase() != "select") {
          $(this).find('ul').hide();
        }
      }
    });
    $ato.find('>a').on('click', function(e) {
      e.preventDefault();
    });
    $ato.find('ul').on('mouseleave', function(e) {
      if (e.target.tagName.toLowerCase() != "select") {
        $(this).hide();
      }
    });

    /* <!-- select boxes --> */
    $ato.find('.switcher').on({
      'change': function() {
        if ($(this).data('type') == 'theme') {
          if ($(this).val() != __this.multiView.theme) {
            window.location.href = __this.urlSelf + 'change_theme=1';
          }
        }
        else {
          window.location.href = __this.urlSelf + 'ato_'+ $(this).data('type') +'='+ $(this).val();
        }
      },
      'click': function(e) {
        e.stopPropagation();
      }
    });
  };

  return this;
}(jQuery);