browser-sync-client
Advanced tools
Comparing version 0.0.2 to 0.0.3
var gulp = require("gulp"); | ||
var karma = require('gulp-karma'); | ||
var jshint = require('gulp-jshint'); | ||
var contribs = require('gulp-contribs'); | ||
@@ -27,6 +28,13 @@ var testFiles = [ | ||
gulp.task('lint', function () { | ||
gulp.src(['test/*.js', 'lib/*']) | ||
gulp.src(['test/client-script/*.js', 'lib/*', 'test/middleware/*.js']) | ||
.pipe(jshint('test/.jshintrc')) | ||
.pipe(jshint.reporter('default')) | ||
.pipe(jshint.reporter("default")) | ||
.pipe(jshint.reporter("fail")) | ||
}); | ||
gulp.task('contribs', function () { | ||
gulp.src('README.md') | ||
.pipe(contribs()) | ||
.pipe(gulp.dest("./")) | ||
}); | ||
gulp.task('default', ["lint", "test"]); |
@@ -123,7 +123,2 @@ /*global window*/ | ||
var yPos = browserSyncActions.getScrollFromHref(window.location.href); | ||
if (yPos) { | ||
window.scrollTo(0, yPos); | ||
} | ||
if (ghostMode.forms) { | ||
@@ -246,36 +241,6 @@ | ||
if (confirm) { | ||
window.location.href = this.getPath(window.location.href, ghost.getScrollTop()); | ||
window.location.reload(); | ||
} | ||
}, | ||
/** | ||
* Retrieve y scroll from href | ||
* @param {String} href | ||
* @returns {String|Boolean} | ||
*/ | ||
getScrollFromHref: function (href) { | ||
var match = /bs_page_y=(\d{1,10})/.exec(href); | ||
if (match) { | ||
return match[1]; | ||
} | ||
return false; | ||
}, | ||
/** | ||
* @param {String} current | ||
* @param {Number} scrollTop | ||
* @returns {String} | ||
*/ | ||
getPath: function (current, scrollTop) { | ||
var regex = /(bs_page_y=\d{1,5})/; | ||
var prefix = "?"; | ||
if (current.match(regex)) { | ||
return current.replace(regex, function () { | ||
return "bs_page_y=" + scrollTop; | ||
}); | ||
} | ||
if (current.match(/\?/)) { | ||
prefix = "&"; | ||
} | ||
return current + prefix + "bs_page_y=" + scrollTop; | ||
}, | ||
/** | ||
* @param {HTMLElement} elem | ||
@@ -541,2 +506,23 @@ * @param {String} attr | ||
}, | ||
/** | ||
* Save scroll position in cookies | ||
*/ | ||
saveScroll: function () { | ||
document.cookie = "bs_scroll_pos=" + ghost.getScrollPosition().join(","); | ||
}, | ||
/** | ||
* Replay saved scroll position from cookies | ||
*/ | ||
replayScroll: function () { | ||
if (document.readyState !== "complete") { | ||
return; | ||
} | ||
var pos = document.cookie.replace(/(?:(?:^|.*;\s*)bs_scroll_pos\s*\=\s*([^;]*).*$)|^.*$/, "$1"); | ||
if (typeof pos === "string") { | ||
pos = pos.split(","); | ||
window.scrollTo(pos[0], pos[1]); | ||
} | ||
}, | ||
listeners: { | ||
@@ -710,4 +696,6 @@ scroll: function () { | ||
socket.on("location", function (data) { | ||
if (data.url) { | ||
if (data.url && data.url !== window.location.pathname) { | ||
window.location = data.url; | ||
} else { | ||
window.location.reload(); | ||
} | ||
@@ -791,2 +779,11 @@ }); | ||
// save scroll position just before page is unloaded | ||
window[ghost.utils.eventListener](ghost.utils.prefix + "beforeunload", ghost.saveScroll, false); | ||
// replaying scroll when DOM is ready | ||
document[ghost.utils.eventListener](ghost.utils.prefix + "DOMContentLoaded", ghost.replayScroll, false); | ||
// fallback for IE8 and older | ||
document[ghost.utils.eventListener](ghost.utils.prefix + "readystatechange", ghost.replayScroll, false); | ||
}(window, (typeof ___socket___ === "undefined") ? {} : ___socket___)); |
{ | ||
"name": "browser-sync-client", | ||
"description": "Client-side scripts for BrowserSync", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/shakyshane/browser-sync-client", | ||
@@ -28,3 +28,3 @@ "author": { | ||
"scripts": { | ||
"test": "karma start test/karma.conf.ci.js" | ||
"test": "gulp lint && karma start test/karma.conf.ci.js" | ||
}, | ||
@@ -39,5 +39,6 @@ "devDependencies": { | ||
"karma-mocha": "~0.1.1", | ||
"karma-sinon": "~1.0.0" | ||
"karma-sinon": "~1.0.0", | ||
"gulp-contribs": "0.0.2" | ||
}, | ||
"keywords": [] | ||
} |
# browser-sync-client [![Build Status](https://travis-ci.org/shakyShane/browser-sync-client.png?branch=master)](https://travis-ci.org/shakyShane/browser-sync-client) | ||
## Contributing | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). | ||
Client-side script for BrowserSync | ||
## Release History | ||
_(Nothing yet)_ | ||
## Contributors | ||
``` | ||
10 Shane Osbourne | ||
1 Sergey Slipchenko | ||
``` | ||
## License | ||
Copyright (c) 2014 Shane Osbourne | ||
Licensed under the MIT license. |
@@ -51,74 +51,2 @@ describe("Injecting Styles:", function () { | ||
describe("adding page_y to href", function () { | ||
var baseUrl; | ||
beforeEach(function () { | ||
baseUrl = "http://0.0.0.0:3002"; | ||
}); | ||
describe("adding when it doesn't exist", function () { | ||
it("should append page_y", function () { | ||
var actual = actions.getPath(baseUrl, 200); | ||
var expected = baseUrl + "?bs_page_y=200"; | ||
assert.equal(actual, expected); | ||
}); | ||
it("should append page_y", function () { | ||
var actual = actions.getPath(baseUrl, 481); | ||
var expected = baseUrl + "?bs_page_y=481"; | ||
assert.equal(actual, expected); | ||
}); | ||
}); | ||
describe("Updating an existing URL", function () { | ||
it("should update an existing page_y", function () { | ||
var oldBaseUrl = baseUrl + "?bs_page_y=200"; | ||
var actual = actions.getPath(oldBaseUrl, 481); | ||
var expected = baseUrl + "?bs_page_y=481"; | ||
assert.equal(actual, expected); | ||
}); | ||
}); | ||
describe("adding when a query exists", function () { | ||
it("should update an existing page_y", function () { | ||
var oldBaseUrl = baseUrl + "?some_ramnom=200"; | ||
var actual = actions.getPath(oldBaseUrl, 481); | ||
var expected = baseUrl + "?some_ramnom=200&bs_page_y=481"; | ||
assert.equal(actual, expected); | ||
}); | ||
it("should update an existing page_y", function () { | ||
var oldBaseUrl = baseUrl + "/index.html/?rel=2335t34&some_ramnom=200"; | ||
var actual = actions.getPath(oldBaseUrl, 481); | ||
var expected = baseUrl + "/index.html/?rel=2335t34&some_ramnom=200&bs_page_y=481"; | ||
assert.equal(actual, expected); | ||
}); | ||
}); | ||
describe("Getting the value from a href", function () { | ||
it("should update an existing page_y", function () { | ||
baseUrl = baseUrl + "?bs_page_y=200"; | ||
var actual = actions.getScrollFromHref(baseUrl); | ||
var expected = "200"; | ||
assert.equal(actual, expected); | ||
}); | ||
it("should update an existing page_y", function () { | ||
baseUrl = baseUrl + "?bs_page_y=300"; | ||
var actual = actions.getScrollFromHref(baseUrl); | ||
var expected = "300"; | ||
assert.equal(actual, expected); | ||
}); | ||
it("should update an existing page_y", function () { | ||
baseUrl = baseUrl + "/?rel=2345&bs_page_y=2300"; | ||
var actual = actions.getScrollFromHref(baseUrl); | ||
var expected = "2300"; | ||
assert.equal(actual, expected); | ||
}); | ||
it("should return false if not exists", function () { | ||
baseUrl = baseUrl + "/about.php?rel=2354&bs_pa=2300"; | ||
var actual = actions.getScrollFromHref(baseUrl); | ||
assert.equal(actual, false); | ||
}); | ||
it("should return false if not exists", function () { | ||
var actual = actions.getScrollFromHref(baseUrl); | ||
assert.equal(actual, false); | ||
}); | ||
}); | ||
}); | ||
describe("Getting Tag names", function () { | ||
@@ -125,0 +53,0 @@ |
var assert = require("chai").assert; | ||
var middleware = require("../../index.js").middleware; | ||
var connect = require("connect"); | ||
var http = require("http"); | ||
var request = require('supertest'); | ||
var express = require('express'); | ||
var request = require("supertest"); | ||
var express = require("express"); | ||
var app = express(); | ||
@@ -22,4 +21,4 @@ | ||
request(app) | ||
.get('/client') | ||
.expect('Content-Type', /text\/javascript/) | ||
.get("/client") | ||
.expect("Content-Type", /text\/javascript/) | ||
.expect(200) | ||
@@ -26,0 +25,0 @@ .end(function (err, res) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15
284157
9
2457