From 0ba8c9a0a054e23f03b82b6fd0132ee7a766c565 Mon Sep 17 00:00:00 2001 From: Hendrik Leppelsack Date: Thu, 29 Oct 2015 11:05:48 +0100 Subject: implement jcard support --- gulpfile.js | 4 +- js/components/contact/contact_controller.js | 6 +- js/main.js | 49 +-------- js/models/addressBook_model.js | 0 js/models/contact_model.js | 53 ++++++++++ js/public/script.js | 148 ++++++++++++++++++---------- js/services/addressBook_service.js | 6 ++ js/services/contact_service.js | 21 ++++ js/services/davClient_service.js | 6 ++ js/services/dav_service.js | 6 ++ templates/contact.html | 2 +- 11 files changed, 199 insertions(+), 102 deletions(-) create mode 100644 js/models/addressBook_model.js create mode 100644 js/models/contact_model.js create mode 100644 js/services/addressBook_service.js create mode 100644 js/services/contact_service.js create mode 100644 js/services/davClient_service.js create mode 100644 js/services/dav_service.js diff --git a/gulpfile.js b/gulpfile.js index 5035647d..696d43e9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -6,7 +6,9 @@ var gulp = require('gulp'), gulp.task('js', function() { return gulp.src([ 'js/main.js', - 'js/components/**/*.js' + 'js/components/**/*.js', + 'js/services/**/*.js', + 'js/models/**/*.js' ]) .pipe(jshint('.jshintrc')) .pipe(jshint.reporter('default')) diff --git a/js/components/contact/contact_controller.js b/js/components/contact/contact_controller.js index 2fffd08c..e6cd51d1 100644 --- a/js/components/contact/contact_controller.js +++ b/js/components/contact/contact_controller.js @@ -1,6 +1,8 @@ -app.controller('contactCtrl', ['$filter', function($filter) { +app.controller('contactCtrl', ['Contact', function(Contact) { var ctrl = this; - console.log($filter('vCard2JSON')(ctrl.data.addressData)); + ctrl.contact = new Contact(ctrl.data); + + console.log(ctrl.contact); }]); \ No newline at end of file diff --git a/js/main.js b/js/main.js index bf5999f9..51b3abcd 100644 --- a/js/main.js +++ b/js/main.js @@ -10,10 +10,6 @@ var app = angular.module('contactsApp', ['ui.router']); -app.run(function($rootScope) { - $rootScope.addressBooks = []; -}); - app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider){ $urlRouterProvider.otherwise('/'); @@ -31,9 +27,7 @@ app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $ur var addressBook = addressBooks.filter(function (element) { return element.displayName === $stateParams.addressBookId; })[0]; - return DavClient.syncAddressBook(addressBook, {accept: 'application/vCard+json'}); - }).then(function (addressBook) { - return addressBook; + return DavClient.syncAddressBook(addressBook, {json: true}); }); } }, @@ -43,44 +37,3 @@ app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $ur }); }]); -app.service('DavClient', function() { - var xhr = new dav.transport.Basic( - new dav.Credentials() - ); - return new dav.Client(xhr); -}); - -app.service('DavService', ['DavClient', function(client) { - return client.createAccount({ - server: OC.linkToRemoteBase('carddav'), - accountType: 'carddav' - }); -}]); - -app.service('AddressBookService', ['DavService', function(DavService){ - return DavService.then(function(account) { - return account.addressBooks; - }); -}]); - -app.filter('JSON2vCard', function() { - return vCard.generate; -}); - -app.filter('vCard2JSON', function() { - return function(input, prop) { - var result = vCard.parse(input); - if(prop === undefined) { - return result; - } - if(result[prop] === undefined) { - return undefined; - } - result = result[prop][0]; - if(result.value instanceof Array) { - return result.value.join(' '); - } else { - return result.value; - } - }; -}); diff --git a/js/models/addressBook_model.js b/js/models/addressBook_model.js new file mode 100644 index 00000000..e69de29b diff --git a/js/models/contact_model.js b/js/models/contact_model.js new file mode 100644 index 00000000..30f1e0fd --- /dev/null +++ b/js/models/contact_model.js @@ -0,0 +1,53 @@ +app.factory('Contact', function(ContactService) +{ + return function Contact(jCard) { + angular.extend(this, { + + jCard: [], + + name: function(value) { + var name = this.getProperty('n'); + if (angular.isDefined(value)) { + // setter + this.setPropertyValue(name, value); + } else { + // getter + return this.getPropertyValue(name); + } + + }, + + getProperty: function(name) { + var contact = this; + if(!angular.isDefined(contact.jCard.addressData[1])) { + return undefined; + } + var properties = contact.jCard.addressData[1]; + for(var i in properties) { + if(properties[i][0] === name) + return properties[i]; + } + return undefined; + }, + + getPropertyValue: function(property) { + if(property[3] instanceof Array) { + return property[3].join(' '); + } else { + return property[3]; + } + }, + + setPropertyValue: function(property, propertyValue) { + property[3] = propertyValue; + this.update(); + }, + + update: function() { + ContactService.update(this.jCard); + } + + }); + angular.extend(this.jCard, jCard); + }; +}); \ No newline at end of file diff --git a/js/public/script.js b/js/public/script.js index 972d8d3c..705d547c 100644 --- a/js/public/script.js +++ b/js/public/script.js @@ -10,10 +10,6 @@ var app = angular.module('contactsApp', ['ui.router']); -app.run(function($rootScope) { - $rootScope.addressBooks = []; -}); - app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider){ $urlRouterProvider.otherwise('/'); @@ -31,9 +27,7 @@ app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $ur var addressBook = addressBooks.filter(function (element) { return element.displayName === $stateParams.addressBookId; })[0]; - return DavClient.syncAddressBook(addressBook, {accept: 'application/vCard+json'}); - }).then(function (addressBook) { - return addressBook; + return DavClient.syncAddressBook(addressBook, {json: true}); }); } }, @@ -43,47 +37,6 @@ app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $ur }); }]); -app.service('DavClient', function() { - var xhr = new dav.transport.Basic( - new dav.Credentials() - ); - return new dav.Client(xhr); -}); - -app.service('DavService', ['DavClient', function(client) { - return client.createAccount({ - server: OC.linkToRemoteBase('carddav'), - accountType: 'carddav' - }); -}]); - -app.service('AddressBookService', ['DavService', function(DavService){ - return DavService.then(function(account) { - return account.addressBooks; - }); -}]); - -app.filter('JSON2vCard', function() { - return vCard.generate; -}); - -app.filter('vCard2JSON', function() { - return function(input, prop) { - var result = vCard.parse(input); - if(prop === undefined) { - return result; - } - if(result[prop] === undefined) { - return undefined; - } - result = result[prop][0]; - if(result.value instanceof Array) { - return result.value.join(' '); - } else { - return result.value; - } - }; -}); app.controller('addressbookCtrl', function() { var ctrl = this; @@ -118,10 +71,12 @@ app.directive('addressbooklist', function() { templateUrl: OC.linkTo('contactsrework', 'templates/addressBookList.html') }; }); -app.controller('contactCtrl', ['$filter', function($filter) { +app.controller('contactCtrl', ['Contact', function(Contact) { var ctrl = this; - console.log($filter('vCard2JSON')(ctrl.data.addressData)); + ctrl.contact = new Contact(ctrl.data); + + console.log(ctrl.contact); }]); app.directive('contact', function() { @@ -149,4 +104,97 @@ app.directive('contactlist', function() { }, templateUrl: OC.linkTo('contactsrework', 'templates/contactList.html') }; +}); +app.service('AddressBookService', ['DavService', function(DavService){ + + return DavService.then(function(account) { + return account.addressBooks; + }); +}]); +app.service('ContactService', [ 'DavClient', function(DavClient) { + + this.create = function(addressBook) { + // push contact to server + return DavClient.createCard(addressBook); + }; + + this.update = function(contact) { + // update contact on server + return DavClient.updateCard(contact, {json: true}); + }; + + this.remove = function(contact) { + // delete contact from server + return DavClient.deleteCard(contact); + }; + + this.fromArray = function(array) { + // from array to contact + }; +}]); +app.service('DavClient', function() { + var xhr = new dav.transport.Basic( + new dav.Credentials() + ); + return new dav.Client(xhr); +}); +app.service('DavService', ['DavClient', function(client) { + return client.createAccount({ + server: OC.linkToRemoteBase('carddav'), + accountType: 'carddav' + }); +}]); + +app.factory('Contact', function(ContactService) +{ + return function Contact(jCard) { + angular.extend(this, { + + jCard: [], + + name: function(value) { + var name = this.getProperty('n'); + if (angular.isDefined(value)) { + // setter + this.setPropertyValue(name, value); + } else { + // getter + return this.getPropertyValue(name); + } + + }, + + getProperty: function(name) { + var contact = this; + if(!angular.isDefined(contact.jCard.addressData[1])) { + return undefined; + } + var properties = contact.jCard.addressData[1]; + for(var i in properties) { + if(properties[i][0] === name) + return properties[i]; + } + return undefined; + }, + + getPropertyValue: function(property) { + if(property[3] instanceof Array) { + return property[3].join(' '); + } else { + return property[3]; + } + }, + + setPropertyValue: function(property, propertyValue) { + property[3] = propertyValue; + this.update(); + }, + + update: function() { + ContactService.update(this.jCard); + } + + }); + angular.extend(this.jCard, jCard); + }; }); \ No newline at end of file diff --git a/js/services/addressBook_service.js b/js/services/addressBook_service.js new file mode 100644 index 00000000..60cccccc --- /dev/null +++ b/js/services/addressBook_service.js @@ -0,0 +1,6 @@ +app.service('AddressBookService', ['DavService', function(DavService){ + + return DavService.then(function(account) { + return account.addressBooks; + }); +}]); \ No newline at end of file diff --git a/js/services/contact_service.js b/js/services/contact_service.js new file mode 100644 index 00000000..bbdc0df9 --- /dev/null +++ b/js/services/contact_service.js @@ -0,0 +1,21 @@ +app.service('ContactService', [ 'DavClient', function(DavClient) { + + this.create = function(addressBook) { + // push contact to server + return DavClient.createCard(addressBook); + }; + + this.update = function(contact) { + // update contact on server + return DavClient.updateCard(contact, {json: true}); + }; + + this.remove = function(contact) { + // delete contact from server + return DavClient.deleteCard(contact); + }; + + this.fromArray = function(array) { + // from array to contact + }; +}]); \ No newline at end of file diff --git a/js/services/davClient_service.js b/js/services/davClient_service.js new file mode 100644 index 00000000..c799b680 --- /dev/null +++ b/js/services/davClient_service.js @@ -0,0 +1,6 @@ +app.service('DavClient', function() { + var xhr = new dav.transport.Basic( + new dav.Credentials() + ); + return new dav.Client(xhr); +}); \ No newline at end of file diff --git a/js/services/dav_service.js b/js/services/dav_service.js new file mode 100644 index 00000000..345657ed --- /dev/null +++ b/js/services/dav_service.js @@ -0,0 +1,6 @@ +app.service('DavService', ['DavClient', function(client) { + return client.createAccount({ + server: OC.linkToRemoteBase('carddav'), + accountType: 'carddav' + }); +}]); \ No newline at end of file diff --git a/templates/contact.html b/templates/contact.html index 960e1e76..efad39c8 100644 --- a/templates/contact.html +++ b/templates/contact.html @@ -1,3 +1,3 @@
  • - {{ctrl.data.addressData | vCard2JSON:'n'}} | {{ctrl.data.addressData | vCard2JSON:'tel'}} | {{ctrl.data.addressData | vCard2JSON:'email'}} +
  • \ No newline at end of file -- cgit v1.2.3