1.9.x compatibility
[imagecompressor.git] / inc / ImageCompressor.js
1 Object.keys = Object.keys || function(obj) {
2         if (obj !== Object(obj)) throw new TypeError('Object.keys called on non-object');
3         var keys = [];
4         for (var key in obj) if (_.has(obj, key)) keys.push(key);
5         return keys;
6 };
7 var ImageCompressor = (function ($) {
8         "use strict";
9         var cms_action_id = '',
10                 cms_user_key = '',
11                 $table = null,
12                 queue = {},
13                 moduleParams = function (params) {
14                         var mod_params = {};
15                         $.each(params, function (index, val) {
16                                 mod_params[cms_action_id + index] = val;
17                         });
18                         return mod_params;
19                 },
20                 ajax = function (mod_params, $td) {
21                         var old_content = $td.html(),
22                                 identifier = Object.keys(queue).length,
23                                 cms_params = {
24                                         'module': 'ImageCompressor',
25                                         'action': 'ajax',
26                                         'showtemplate': false
27                                 };
28                         $td.html('<div class="spinner" />');
29                         queue[identifier] = $.ajax({
30                                 url: 'moduleinterface.php' + cms_user_key,
31                                 data: $.extend({}, moduleParams(mod_params), cms_params),
32                                 dataType: 'json',
33                                 success: function (data) {
34                                         var $html = $();
35                                         if(typeof data.message !== 'undefined')
36                                         {
37                                                 $html = $html.add('<span class="ic_message">' + data.message + '</span>');
38                                         }
39                                         if(typeof data.error !== 'undefined')
40                                         {
41                                                 $html = $html.add('<span class="ic_error">' + data.error + '</span>');
42                                         }
43                                         $td.html($html);
44                                 },
45                                 complete: function (response, status) {
46                                         delete queue[identifier];
47                                         if(status != 'success') {
48                                                 $td.html(old_content);
49                                         }
50                                 }
51                         });
52                 },
53                 checkRow = function (row) {
54                         var $row = $(row),
55                                 $td = $row.find('td:last-child'),
56                                 mod_params = {
57                                         'action': 'check',
58                                         'path': $row.find('td:eq(1)').text()
59                                 };
60
61                         ajax(mod_params, $td);
62                 },
63                 smushRow = function (row) {
64                         var $row = $(row),
65                                 $td = $row.find('td:last-child'),
66                                 mod_params = {
67                                         'action': 'smush',
68                                         'source': $td.find('.smush').attr('data-source'),
69                                         'path': $row.find('td:eq(1)').text()
70                                 };
71
72                         ajax(mod_params, $td);
73                 },
74                 abortQueue = function () {
75                         $.each(queue, function () {
76                                 this.abort();
77                         });
78                 };
79
80         return {
81                 init: function (action_id, user_key) {
82                         var $massprocessing = null;
83
84                         cms_action_id = action_id;
85                         cms_user_key = user_key;
86                         $table = $('#filelist_imagecompressor');
87                         $massprocessing = $table.find('thead th:last-child a');
88
89                         $massprocessing.click(function (e) {
90                                 e.preventDefault();
91                                 switch($massprocessing.index(this)) {
92                                         case 0:
93                                                 $table.find('tbody .check').click();
94                                                 break;
95                                         case 1:
96                                                 $table.find('tbody .smush').click();
97                                                 break;
98                                         case 2:
99                                                 abortQueue();
100                                                 break;
101                                 }
102                         });
103                         $table.find('tbody .check').live('click', function (e) {
104                                 e.preventDefault();
105                                 checkRow($(this).parent().parent());
106                         });
107                         $table.find('tbody .smush').live('click', function (e) {
108                                 e.preventDefault();
109                                 smushRow($(this).parent().parent().parent());
110                         });
111                 }
112         };
113 }(jQuery));