Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 90 additions & 82 deletions build/js/material-refresh.js
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';
Expand Down Expand Up @@ -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 ">\
Expand Down Expand Up @@ -65,7 +64,7 @@
y2: 0
}

// Default options
// Default options
/* var opts = { */
/* scrollEl: '', //String */
/* nav: '', //String */
Expand All @@ -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;

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?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remade all selectors.


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basePosY = parseIntPx(refreshNav.clientHeight) + 20; might be better?

Copy link
Author

Choose a reason for hiding this comment

The 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();
Expand All @@ -171,7 +179,7 @@
// Destory refresh
mRefresh.destroy = function(){
unbindEvents();
$refreshMain.remove();
refreshMain.parentNode.removeChild(refreshMain);

}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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)'; */

}

Expand All @@ -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;
Expand All @@ -414,9 +422,9 @@
} else if (typeof onEnd === 'function') {
onEnd();
}

isBtnAction = false;

}, 500);
}

Expand All @@ -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);
Loading