| 1 |
/* |
| 2 |
* Copyright 2012-2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This file is part of lomiri-addressbook-app. |
| 5 |
* |
| 6 |
* phone-app is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; version 3. |
| 9 |
* |
| 10 |
* phone-app is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
var sectionData = []; |
| 19 |
var _sections = []; |
| 20 |
|
| 21 |
function sectionValueForContact(contact) { |
| 22 |
if (contact) { |
| 23 |
var section = contact.tag.tag.charAt(0).toUpperCase() |
| 24 |
return (section === "" ? "#" : section) |
| 25 |
} else { |
| 26 |
return null |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
function initSectionData(list) { |
| 31 |
if (!list || !list.listModel) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
sectionData = []; |
| 36 |
_sections = []; |
| 37 |
|
| 38 |
var current = ""; |
| 39 |
var item; |
| 40 |
var contacts = list.listModel.contacts; |
| 41 |
|
| 42 |
for (var i = 0, count = contacts.length; i < count; i++) { |
| 43 |
item = sectionValueForContact(contacts[i]) |
| 44 |
if (item !== current) { |
| 45 |
current = item; |
| 46 |
_sections.push(current); |
| 47 |
sectionData.push({ index: i, header: current}); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function getIndexFor(sectionName) { |
| 53 |
var index = _sections.indexOf(sectionName) |
| 54 |
if (index != -1) { |
| 55 |
var val = sectionData[_sections.indexOf(sectionName)].index; |
| 56 |
return val === 0 || val > 0 ? val : -1; |
| 57 |
} else { |
| 58 |
return -1 |
| 59 |
} |
| 60 |
} |