| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** Copyright (C) 2014 Canonical Ltda |
| 5 |
** All rights reserved. |
| 6 |
** Contact: Nokia Corporation ([email protected]) |
| 7 |
** |
| 8 |
** This file is part of the Qt Components project. |
| 9 |
** |
| 10 |
** $QT_BEGIN_LICENSE:BSD$ |
| 11 |
** You may use this file under the terms of the BSD license as follows: |
| 12 |
** |
| 13 |
** "Redistribution and use in source and binary forms, with or without |
| 14 |
** modification, are permitted provided that the following conditions are |
| 15 |
** met: |
| 16 |
** * Redistributions of source code must retain the above copyright |
| 17 |
** notice, this list of conditions and the following disclaimer. |
| 18 |
** * Redistributions in binary form must reproduce the above copyright |
| 19 |
** notice, this list of conditions and the following disclaimer in |
| 20 |
** the documentation and/or other materials provided with the |
| 21 |
** distribution. |
| 22 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
| 23 |
** the names of its contributors may be used to endorse or promote |
| 24 |
** products derived from this software without specific prior written |
| 25 |
** permission. |
| 26 |
** |
| 27 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
// FastScroll.js - this is just SectionScroller.js with a fix for |
| 43 |
// section.criteria == ViewSection.FirstCharacter |
| 44 |
var sectionData = []; |
| 45 |
var _sections = []; |
| 46 |
|
| 47 |
function initialize(list) { |
| 48 |
initSectionData(list); |
| 49 |
} |
| 50 |
|
| 51 |
function contains(name) { |
| 52 |
return (_sections.indexOf(name) > -1) |
| 53 |
} |
| 54 |
|
| 55 |
function initSectionData(list) { |
| 56 |
if (!list || !list.model) return; |
| 57 |
sectionData = []; |
| 58 |
_sections = []; |
| 59 |
var current = "", |
| 60 |
prop = list.section.property, |
| 61 |
sectionText; |
| 62 |
|
| 63 |
if (list.section.criteria === ViewSection.FullString) { |
| 64 |
for (var i = 0, count = list.model.count; i < count; i++) { |
| 65 |
sectionText = list.getSectionText(i) |
| 66 |
if (sectionText !== current) { |
| 67 |
current = sectionText; |
| 68 |
_sections.push(current); |
| 69 |
sectionData.push({ index: i, header: current }); |
| 70 |
} |
| 71 |
} |
| 72 |
} else if (list.section.criteria === ViewSection.FirstCharacter) { |
| 73 |
for (var i = 0, count = list.model.count; i < count; i++) { |
| 74 |
sectionText = list.getSectionText(i).substring(0, 1) |
| 75 |
if (sectionText !== current) { |
| 76 |
current = sectionText |
| 77 |
_sections.push(sectionText); |
| 78 |
sectionData.push({ index: i, header: current }); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
function getSectionPositionString(name) { |
| 85 |
var val = _sections.indexOf(name); |
| 86 |
return val === 0 ? "first" : |
| 87 |
val === _sections.length - 1 ? "last" : false; |
| 88 |
} |
| 89 |
|
| 90 |
function getAt(pos) { |
| 91 |
return _sections[pos] ? _sections[pos] : ""; |
| 92 |
} |
| 93 |
|
| 94 |
function getRelativeSections(current) { |
| 95 |
var val = _sections.indexOf(current), |
| 96 |
sect = [], |
| 97 |
sl = _sections.length; |
| 98 |
|
| 99 |
val = val < 1 ? 1 : val >= sl-1 ? sl-2 : val; |
| 100 |
sect = [getAt(val - 1), getAt(val), getAt(val + 1)]; |
| 101 |
|
| 102 |
return sect; |
| 103 |
} |
| 104 |
|
| 105 |
function getClosestSection(pos, down) { |
| 106 |
var tmp = (_sections.length) * pos; |
| 107 |
var val = Math.ceil(tmp) // TODO: better algorithm |
| 108 |
val = val < 2 ? 1 : val; |
| 109 |
return _sections[val-1]; |
| 110 |
} |
| 111 |
|
| 112 |
function getNextSection(current) { |
| 113 |
var val = _sections.indexOf(current); |
| 114 |
return (val > -1 ? _sections[(val < _sections.length - 1 ? val + 1 : val)] : _sections[0]) || ""; |
| 115 |
} |
| 116 |
|
| 117 |
function getPreviousSection(current) { |
| 118 |
var val = _sections.indexOf(current); |
| 119 |
return (val > -1 ? _sections[(val > 0 ? val - 1 : val)] : _sections[0]) || ""; |
| 120 |
} |
| 121 |
|
| 122 |
function getIndexFor(sectionName) { |
| 123 |
var data = sectionData[_sections.indexOf(sectionName)] |
| 124 |
if (data) { |
| 125 |
var val = data.index; |
| 126 |
return val === 0 || val > 0 ? val : -1; |
| 127 |
} else { |
| 128 |
return -1 |
| 129 |
} |
| 130 |
} |