Fix for TC-1560 UI always zoomed out
[profile/ivi/Modello_Phone.git] / js / contacts_library.js
1 /*global Phone, callContactCarousel, GRID_TAB, LIST_TAB, loadTemplate, ko*/
2
3 /**
4  * Class which provides methods to operate with contacts library which displays all contact information (name, phone number, photo) from paired device ordered by name.
5  *
6  * @class ContactsLibrary
7  * @module PhoneApplication
8  */
9 var ContactsLibrary = {
10         currentSelectedContact : "",
11         /**
12          * Method initializes contacts library.
13          *
14          * @method init
15          */
16         init : function() {
17                 "use strict";
18                 $('#library').library("setSectionTitle", "PHONE CONTACTS");
19                 $('#library').library("init");
20
21                 var tabMenuModel = {
22                         Tabs : [ {
23                                 text : "CONTACTS A-Z",
24                                 selected : true
25                         } ]
26                 };
27
28                 $('#library').library("tabMenuTemplateCompile", tabMenuModel);
29
30                 $('#library').bind('eventClick_GridViewBtn', function() {
31                         ContactsLibrary.showContacts();
32                 });
33
34                 $('#library').bind('eventClick_ListViewBtn', function() {
35                         ContactsLibrary.showContacts();
36                 });
37
38                 $('#library').bind('eventClick_SearchViewBtn', function() {
39                 });
40
41                 $('#library').bind('eventClick_menuItemBtn', function() {
42                         ContactsLibrary.showContacts();
43                 });
44
45                 $('#library').bind('eventClick_closeSubpanel', function() {
46                 });
47
48                 $("#alphabetBookmarkList").on("letterClick", function(event, letter) {
49                         console.log(letter);
50                         Phone.contactsAlphabetFilter(letter === "*" ? "" : letter);
51                 });
52
53                 ContactsLibrary.showContacts();
54         },
55         /**
56          * Method unhides library page.
57          *
58          * @method show
59          */
60         show : function() {
61                 "use strict";
62                 $('#library').library("showPage");
63         },
64         /**
65          * Method hides library page.
66          *
67          * @method hide
68          */
69         hide : function() {
70                 "use strict";
71                 $('#library').library("hidePage");
72         },
73         /**
74          * Method opens contact detail.
75          *
76          * @method openContactDetail
77          * @param contact
78          *            {Object} Object representing contact's information.
79          */
80         openContactDetail : function(contact) {
81                 "use strict";
82                 if (!!contact) {
83                         ContactsLibrary.currentSelectedContact  = contact;
84                         var history = Phone.getCallHistoryByPersonId(contact.personId);
85                         var formattedContact = ContactsLibrary.initContactDetail(contact);
86                         formattedContact.history = history;
87                         ContactsLibrary.renderContactDetailView(formattedContact);
88                 } else {
89                         console.log("Supplied contact is null.");
90                 }
91         },
92         /**
93          * Method renders search view.
94          *
95          * @method renderContactDetailView
96          * @param contact
97          *            {Object} Contact object.
98          */
99         renderContactDetailView : function(contact) {
100                 "use strict";
101                 console.log("open contact called");
102                 var subpanelModel = {
103                         textTitle : "CONTACT",
104                         textSubtitle : contact.name || "Unknown",
105                         actionName : "BACK",
106                         action : function() {
107                                 console.log("back clicked");
108                                 ContactsLibrary.showContacts();
109                                 ContactsLibrary.currentSelectedContact = "";
110                         }
111                 };
112                 $('#library').library("subpanelContentTemplateCompile", subpanelModel);
113                 $('#library').library("clearContent");
114                 $('#library').library("setContentDelegate", "templates/libraryContactDetailDelegate.html");
115                 $('#library').library("contentTemplateCompile", contact, "contactDetail", function() {
116                         $("#contactDetailMobileTitle").boxCaptionPlugin('initSmall', "MOBILE");
117                         $("#contactDetailEmailTitle").boxCaptionPlugin('initSmall', "EMAIL");
118                         $("#contactDetailAddressTitle").boxCaptionPlugin('initSmall', "ADDRESS");
119                 });
120         },
121         /**
122          * Method which shows contacts in grid or list view.
123          *
124          * @method showContacts
125          */
126         showContacts : function() {
127                 "use strict";
128                 console.log("show contacts called");
129                 var view = "";
130                 switch ($('#library').library('getSelectetLeftTabIndex')) {
131                 case GRID_TAB:
132                         view = "contactsLibraryContentGrid";
133                         break;
134                 case LIST_TAB:
135                         view = "contactsLibraryContentList";
136                         break;
137                 default:
138                         view = "contactsLibraryContentList";
139                         break;
140                 }
141                 $('#library').library('closeSubpanel');
142                 $('#library').library("clearContent");
143                 $('#library').library("changeContentClass", view);
144                 loadTemplate("templates/", "template-contacts", function() {
145                         var contactsElement = '<div data-bind="template: { name: \'template-contacts\', foreach: Phone.contactsComputed }"></div>';
146                         $(contactsElement).appendTo($('.' + view));
147                         ko.applyBindings(Phone);
148                 });
149         },
150         /**
151          * Method which initializes contact detail.
152          *
153          * @method initContactDetail
154          * @param contact
155          *            {Object} Contact object.
156          */
157         initContactDetail : function(contact) {
158                 "use strict";
159                 var tempContact = {
160                         id : "",
161                         name : "",
162                         phoneNumber : "",
163                         email : "",
164                         photoURI : "",
165                         address : "",
166                         isFavorite : false,
167                         history : []
168                 };
169
170                 if (!!contact) {
171                         var str = "";
172
173                         if (!!contact.uid) {
174                                 tempContact.id = contact.uid;
175                         }
176
177                         if (!!contact.name) {
178                                 tempContact.name = Phone.getDisplayNameStr(contact);
179                         }
180
181                         if (!!contact.phoneNumbers && contact.phoneNumbers.length && !!contact.phoneNumbers[0].number) {
182                                 tempContact.phoneNumber = contact.phoneNumbers[0].number.trim();
183                         }
184
185                         if (!!contact.emails && contact.emails.length && !!contact.emails[0].email) {
186                                 tempContact.email = contact.emails[0].email.trim();
187                         }
188
189                         if (!!contact.photoURI) {
190                                 tempContact.photoURI = contact.photoURI.trim();
191                         }
192
193                         if (!!contact.addresses && contact.addresses.length) {
194                                 str = !!contact.addresses[0].streetAddress ? contact.addresses[0].streetAddress.trim() + "<br />" : "";
195                                 str += !!contact.addresses[0].city ? contact.addresses[0].city.trim() + "<br />" : "";
196                                 str += !!contact.addresses[0].country ? contact.addresses[0].country.trim() + "<br />" : "";
197                                 str += !!contact.addresses[0].postalCode ? contact.addresses[0].postalCode.trim() : "";
198
199                                 if (str.toString().trim() === "") {
200                                         str = "-";
201                                 }
202
203                                 tempContact.address = str.trim();
204                         }
205
206                         tempContact.isFavorite = contact.isFavorite;
207                 }
208                 return tempContact;
209         }
210 };