source: sandbox/expressoServiceImap/prototype/modules/attach_message/common.js @ 6510

Revision 6510, 6.4 KB checked in by natan, 12 years ago (diff)

Ticket #2845 - Atualizacao da API na busca de mensagens - Otimizando o carregamento do anexar mensagens

Line 
1dots = /\./gi;
2dashes = /\//gi;
3flags = [ 'Attachment', 'Forwarded' ,'Recent', 'Unseen',  'Answered',  'Draft',  'Deleted', 'Flagged', 'Followupflag', 'Label' ];
4months = ['Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec'];
5current_folder = 'INBOX';
6
7DataLayer.codec( 'folder', 'tree', {
8
9        encoder: function( data ){
10   
11            if( preferences.use_local_messages == 1 || expresso_offline)
12            {
13                var folders = expresso_local_messages.list_local_folders();
14               
15                var stripParents = /^(.*)\/([^\/]*)/;
16
17                $.each( folders, function( i, folder ){
18                       
19                        if(typeof(folder) == 'undefined')  return;
20                         
21                        var id = 'local_messages/' + folder[0];
22
23                        var parts = stripParents.exec( id );
24                   
25                        data[data.length] = { 'id' : id,
26                                              'commonName' : parts[2],
27                                              'parentFolder' : parts[1] };
28                });
29            }
30
31            var trees = [[],[],[]],
32
33            root = { inbox: /^INBOX/, share: /^user/, local: /^local_messages/ };
34
35            for (var i = 0; i < data.length; i++ )
36            {
37                var index = root.inbox.test(data[i].id) ? 0 :
38                            root.share.test(data[i].id) ? 1 :
39                            root.local.test(data[i].id) ? 2 :
40                            'false';
41
42                if( isNaN(index) ) continue;
43
44                if (!unorphanize(trees[index], data[i]))
45                {
46                    data[i].children = [];
47                    trees[index].push(data[i]);
48                }
49            }
50
51            return( { folders: trees } );
52        }
53} );
54
55DataLayer.codec( 'message', 'jqGrid', {
56
57      encoder: function( data, crit ){
58
59            if( !crit.criteria.properties.context.folder.indexOf( 'local_messages/' ) )
60            {
61                var msgs = expresso_local_messages.get_local_range_msgs( crit.criteria.properties.context.folder.replace(dots, "/").replace("local_messages/", ""),
62                                                                          crit.offset + 1, crit.limit, "SORTARRIVAL", crit.orderDesc, "ALL", 1, 1 );
63
64                for( var i = 0; i < msgs.length; i++ )
65                {
66                      msgs[i].size = msgs[i].Size;
67                      msgs[i].timestamp = msgs[i].udate * 1000;
68                      msgs[i].flags = [];
69
70                      for( var ii = 0; ii < flags.length; ii++ )
71                          if( f = $.trim( msgs[i][ flags[ii] ] ) )
72                              msgs[i].flags[ msgs[i].flags.length ] =  f;
73
74                      msgs[i].flags = msgs[i].flags.join(',');
75                }
76
77                return( {"rows": msgs,
78                          "records": msgs.length,
79                          "page": crit.page,
80                          "total": Math.ceil( msgs.num_msgs / crit.limit )} );
81            }
82
83            return( data );
84      },
85      criteria: function( crit ){
86
87            return { criteria: { properties: {context:{folder: current_folder || 'INBOX'}}, offset: crit.rows * ( crit.page - 1 ), limit: crit.rows, orderDesc: crit.sord == "desc" } };
88
89      }
90});
91   
92//BASE_PATH = '../';
93//encontra os pais de todas as pastas e cria uma nova estrutura adicionando os filhos a um array no atributo 'children' do respectivo pai
94unorphanize = function(root, element) {
95        var ok = false;
96        for (var i=0; i<root.length; i++) {
97                if (root[i].id == element.parentFolder) {
98                        element.children = new Array();
99                        root[i].children.push(element);
100                        return true;
101                } else if (ok = unorphanize(root[i].children, element)) {
102                        break;
103                }
104        }
105
106        return ok;
107}
108
109/* --- helpers --- */
110bytes2Size = function(bytes) {
111        var sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
112        if (bytes == 0) return 'n/a';
113        var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
114        var size = (i<2) ? Math.round((bytes / Math.pow(1024, i))) : Math.round((bytes / Math.pow(1024, i)) * 100)/100;
115        return  size + ' ' + sizes[i];
116}
117
118flags2Class = function(cellvalue, options, rowObject) {
119        var classes = '';       
120        cellvalue = cellvalue.split(',');
121        cell = {
122                        Unseen: parseInt(cellvalue[0])  ? 'Unseen' : 'Seen',
123                        Answered: parseInt(cellvalue[1]) ? 'Answered' : (parseInt(cellvalue[2]) ? 'Forwarded' : ''),
124                        Flagged: parseInt(cellvalue[3]) ? 'Flagged' : '',
125                        Recent: parseInt(cellvalue[4])  ? 'Recent' : '',                       
126                        Draft: parseInt(cellvalue[5]) ? 'Draft' : ''           
127                };
128        for(var flag in cell){
129                classes += '<span class="flags '+ (cell[flag]).toLowerCase() + '"' + (cell[flag] != "" ? 'title="'+ get_lang(cell[flag])+'"' : '')+'> </span>';
130        }
131        if(rowObject.labels){   
132                var titles = [];
133                var count = 0;
134                 for(i in rowObject.labels){
135                        titles[count] = " "+rowObject.labels[i].name;
136                        count++;
137                }
138                titles = titles.join();
139                classes += '<span class="flags labeled" title="'+titles+'"> </span>';
140        }else{
141                classes += '<span class="flags"> </span>';
142        }
143       
144        if(rowObject.followupflagged){         
145                if(rowObject.followupflagged.followupflag.id < 7){
146                        var nameFollowupflag = get_lang(rowObject.followupflagged.followupflag.name);
147                }else{
148                        var nameFollowupflag = rowObject.followupflagged.followupflag.name;
149                }
150                if(rowObject.followupflagged.isDone == 1){
151                        classes += '<span class="flags followupflagged" title="'+nameFollowupflag+'" style="background:'+rowObject.followupflagged.backgroundColor+';"><img style=" margin-left:-3px;" src="../prototype/modules/mail/img/flagChecked.png"></span>';
152                }else{                 
153                        classes += '<span class="flags followupflagged" title="'+nameFollowupflag+'" style="background:'+rowObject.followupflagged.backgroundColor+';"><img src="../prototype/modules/mail/img/flagEditor.png"></span>';
154                }
155               
156        }
157
158        return classes;
159}
160
161NormaliseFrom = function(cellvalue, options, rowObject) {
162        rowObject['flags'] = rowObject['flags'].split(",");
163        if(rowObject['flags'][rowObject['flags'].length-1] ==  1){
164                return get_lang(special_folders["Drafts"]);
165        }
166        return cellvalue;       
167}
168
169NormaliseSubject = function(cellvalue, options, rowObject) {
170        return html_entities(cellvalue);
171}
172
173date2Time = function (timestamp) {
174        date = new Date();
175        if( typeof timestamp === "string" )
176            timestamp = parseInt( timestamp, 10 );
177        dat = new Date(timestamp);
178        if ((date.getTime() - timestamp) < (24*60*60*1000)) {
179                return '<span class="timable" title="'+dat.getTime()+'"></span>';
180        } else {
181                date = new Date(timestamp);
182                if(is_ie){
183                        var b = date.toString().split(' ');
184                        var c = b[2] + "/" + months.indexOf(b[1]) + "/" + b[5];
185                        return '<span class="datable">' + c + '</span>';
186                }else{
187                        var b = date.toISOString().split("T")[0].split("-");
188                        var c = b[2] + "/" + b[1] + "/" + b[0];
189                        return '<span class="datable">' + c + '</span>';
190                }
191        }
192}
193
194changeTabIndex = function (elements) {
195//      jQuery('#foldertree').attr('tabIndex', '1').focus();
196}
197
198selectedMessagesCount = function() {
199        var byte_size = 0, total_messages = 0;
200        for (var folder in selectedMessages) {
201                for (var message in selectedMessages[folder]) {
202                        if (selectedMessages[folder][message]) {
203                                byte_size += parseInt(onceOpenedMessages[folder][message].size);
204                                total_messages++;
205                        }
206                }
207        }
208        $("#selected_messages_number").html(total_messages).next().html(bytes2Size(byte_size));
209        return total_messages;
210}
Note: See TracBrowser for help on using the repository browser.