loader.js
4.36 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
OC.ContactsImporter = OC.ContactsImporter || {
init:function(fileName) {
var self = OC.ContactsImporter;
self.path = $('#dir').val();
self.fileName = fileName;
console.log('fileName', self.path, self.fileName);
OC.addScript('contacts', 'addressbooks', function() {
OC.addScript('contacts', 'storage', function() {
$.when(self._getTemplate()).then(function($tmpl) {
self.$template = $tmpl;
if(self.$dialog) {
self.$dialog.ocdialog('close');
}
self.$dialog = self.$template.octemplate({
selectText: t('contacts', 'Please choose the addressbook'),
defaultText: t('contacts', 'Import into...')
});
self.$dialog.appendTo($('body'));
self.addressBooks = new OC.Contacts.AddressBookList(
new OC.Contacts.Storage(), self.$dialog, null, true
);
self.showDialog();
})
.fail(function() {
alert(t('contacts', 'Error loading import template'));
});
})
.fail(function(jqxhr, settings, exception) {
console.warn('Error loading storage backend', jqxhr, settings, exception);
});
})
.fail(function(jqxhr, settings, exception) {
console.warn('Error loading address book backend', jqxhr, settings, exception);
});
},
showDialog:function() {
var self = this;
$.when(self.addressBooks.loadAddressBooks()).then(function(response) {
if(!response.error) {
self.$dialog.ocdialog({
modal: true,
title: t('contacts', 'Import contacts'),
close: function() {
$(this).ocdialog('destroy').remove();
self.$dialog = null;
}
});
self.$importIntoSelect = self.$dialog.find('#import_into');
self.$importIntoSelect.on('change', function() {
var $selected = $(this).find('option:selected');
if($(this).val() === '-1') {
self.$dialog.ocdialog('option', 'buttons', []);
} else {
self.$dialog.ocdialog('option', 'buttons', [{
text: t('contacts', 'Import'),
defaultButton: true,
click:function() {
console.log('Selected', $selected);
self.$dialog.ocdialog('option', {
buttons: [],
closeButton: false,
title: t('contacts', 'Importing...')
});
self.startImport($selected.data('backend'), $selected.val());
}
}]);
}
});
} else {
console.warn('response.message');
}
})
.fail(function(response) {
console.warn(response);
});
},
startImport: function(backend, addressBookId, importType) {
var self = this;
$('.import-select').hide();
$('.import-status').show();
$.when(self.addressBooks.prepareImport(backend, addressBookId, importType, this.path, this.fileName))
.then(function(response) {
if(!response.error) {
$.when(self.addressBooks.doImport(response)).then(function(response) {
self.$dialog.ocdialog('option', {
title: t('contacts', 'Import done'),
closeButton: true,
buttons: [{
text: t('contacts', 'Close'),
defaultButton: true,
click:function() {
self.$dialog.ocdialog('close');
}
}]
});
})
.fail(function(response) {
console.warn(response);
});
} else {
console.warn('response.message');
}
})
.fail(function(response) {
console.warn(response);
});
},
_getTemplate: function() {
var defer = $.Deferred();
if(!this.$template) {
$.get(OC.filePath('contacts', 'templates', 'importdialog.html'), function(tmpl) {
defer.resolve($(tmpl));
})
.fail(function() {
defer.reject();
});
} else {
defer.resolve(this.$template);
}
return defer.promise();
}
};
$(document).ready(function(){
// If the app is already active there's no need for the FileActions
if(OC.Contacts) {
return;
}
$(document).bind('status.contacts.error', function(e, data) {
console.warn(data.message);
//console.trace();
//OC.notify({message:data.message});
});
if(typeof FileActions !== 'undefined'){
FileActions.register('text/vcard','importaddressbook', OC.PERMISSION_READ, '', OC.ContactsImporter.init);
FileActions.setDefault('text/vcard','importaddressbook');
FileActions.register('text/x-vcard','importaddressbook', OC.PERMISSION_READ, '', OC.ContactsImporter.init);
FileActions.setDefault('text/x-vcard','importaddressbook');
}
});