Skip to content
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

Add support for specifying parent container #80

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions nprogress.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

#nprogress .bar {
background: #29d;

position: fixed;
z-index: 1031;
position: absolute;
top: 0;
left: 0;

Expand All @@ -33,8 +32,8 @@
/* Remove these to get rid of the spinner */
#nprogress .spinner {
display: block;
position: fixed;
z-index: 1031;
position: absolute;
top: 15px;
right: 15px;
}
Expand All @@ -53,6 +52,16 @@
animation: nprogress-spinner 400ms linear infinite;
}

.nprogress-parent {
overflow: hidden;
position: relative;
}

body.nprogress-parent #nprogress .bar,
body.nprogress-parent #nprogress .spinner {
position: fixed;
}

@-webkit-keyframes nprogress-spinner {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
Expand Down
67 changes: 42 additions & 25 deletions nprogress.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
showSpinner: true,
barSelector: '[role="bar"]',
spinnerSelector: '[role="spinner"]',
parent: 'body',
template: '<div class="bar" role="bar"><div class="peg"></div></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div>'
};

Expand Down Expand Up @@ -82,16 +83,16 @@

if (n === 1) {
// Fade out
css(progress, {
transition: 'none',
opacity: 1
css(progress, {
transition: 'none',
opacity: 1
});
progress.offsetWidth; /* Repaint */

setTimeout(function() {
css(progress, {
transition: 'all ' + speed + 'ms linear',
opacity: 0
css(progress, {
transition: 'all ' + speed + 'ms linear',
opacity: 0
});
setTimeout(function() {
NProgress.remove();
Expand Down Expand Up @@ -177,24 +178,24 @@
/**
* Waits for all supplied jQuery promises and
* increases the progress as the promises resolve.
*
*
* @param $promise jQUery Promise
*/
(function() {
var initial = 0, current = 0;

NProgress.promise = function($promise) {
if (!$promise || $promise.state() == "resolved") {
return this;
}

if (current == 0) {
NProgress.start();
}

initial++;
current++;

$promise.always(function() {
current--;
if (current == 0) {
Expand All @@ -204,10 +205,10 @@
NProgress.set((initial - current) / initial);
}
});

return this;
};

})();

/**
Expand All @@ -219,15 +220,15 @@
if (NProgress.isRendered()) return document.getElementById('nprogress');

addClass(document.documentElement, 'nprogress-busy');

var progress = document.createElement('div');
progress.id = 'nprogress';
progress.innerHTML = Settings.template;

var bar = progress.querySelector(Settings.barSelector),
perc = fromStart ? '-100' : toBarPerc(NProgress.status || 0),
spinner;

css(bar, {
transition: 'all 0 linear',
transform: 'translate3d(' + perc + '%,0,0)'
Expand All @@ -238,7 +239,10 @@
spinner && removeElement(spinner);
}

document.body.appendChild(progress);
var parent = findElement(Settings.parent);
addClass(parent, 'nprogress-parent');
parent.appendChild(progress);

return progress;
};

Expand All @@ -247,7 +251,8 @@
*/

NProgress.remove = function() {
removeClass(document.documentElement, 'nprogress-busy');
var parent = findElement(Settings.parent);
removeClass(parent, 'nprogress-busy');
var progress = document.getElementById('nprogress');
progress && removeElement(progress);
};
Expand Down Expand Up @@ -333,7 +338,7 @@

var queue = (function() {
var pending = [];

function next() {
var fn = pending.shift();
if (fn) {
Expand All @@ -348,10 +353,10 @@
})();

/**
* (Internal) Applies css properties to an element, similar to the jQuery
* (Internal) Applies css properties to an element, similar to the jQuery
* css method.
*
* While this helper does assist with vendor prefixed property names, it
* While this helper does assist with vendor prefixed property names, it
* does not perform any manipulation of values prior to setting styles.
*/

Expand Down Expand Up @@ -392,7 +397,7 @@

return function(element, properties) {
var args = arguments,
prop,
prop,
value;

if (args.length == 2) {
Expand Down Expand Up @@ -423,7 +428,7 @@
var oldList = classList(element),
newList = oldList + name;

if (hasClass(oldList, name)) return;
if (hasClass(oldList, name)) return;

// Trim the opening space.
element.className = newList.substring(1);
Expand All @@ -447,8 +452,8 @@
}

/**
* (Internal) Gets a space separated list of the class names on the element.
* The list is wrapped with a single space on each end to facilitate finding
* (Internal) Gets a space separated list of the class names on the element.
* The list is wrapped with a single space on each end to facilitate finding
* matches within the list.
*/

Expand All @@ -464,6 +469,18 @@
element && element.parentNode && element.parentNode.removeChild(element);
}

/**
* (Internal) Find the parent, which can either be a tag name or id.
*/

function findElement(selector) {
var tags = document.getElementsByTagName(selector);
if (tags.length > 0) {
return tags[0];
}

return document.getElementById(selector);
}

return NProgress;
});

12 changes: 8 additions & 4 deletions support/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ i, b {
font-weight: 400;
}

body, html {
padding: 0;
margin: 0;
}

body {
background: white;
}
Expand Down Expand Up @@ -130,11 +135,10 @@ button {
}

.page-header {
margin: 1.5em auto;
text-align: center;
max-width: 400px;
padding: 0 20px;
margin: 3em auto;
padding: 3em 20px;
margin: 0 auto;
}

.page-header h1 {
Expand Down Expand Up @@ -173,7 +177,7 @@ p.brief.big {
.page-header h1 {
font-size: 3em; }
.page-header {
margin: 4.5em auto 3.5em auto;
padding: 4.5em 20px 3.5em 20px;
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
NProgress.start();
assert.equal(NProgress.status, NProgress.settings.minimum);
});

it('must be attached to specified parent', function() {
var test = $('<div>', {id: 'test'}).appendTo('body');
NProgress.configure({parent: test});
NProgress.start();
assert.isTrue($("#nprogress").parent().is(test));
assert.isTrue($(NProgress.settings.parent).hasClass("nprogress-parent"));
});
});

// ----
Expand All @@ -91,6 +99,19 @@

// ----

describe('.remove()', function() {
it('should be removed from the parent', function() {
NProgress.set(1);
NProgress.remove();

var parent = $(NProgress.settings.parent);
assert.isFalse(parent.hasClass('nprogress-parent'));
assert.equal(parent.find('#nprogress').length, 0);
});
})

// ----

describe('.inc()', function() {
it('should render', function() {
NProgress.inc();
Expand Down