Skip to content

Commit

Permalink
Merge pull request #13 from dlockhart/framed-support
Browse files Browse the repository at this point in the history
framed support
  • Loading branch information
dlockhart committed Jul 28, 2015
2 parents b817562 + 2aaf5cf commit 6390ef8
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 163 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ region subtag, separated by a hyphen (e.g. "en-US", "fr-CA").
```javascript
var localeProvider = require('frau-locale-provider');

var langTag = localeProvider.getLangTag();
console.log(langTag); // -> e.g. "en-GB"
localeProvider.getLangTag().then(function(langTag) {
console.log(langTag); // -> e.g. "en-GB"
});
```

### isRtl()
Expand All @@ -34,8 +35,9 @@ direction.
```javascript
var localeProvider = require('frau-locale-provider');

var isRtl = localeProvider.isRtl();
console.log(isRtl); // -> true or false
localeProvider.isRtl().then(function(isRtl) {
console.log(isRtl); // -> true or false
});
```

## Contributing
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frau-locale-provider",
"version": "0.2.1",
"version": "0.3.0",
"description": "Free range app utility for providing information about the current user's locale within the ILP",
"main": "src/index.js",
"scripts": {
Expand All @@ -26,6 +26,10 @@
"url": "https://github.com/Brightspace/frau-locale-provider/issues"
},
"homepage": "https://github.com/Brightspace/frau-locale-provider",
"dependencies": {
"ifrau": "^0.7.1",
"q": "^1.4.1"
},
"devDependencies": {
"chai": "^1.10.0",
"coveralls": "^2.11.2",
Expand Down
11 changes: 11 additions & 0 deletions src/framed/getHtmlLang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var config = require('../config'),
Client = require('ifrau').Client;

module.exports = function() {
var client = new Client();
return client.connect().then(function() {
return client.request('lang');
});
};
10 changes: 10 additions & 0 deletions src/framed/isRtl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var Client = require('ifrau').Client;

module.exports = function() {
var client = new Client();
return client.connect().then(function() {
return client.request('isRtl');
});
};
31 changes: 0 additions & 31 deletions src/getHtmlLang.js

This file was deleted.

16 changes: 11 additions & 5 deletions src/getLangTag.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
'use strict';

var getHtmlLang = require('./getHtmlLang'),
var isFramed = require('./isFramed'),
getHtmlLangFramed = require('./framed/getHtmlLang'),
getHtmlLangLocal = require('./local/getHtmlLang'),
resolveLang = require('./resolveLang');

function get() {
var value = exports._getHtmlLang();
var langTag = exports._resolveLang( value.lang, value.fallback );
return langTag;
var getHtmlLang = isFramed() ? exports._getHtmlLangFramed
: exports._getHtmlLangLocal;
return getHtmlLang().then(function(value) {
var langTag = exports._resolveLang( value.lang, value.fallback );
return langTag;
});
}

var exports = {
get: get,
_getHtmlLang: getHtmlLang,
_getHtmlLangFramed: getHtmlLangFramed,
_getHtmlLangLocal: getHtmlLangLocal,
_resolveLang: resolveLang
};

Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
'use strict';

var isFramed = require('./isFramed'),
isRtlLocal = require('./local/isRtl'),
isRtlFramed = require('./framed/isRtl');

module.exports.getLangTag = require('./getLangTag').get;
module.exports.isRtl = require('./isRtl');
module.exports.isRtl = function() {
var isRtl = isFramed() ? isRtlFramed : isRtlLocal;
return isRtl();
};
5 changes: 5 additions & 0 deletions src/isFramed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function() {
return !window.D2L;
};
14 changes: 0 additions & 14 deletions src/isRtl.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/local/getHtmlLang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var config = require('../config'),
Q = require('q');

module.exports = function() {
return Q.fcall(function() {
if(!window || !window.document) {
return { lang: config.defaultLangTag };
}

var elems = window.document.getElementsByTagName('html');
if( elems.length === 0 ) {
return { lang: config.defaultLangTag };
}

var lang = elems[0].getAttribute('lang');
if( lang === null ) {
return { lang: config.defaultLangTag };
}

var fallback = elems[0].getAttribute('data-lang-default');

var value = {
lang: lang
};
if(fallback !== null) {
value.fallback = fallback;
}
return value;
});
};
13 changes: 13 additions & 0 deletions src/local/isRtl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var Q = require('q');

module.exports = function() {
return Q.fcall(function() {
if(!window || !window.document || !window.document.body) {
return false;
}
var value = (window.document.body.dir.toLowerCase() === 'rtl');
return value;
});
};
9 changes: 9 additions & 0 deletions src/local/thenable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(value) {
return {
then: function(resolve) {
setTimeout(function() { resolve(value); }, 0);
}
};
};
117 changes: 65 additions & 52 deletions test/getHtmlLang.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,85 @@

var config = require('../src/config'),
expect = require('chai').expect,
getHtmlLang = require('../src/getHtmlLang'),
getHtmlLangLocal = require('../src/local/getHtmlLang'),
sinon = require('sinon');

describe('getHtmlLang', function() {

var getElementsByTagName, getAttribute;
describe('local', function() {

beforeEach(function() {
var getElementsByTagName, getAttribute;

getAttribute = sinon.stub();
beforeEach(function() {

getElementsByTagName = sinon.stub()
.returns([{ getAttribute: getAttribute }]);
getAttribute = sinon.stub();

global.window = {
document: {
body: {
dir: 'blah'
},
getElementsByTagName: getElementsByTagName
}
};
getElementsByTagName = sinon.stub()
.returns([{ getAttribute: getAttribute }]);

});
global.window = {
D2L: {},
document: {
body: {
dir: 'blah'
},
getElementsByTagName: getElementsByTagName
}
};

[
/* window is undefined */
function() { global.window = undefined; },
/* window.document is undefined */
function() { global.window.document = undefined; },
/* no HTML elements */
function() {
global.window.document.getElementsByTagName = sinon.stub()
.returns([]);
},
/* no lang attribute on HTML element */
function() {
global.window.document.getElementsByTagName = sinon.stub()
.returns([{getAttribute: sinon.stub().returns(null)}]);
}
].forEach(function(func,index) {
it('should return default ' + ( index + 1 ), function() {
func();
var value = getHtmlLang();
expect(value.lang).to.equal(config.defaultLangTag);
});
});

it('should return HTML element lang attribute value', function() {
getAttribute.withArgs('lang').returns('ab-CD');
var value = getHtmlLang();
expect(getElementsByTagName.calledWith('html')).to.be.true;
expect(getAttribute.calledWith('lang')).to.be.true;
expect(value.lang).to.equal('ab-CD');
});
[
/* window is undefined */
function() { global.window = undefined; },
/* window.document is undefined */
function() { global.window.document = undefined; },
/* no HTML elements */
function() {
global.window.document.getElementsByTagName = sinon.stub()
.returns([]);
},
/* no lang attribute on HTML element */
function() {
global.window.document.getElementsByTagName = sinon.stub()
.returns([{getAttribute: sinon.stub().returns(null)}]);
}
].forEach(function(func,index) {
it('should return default ' + ( index + 1 ), function(done) {
func();
getHtmlLangLocal().then(function(value) {
expect(value.lang).to.equal(config.defaultLangTag);
done();
});
});
});

it('should return null fallback when no data attribute', function() {
getAttribute.withArgs('data-lang-default').returns(null);
var value = getHtmlLang();
expect(value.fallback).to.not.be.defined;
});
it('should return HTML element lang attribute value', function(done) {
getAttribute.withArgs('lang').returns('ab-CD');
getHtmlLangLocal().then(function(value) {
expect(getElementsByTagName.calledWith('html')).to.be.true;
expect(getAttribute.calledWith('lang')).to.be.true;
expect(value.lang).to.equal('ab-CD');
done();
});
});

it('should return null fallback when no data attribute', function(done) {
getAttribute.withArgs('data-lang-default').returns(null);
getHtmlLangLocal().then(function(value) {
expect(value.fallback).to.not.be.defined;
done();
});
});

it('should return fallback value', function(done) {
getAttribute.withArgs('data-lang-default').returns('de-FG');
getHtmlLangLocal().then(function(value) {
expect(value.fallback).to.equal('de-FG');
done();
});
});

it('should return fallback value', function() {
getAttribute.withArgs('data-lang-default').returns('de-FG');
var value = getHtmlLang();
expect(value.fallback).to.equal('de-FG');
});

});

0 comments on commit 6390ef8

Please sign in to comment.