Blame view

sources/apps/contacts/js/loader.js 4.36 KB
d1bafeea1   Kload   [fix] Upgrade to ...
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
  /**
   * 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);
  		});
  	},
6d9380f96   Cédric Dupont   Update sources OC...
83
  	startImport: function(backend, addressBookId, importType) {
d1bafeea1   Kload   [fix] Upgrade to ...
84
85
86
  		var self = this;
  		$('.import-select').hide();
  		$('.import-status').show();
6d9380f96   Cédric Dupont   Update sources OC...
87
  		$.when(self.addressBooks.prepareImport(backend, addressBookId, importType, this.path, this.fileName))
d1bafeea1   Kload   [fix] Upgrade to ...
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
  		.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() {
6d9380f96   Cédric Dupont   Update sources OC...
115
  		var defer = $.Deferred();
d1bafeea1   Kload   [fix] Upgrade to ...
116
  		if(!this.$template) {
d1bafeea1   Kload   [fix] Upgrade to ...
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
  			$.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');
  	}
6d9380f96   Cédric Dupont   Update sources OC...
149
150
  	
  });