-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now compatible with Android (and other)
- Loading branch information
Showing
5 changed files
with
154 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,141 @@ | ||
var args = arguments[0] || {}; | ||
|
||
var options = { | ||
height: 50 | ||
msgTap: L('isTap', 'Tap to load more...'), | ||
msgDone: L('isDone', 'No more to load...'), | ||
msgError: L('isError', 'Tap to try again...') | ||
}; | ||
|
||
var attached = false; | ||
var loading = false; | ||
var shown = false; | ||
|
||
var item = null; | ||
var offset = null; | ||
|
||
function show() { | ||
|
||
if (!attached || shown) { | ||
return false; | ||
} | ||
|
||
shown = true; | ||
|
||
$.isIndicator.show(); | ||
$.is.height = options.height; | ||
|
||
return true; | ||
var loading = false, | ||
position = null; | ||
|
||
init(); | ||
|
||
function init() { | ||
|
||
// delete special args | ||
delete args.__parentSymbol; | ||
delete args.__itemTemplate; | ||
delete args.$model; | ||
|
||
// set args as options | ||
setOptions(args); | ||
|
||
// set default text & remove indicator | ||
$.isText.text = options.msgTap; | ||
$.is.remove($.isIndicator); | ||
|
||
// listen to scroll | ||
__parentSymbol.addEventListener('scroll', onScroll); | ||
|
||
return; | ||
} | ||
|
||
function hide() { | ||
|
||
if (!attached || !shown) { | ||
return false; | ||
} | ||
|
||
$.is.height = 0; | ||
$.isIndicator.hide(); | ||
|
||
shown = false; | ||
loading = false; | ||
|
||
return true; | ||
function state(state, message) { | ||
|
||
// remove indicator | ||
$.isIndicator.hide(); | ||
$.is.remove($.isIndicator); | ||
|
||
// set custom message | ||
if (message) { | ||
$.isText.text = message; | ||
|
||
// set state message | ||
} else { | ||
|
||
if (state === 0 || state === false) { | ||
$.isText.text = options.msgError; | ||
} else if (state === -1) { | ||
$.isText.text = options.msgDone; | ||
} else if (state === 1 || state === true) { | ||
$.isText.text = options.msgTap; | ||
} else { | ||
throw Error('Pass a valid state'); | ||
} | ||
} | ||
|
||
// add text | ||
$.is.add($.isText); | ||
|
||
loading = false; | ||
|
||
return true; | ||
} | ||
|
||
function load() { | ||
|
||
if (!attached || loading) { | ||
return false; | ||
} | ||
|
||
loading = true; | ||
|
||
show(); | ||
|
||
$.trigger('end'); | ||
|
||
return true; | ||
} | ||
|
||
function scrollListener(e) { | ||
|
||
if (OS_ANDROID) { | ||
var triggerLoad = triggerLoad || (item && e.firstVisibleItem > item) && (e.totalItemCount < e.firstVisibleItem + e.visibleItemCount); | ||
item = e.firstVisibleItem; | ||
|
||
} else if (OS_IOS) { | ||
var triggerLoad = triggerLoad || (offset && e.contentOffset.y > offset) && (e.contentOffset.y + e.size.height > e.contentSize.height); | ||
offset = e.contentOffset.y; | ||
} | ||
|
||
if (triggerLoad) { | ||
load(); | ||
} | ||
|
||
return; | ||
} | ||
if (loading) { | ||
return false; | ||
} | ||
|
||
function setOptions(_properties) { | ||
_.extend(options, _properties); | ||
} | ||
loading = true; | ||
|
||
// remove text | ||
$.is.remove($.isText); | ||
|
||
function attach(set) { | ||
|
||
if (attached) { | ||
return false; | ||
} | ||
|
||
$.is.height = 0; | ||
__parentSymbol.footerView = $.is; | ||
// add indicator | ||
$.is.add($.isIndicator); | ||
$.isIndicator.show(); | ||
|
||
init(); | ||
// trigger listener to load | ||
$.trigger('end', { | ||
success: function (msg) { return state(exports.SUCCESS, msg); }, | ||
error: function (msg) { return state(exports.ERROR, msg); }, | ||
done: function (msg) { return state(exports.DONE, msg); }, | ||
}); | ||
|
||
return true; | ||
return true; | ||
} | ||
|
||
function init() { | ||
__parentSymbol.addEventListener('scroll', scrollListener); | ||
|
||
attached = true; | ||
loading = false; | ||
shown = false; | ||
function onScroll(e) { | ||
var triggerLoad; | ||
|
||
item = null; | ||
offset = null; | ||
if (OS_ANDROID) { | ||
|
||
return; | ||
} | ||
// last item shown | ||
triggerLoad = (position && e.firstVisibleItem > position && e.totalItemCount <= (e.firstVisibleItem + e.visibleItemCount)); | ||
|
||
function dettach() { | ||
// remember position | ||
position = e.firstVisibleItem; | ||
|
||
if (!attached) { | ||
return false; | ||
} | ||
} else if (OS_IOS) { | ||
|
||
__parentSymbol.footerView = null; | ||
// last pixel shown | ||
triggerLoad = (position && e.contentOffset.y > position) && (e.contentOffset.y + e.size.height > e.contentSize.height); | ||
|
||
__parentSymbol.removeEventListener('scroll', scrollListener); | ||
// remember position | ||
position = e.contentOffset.y; | ||
} | ||
|
||
attached = false; | ||
// trigger | ||
if (triggerLoad) { | ||
load(); | ||
} | ||
|
||
return true; | ||
return; | ||
} | ||
|
||
delete args.__parentSymbol; | ||
function dettach() { | ||
|
||
setOptions(args); | ||
// set as done | ||
state(exports.DONE); | ||
|
||
init(); | ||
// remove listener | ||
__parentSymbol.removeEventListener('scroll', onScroll); | ||
|
||
return; | ||
} | ||
|
||
function setOptions(_properties) { | ||
_.extend(options, _properties); | ||
} | ||
|
||
exports.SUCCESS = 1; | ||
exports.ERROR = 0; | ||
exports.DONE = -1; | ||
|
||
exports.setOptions = setOptions; | ||
exports.show = show; | ||
exports.hide = hide; | ||
exports.load = load; | ||
exports.dettach = dettach; | ||
exports.attach = attach; | ||
exports.state = state; | ||
exports.dettach = dettach; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
".is": { | ||
height: 0 | ||
top: 0, | ||
|
||
width: Ti.UI.FILL, | ||
height: 50, | ||
} | ||
|
||
".isIndicator": { | ||
style: Ti.UI.ActivityIndicatorStyle.DARK | ||
style: Ti.UI.ActivityIndicatorStyle.DARK | ||
} | ||
|
||
".isIndicator[platform=ios]": { | ||
style: Ti.UI.iPhone.ActivityIndicatorStyle.DARK | ||
style: Ti.UI.iPhone.ActivityIndicatorStyle.DARK | ||
} | ||
|
||
".isText": { | ||
color: '#777', | ||
font: { | ||
fontSize: 13, | ||
fontWeight: 'bold' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<Alloy> | ||
<FooterView> | ||
<View id="is" class="stl"> | ||
<ActivityIndicator id="isIndicator" class="isIndicator" /> | ||
</View> | ||
</FooterView> | ||
<FooterView> | ||
<View id="is" class="is"> | ||
<Label id="isText" class="isText" /> | ||
<ActivityIndicator id="isIndicator" class="isIndicator" /> | ||
</View> | ||
</FooterView> | ||
</Alloy> |
Oops, something went wrong.