-
Notifications
You must be signed in to change notification settings - Fork 108
Remove jQuery and Zepto dependencies #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blvz
wants to merge
2
commits into
lightningtgc:master
Choose a base branch
from
blvz:no-deps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,8 +1,7 @@ | ||
(function() { | ||
"use strict"; | ||
(function($){ | ||
var $scrollEl = $(document.body); | ||
var $refreshMain, $spinnerWrapper, $arrowWrapper, $arrowMain; | ||
(function() { | ||
var refreshMain, spinnerWrapper, arrowWrapper, arrowMain; | ||
var scrollEl = document.body; | ||
|
||
var noShowClass = 'mui-refresh-noshow'; | ||
|
@@ -31,12 +30,12 @@ | |
var onEnd = null; | ||
var onBtnEnd = null; | ||
var stopAnimatTimeout = null; | ||
|
||
var refreshNav = ''; | ||
|
||
var lastTime = new Date().getTime(); | ||
|
||
var isIOS = $.os.ios; | ||
var isIOS = /ip(hone|ad|od)/i.test(navigator.userAgent); | ||
|
||
var tmpl = '<div id="muiRefresh" class="mui-refresh-main">\ | ||
<div class="mui-refresh-wrapper ">\ | ||
|
@@ -65,7 +64,7 @@ | |
y2: 0 | ||
} | ||
|
||
// Default options | ||
// Default options | ||
/* var opts = { */ | ||
/* scrollEl: '', //String */ | ||
/* nav: '', //String */ | ||
|
@@ -78,78 +77,87 @@ | |
/* onEnd: null //Function */ | ||
/* } */ | ||
|
||
/* Known issue: | ||
* 1. iOS feature when scrolling ,animation will stop | ||
|
||
/* Known issue: | ||
* 1. iOS feature when scrolling ,animation will stop | ||
* 2. Animation display issue in anfroid like miui小米 | ||
* | ||
* | ||
* TODO list: | ||
* 1. Using translate and scale together to replace top | ||
* 1. Using translate and scale together to replace top | ||
* 2. Optimize circle rotate animation | ||
*/ | ||
|
||
function parseIntPx(v) { | ||
var m = v.match('(\d+)px'); | ||
if (m == null) return 0; | ||
return parseInt(m[1], 10); | ||
} | ||
|
||
// Main function to init the refresh style | ||
function mRefresh(options) { | ||
options = options || {}; | ||
|
||
scrollEl = options.scrollEl ? options.scrollEl : | ||
isIOS ? scrollEl : document; | ||
$scrollEl = $(scrollEl); | ||
|
||
// extend options | ||
onBegin = options.onBegin; | ||
onEnd = options.onEnd; | ||
maxRotateTime = options.maxTime || maxRotateTime; | ||
refreshNav = options.nav || refreshNav; | ||
|
||
if ($('#muirefresh').length === 0) { | ||
if (document.querySelectorAll('#muirefresh').length === 0) { | ||
renderTmpl(); | ||
} | ||
|
||
$refreshMain = $('#muiRefresh'); | ||
$spinnerWrapper = $('.mui-spinner-wrapper', $refreshMain); | ||
$arrowWrapper = $('.mui-arrow-wrapper', $refreshMain); | ||
$arrowMain = $('.mui-arrow-main', $refreshMain); | ||
refreshMain = document.querySelector('#muiRefresh'); | ||
spinnerWrapper = refreshMain.querySelector('.mui-spinner-wrapper'); | ||
arrowWrapper = refreshMain.querySelector('.mui-arrow-wrapper'); | ||
arrowMain = refreshMain.querySelector('.mui-arrow-main'); | ||
|
||
// Custom nav bar | ||
// Custom nav bar | ||
if (!isDefaultType()) { | ||
$refreshMain.addClass('mui-refresh-nav'); | ||
basePosY = $(refreshNav).height() + 20; | ||
if($(refreshNav).offset()){ | ||
customNavTop = $(refreshNav).offset().top; | ||
refreshMain.classList.add('mui-refresh-nav'); | ||
basePosY = parseIntPx(refreshNav.style.height) + 20; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally. |
||
var rect; | ||
if(rect = refreshNav.getBoundingClientRect()){ | ||
var top = rect.top + document.body.scrollTop, | ||
left = rect.left + document.body.scrollLeft; | ||
|
||
customNavTop = top; | ||
// Handle position fix | ||
if($(refreshNav).css('position') !== 'fixed'){ | ||
if(refreshNav.style.position !== 'fixed'){ | ||
basePosY += customNavTop; | ||
} | ||
// Set the first Y position | ||
$refreshMain.css('top', customNavTop + 'px'); | ||
refreshMain.style.top = customNavTop + 'px'; | ||
} | ||
|
||
//Set z-index to make sure ablow the nav bar | ||
var navIndex = $(refreshNav).css('z-index'); | ||
$refreshMain.css('z-index', navIndex - 1); | ||
var navIndex = refreshNav.style.zIndex; | ||
refreshMain.style.zIndex = navIndex - 1; | ||
} | ||
|
||
//Set custom z-index | ||
if(options.index){ | ||
$refreshMain.css('z-index', ~~options.index); | ||
refreshMain.style.zIndex = ~~options.index; | ||
} | ||
|
||
//Set custom top, to change the position | ||
if(options.top){ | ||
$refreshMain.css('top', options.top); | ||
refreshMain.style.top = options.top; | ||
} | ||
|
||
// Extract theme | ||
// Extract theme | ||
if (options.theme) { | ||
$refreshMain.addClass(options.theme); | ||
refreshMain.classList.add(options.theme); | ||
} else { | ||
$refreshMain.addClass(blueThemeClass); | ||
refreshMain.classList.add(blueThemeClass); | ||
} | ||
|
||
// Add Animation Class | ||
$refreshMain.addClass(mainAnimatClass); | ||
refreshMain.classList.add(mainAnimatClass); | ||
|
||
if(!options.freeze){ | ||
bindEvents(); | ||
|
@@ -171,7 +179,7 @@ | |
// Destory refresh | ||
mRefresh.destroy = function(){ | ||
unbindEvents(); | ||
$refreshMain.remove(); | ||
refreshMain.parentNode.removeChild(refreshMain); | ||
|
||
} | ||
|
||
|
@@ -190,16 +198,16 @@ | |
if (!isDefaultType()) { | ||
realTargetPos = realTargetPos + NUM_NAV_TARGET_ADDY; | ||
} | ||
|
||
// Handle freeze | ||
$refreshMain.show(); | ||
refreshMain.style.display = ''; | ||
//Romove animat time | ||
$refreshMain.removeClass(mainAnimatClass); | ||
refreshMain.classList.remove(mainAnimatClass); | ||
// move to target position | ||
$refreshMain.css('top', realTargetPos + 'px'); | ||
refreshMain.style.top = realTargetPos + 'px'; | ||
// make it small | ||
$refreshMain.css('-webkit-transform', 'scale(' + 0.01 + ')'); | ||
refreshMain.style.webkitTransform = 'scale(' + 0.01 + ')'; | ||
|
||
setTimeout(doRotate, 60); | ||
} | ||
} | ||
|
@@ -233,8 +241,8 @@ | |
} | ||
|
||
touchCurrentY = basePosY + NUM_POS_START_Y; | ||
$refreshMain.show(); | ||
refreshMain.style.display = ''; | ||
|
||
if (e.touches[0]) { | ||
touchPos.x1 = e.touches[0].pageX; | ||
touchStartY = touchPos.y1 = e.touches[0].pageY; | ||
|
@@ -259,10 +267,10 @@ | |
distanceY = touchPos.y2 - touchPos.y1; | ||
|
||
if (touchPos.y2 - touchStartY + verticalThreshold > 0) { | ||
e.preventDefault(); | ||
e.preventDefault(); | ||
|
||
// Some android phone | ||
// Throttle, aviod jitter | ||
// Throttle, aviod jitter | ||
if (now - lastTime < 90) { | ||
return; | ||
} | ||
|
@@ -288,34 +296,34 @@ | |
return; | ||
} | ||
e.preventDefault(); | ||
|
||
if (touchCurrentY > basePosY - customNavTop + NUM_POS_MIN_Y) { | ||
// Should move over the min position | ||
doRotate(); | ||
} else { | ||
backToStart(); | ||
} | ||
} | ||
|
||
/** | ||
* backToStart | ||
* Return to start position | ||
*/ | ||
function backToStart() { | ||
var realStartPos = basePosY + NUM_POS_START_Y; | ||
if ( isDefaultType() ) { | ||
$refreshMain.css('top', realStartPos + 'px'); | ||
$refreshMain.css('-webkit-transform', 'scale(' + 0 + ')'); | ||
refreshMain.style.top = realStartPos + 'px'; | ||
refreshMain.style.webkitTransform = 'scale(' + 0 + ')'; | ||
} else { | ||
// Distance must greater than NUM_POS_MIN_Y | ||
$refreshMain.css('top', customNavTop + 'px'); | ||
/* $refreshMain.css('-webkit-transform', 'translateY(' + realStartPos + 'px)'); */ | ||
refreshMain.style.top = customNavTop + 'px'; | ||
/* refreshMain.style.webkitTransform = 'translateY(' + realStartPos + 'px)'; */ | ||
} | ||
setTimeout(function(){ | ||
// Handle button action | ||
if(!isShowLoading){ | ||
$refreshMain.css('opacity', 0); | ||
$refreshMain.hide(); | ||
refreshMain.style.opacity = 0; | ||
refreshMain.style.display = 'none'; | ||
} | ||
}, 300); | ||
} | ||
|
@@ -333,15 +341,15 @@ | |
|
||
if (isDefaultType()) { | ||
// Small to Big | ||
$refreshMain.css('-webkit-transform', 'scale(' + scalePer + ')'); | ||
refreshMain.style.webkitTransform = 'scale(' + scalePer + ')'; | ||
} | ||
/* $refreshMain.css('-webkit-transform', 'translateY('+ y + 'px)'); */ | ||
/* refreshMain.style.webkitTransform = 'translateY('+ y + 'px)'; */ | ||
|
||
$refreshMain.css('opacity', scalePer); | ||
refreshMain.style.opacity = scalePer; | ||
// Change the position | ||
$refreshMain.css('top', currMoveY + 'px'); | ||
$arrowMain.css('-webkit-transform', 'rotate(' + -(y * 3) + 'deg)'); | ||
/* $arrowMain.css('transform', 'rotate(' + -(y * 3) + 'deg)'); */ | ||
refreshMain.style.top = currMoveY + 'px'; | ||
arrowMain.style.webkitTransform = 'rotate(' + -(y * 3) + 'deg)'; | ||
/* arrowMain.style.transform = 'rotate(' + -(y * 3) + 'deg)'; */ | ||
|
||
} | ||
|
||
|
@@ -362,49 +370,49 @@ | |
} | ||
|
||
// Make sure display entirely | ||
$refreshMain.css('opacity', 1); | ||
refreshMain.style.opacity = 1; | ||
|
||
if (!isBtnAction) { | ||
if (!isBtnAction) { | ||
var realTargetPos = basePosY + NUM_POS_TARGET_Y - 20; | ||
if (!isDefaultType()) { | ||
realTargetPos = realTargetPos + NUM_NAV_TARGET_ADDY; | ||
} | ||
$refreshMain.css('top', realTargetPos + 'px'); | ||
/* $refreshMain.css('-webkit-transform', 'translateY(' + realTargetPos + 'px)'); */ | ||
refreshMain.style.top = realTargetPos + 'px'; | ||
/* refreshMain.style.webkitTransform = 'translateY(' + realTargetPos + 'px)'; */ | ||
} else { | ||
$refreshMain.addClass(mainAnimatClass); | ||
$refreshMain.css('-webkit-transform', 'scale(' + 1 + ')'); | ||
refreshMain.classList.add(mainAnimatClass); | ||
refreshMain.style.webkitTransform = 'scale(' + 1 + ')'; | ||
} | ||
|
||
$arrowWrapper.hide(); | ||
arrowWrapper.style.display = 'none'; | ||
|
||
// Start animation | ||
$spinnerWrapper.show(); | ||
spinnerWrapper.style.display = ''; | ||
|
||
// Timeout to stop animation | ||
stopAnimatTimeout = setTimeout(recoverRefresh, maxRotateTime); | ||
} | ||
|
||
/** | ||
* Recover Refresh | ||
* Hide the circle | ||
* Hide the circle | ||
*/ | ||
function recoverRefresh(){ | ||
// For aviod resolve | ||
isStoping = true; | ||
|
||
// Stop animation | ||
$refreshMain.addClass(noShowClass); | ||
// Stop animation | ||
refreshMain.classList.add(noShowClass); | ||
|
||
$spinnerWrapper.hide(); | ||
spinnerWrapper.style.display = 'none'; | ||
|
||
setTimeout(function(){ | ||
$refreshMain.removeClass(noShowClass); | ||
$refreshMain.hide(); | ||
refreshMain.classList.remove(noShowClass); | ||
refreshMain.style.display = 'none'; | ||
|
||
backToStart(); | ||
|
||
$arrowWrapper.show(); | ||
arrowWrapper.style.display = ''; | ||
|
||
isShowLoading = false; | ||
isStoping = false; | ||
|
@@ -414,9 +422,9 @@ | |
} else if (typeof onEnd === 'function') { | ||
onEnd(); | ||
} | ||
|
||
isBtnAction = false; | ||
|
||
}, 500); | ||
} | ||
|
||
|
@@ -427,23 +435,23 @@ | |
* @return {Boolen} | ||
*/ | ||
function isDefaultType() { | ||
return $(refreshNav).length === 0; | ||
return !refreshNav.length || document.querySelectorAll(refreshNav).length === 0; | ||
} | ||
|
||
function bindEvents() { | ||
$scrollEl.on('touchstart', touchStart); | ||
$scrollEl.on('touchmove', touchMove); | ||
$scrollEl.on('touchend', touchEnd); | ||
scrollEl.addEventListener('touchstart', touchStart); | ||
scrollEl.addEventListener('touchmove', touchMove); | ||
scrollEl.addEventListener('touchend', touchEnd); | ||
} | ||
|
||
function unbindEvents() { | ||
$scrollEl.off('touchstart', touchStart); | ||
$scrollEl.off('touchmove', touchMove); | ||
$scrollEl.off('touchend', touchEnd); | ||
scrollEl.removeEventListener('touchstart', touchStart); | ||
scrollEl.removeEventListener('touchmove', touchMove); | ||
scrollEl.removeEventListener('touchend', touchEnd); | ||
} | ||
|
||
|
||
window.mRefresh = mRefresh; | ||
|
||
})(Zepto || jQuery); | ||
})(); | ||
}).call(this); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shoud you use
refreshNav = document.querySelector(options.nav) || refreshNav;
here? (Or create query selector below?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remade all selectors.