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

Hello scope #403

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ module.exports = function(grunt) {
'src/hello.js',
'src/hello.chromeapp.js',
'src/hello.phonegap.js',
'src/deprecated.js',
'src/hello.amd.js',
'src/hello.commonjs.js'
],
'dist/hello.all.js': [
'src/hello.polyfill.js',
'src/hello.js',
'src/hello.chromeapp.js',
'src/deprecated.js',
'src/modules/dropbox.js',
'src/modules/facebook.js',
'src/modules/flickr.js',
Expand Down
113 changes: 111 additions & 2 deletions assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,8 @@ var tests = [
// BEFORE SETUPS

function scopeFilter(test) {
return hello.services[test.network].scope[test.method];
var scope = hello.services[test.network].scope_map;
return scope ? scope[test.method] : undefined;
}


Expand Down Expand Up @@ -950,7 +951,7 @@ function Test(test,network,parent){
test.data = document.getElementById(parent.formId());

// Format the data now
hello.utils.dataToJSON(test);
dataToJSON(test);

/*
var form = document.getElementById(parent.formId());
Expand Down Expand Up @@ -1259,6 +1260,114 @@ function _indexOf(a,s){
}


// DataToJSON
// This takes a FormElement|NodeList|InputElement|MixedObjects and convers the data object to JSON.
function dataToJSON(p) {

var _this = this;
var w = window;
var data = p.data;

// Is data a form object
if (domInstance('form', data)) {
data = nodeListToJSON(data.elements);
}
else if ('NodeList' in w && data instanceof NodeList) {
data = nodeListToJSON(data);
}
else if (domInstance('input', data)) {
data = nodeListToJSON([data]);
}

// Is data a blob, File, FileList?
if (('File' in w && data instanceof w.File) ||
('Blob' in w && data instanceof w.Blob) ||
('FileList' in w && data instanceof w.FileList)) {
data = {file: data};
}

// Loop through data if it's not form data it must now be a JSON object
if (!('FormData' in w && data instanceof w.FormData)) {

for (var x in data) if (data.hasOwnProperty(x)) {

if ('FileList' in w && data[x] instanceof w.FileList) {
if (data[x].length === 1) {
data[x] = data[x][0];
}
}
else if (domInstance('input', data[x]) && data[x].type === 'file') {
continue;
}
else if (domInstance('input', data[x]) ||
domInstance('select', data[x]) ||
domInstance('textArea', data[x])) {
data[x] = data[x].value;
}
else if (domInstance(null, data[x])) {
data[x] = data[x].innerHTML || data[x].innerText;
}
}
}

p.data = data;
return data;
}

// NodeListToJSON
// Given a list of elements extrapolate their values and return as a json object
function nodeListToJSON(nodelist) {

var json = {};

// Create a data string
for (var i = 0; i < nodelist.length; i++) {

var input = nodelist[i];

// If the name of the input is empty or diabled, dont add it.
if (input.disabled || !input.name) {
continue;
}

// Is this a file, does the browser not support 'files' and 'FormData'?
if (input.type === 'file') {
json[input.name] = input;
}
else {
json[input.name] = input.value || input.innerHTML;
}
}

return json;
}


// Return the type of DOM object
function domInstance(type, data) {
var test = 'HTML' + (type || '').replace(
/^[a-z]/,
function(m) {
return m.toUpperCase();
}

) + 'Element';

if (!data) {
return false;
}

if (window[test]) {
return data instanceof window[test];
}
else if (window.Element) {
return data instanceof window.Element && (!type || (data.tagName && data.tagName.toLowerCase() === type));
}
else {
return (!(data instanceof Object || data instanceof Array || data instanceof String || data instanceof Number) && data.tagName && data.tagName.toLowerCase() === type);
}
}


self.getText = function getText(path, callback){
// Load in the templates for API calls
Expand Down
4 changes: 2 additions & 2 deletions demos/instagram.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>hello( instagram )</h1>
instagram.login().then( function(){

// Get Profile
instagram.api('me').then( profileHandler, errorHandler);
instagram.api('me').then(profileHandler, errorHandler);

// Get user photos
instagram.api('me/photos').then( photosHandler, errorHandler );
Expand Down Expand Up @@ -157,4 +157,4 @@ <h2>Like</h2>
console.log(str);
document.getElementById('result').appendChild(document.createTextNode(str));
}
</script>
</script>
9 changes: 6 additions & 3 deletions demos/promises.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ <h3>Example Promise.all</h3>

<script class="pre">
function friendsAndContacts(network){

var hi = hello(network);

// Make a login call and handle the response using promises
hello(network).login({scope:'friends'}).then(function(){
hi.login({scope:'friends'}).then(function(){
console.log('fullfilled', 'making api call');
// Reurn another promise to get the users profile.
return Promise.all([
hello( network ).api( 'me/friends' ),
hello( network ).api( 'me/contacts' )
hi.api('me/friends'),
hi.api('me/contacts')
]);
}).then(function(all){

Expand Down
26 changes: 12 additions & 14 deletions demos/signin-oauth1.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@

<h1>HelloJS w/ OAuth 1.0&amp;1.0a</h1>

<button id='yahoo' onclick="hello.login('yahoo');">yahoo</button>
<button id='dropbox' onclick="hello.login('dropbox');">dropbox</button>
<button id='twitter' onclick="hello.login('twitter');">twitter</button>
<button id='yahoo' onclick="login('yahoo');">yahoo</button>
<button id='dropbox' onclick="login('dropbox');">dropbox</button>
<button id='twitter' onclick="login('twitter');">twitter</button>

<script class="pre">

hello.on('auth.login', function(r){
// Get Profile
hello(r.network).api('me', function(p){
document.getElementById(r.network).innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to "+ r.network+" as " + p.name;
});
});

hello.on('auth.failed', function(r){
console.log(r);
});
function login(network) {
var hi = hello(network);
hi.login().then(function() {
return hi.api('me');
}).then(function(p) {
document.getElementById(network).innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to "+ network+" as " + p.name;
}, console.error.bind(console));
}

hello.init({
yahoo : YAHOO_CLIENT_ID,
Expand All @@ -41,4 +39,4 @@ <h1>HelloJS w/ OAuth 1.0&amp;1.0a</h1>
redirect_uri:'../redirect.html'
});

</script>
</script>
75 changes: 37 additions & 38 deletions demos/vimeo.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
<!DOCTYPE html>
<link rel="stylesheet" href="/adorn/adorn.css"/>
<script src="/adorn/adorn.js" async></script>
<script src="client_ids.js"></script>

<title>hello( vimeo )</title>
<h1>hello( vimeo )</h1>
<button onclick="login('vimeo');">Login Vimeo</button>

<pre class="response"></pre>

<script src="../src/hello.js" class="pre"></script>
<script src="../src/modules/vimeo.js" class="pre"></script>

<script class="pre">
function login(network){

var app = hello(network);
app.login()
.then(function() {
app.api('me', function(r) {
document.body.querySelector('.response').appendChild(document.createTextNode(JSON.stringify(r, true, 2)));
});
})
.then(null, console.error.bind(console));
}
</script>

<p>Initiate the library</p>

<script class="pre">
hello.init({
vimeo : "69000585ece5a658a7d57911da381472ab4bc406"
},{
oauth_proxy: OAUTH_PROXY_URL,
redirect_uri:'../redirect.html'
});
</script>
<!DOCTYPE html>
<link rel="stylesheet" href="/adorn/adorn.css"/>
<script src="/adorn/adorn.js" async></script>
<script src="client_ids.js"></script>

<title>hello( vimeo )</title>
<h1>hello( vimeo )</h1>
<button onclick="login('vimeo');">Login Vimeo</button>

<pre class="response"></pre>

<script src="../src/hello.js" class="pre"></script>
<script src="../src/modules/vimeo.js" class="pre"></script>

<script class="pre">
function login(network){

var app = hello(network);
app.login().then(function() {
app.api('me', function(r) {
document.body.querySelector('.response').appendChild(document.createTextNode(JSON.stringify(r, true, 2)));
});
})
.then(null, console.error.bind(console));
}
</script>

<p>Initiate the library</p>

<script class="pre">
hello.init({
vimeo : "69000585ece5a658a7d57911da381472ab4bc406"
},{
oauth_proxy: OAUTH_PROXY_URL,
redirect_uri:'../redirect.html'
});
</script>
2 changes: 1 addition & 1 deletion demos/vk.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>hello( vk )</h1>
return vk.api('me');
})
.then(function(p){
document.getElementById('login').innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to " + r.network+ " as " + p.name;
document.getElementById('login').innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to " + network+ " as " + p.name;
});
});

Expand Down
6 changes: 2 additions & 4 deletions demos/windows.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ <h1>hello( windows )</h1>
function login(network){
var windows = hello(network);

windows
.login()
.then(function(){
windows.login().then(function(){

// Get Profile
return windows.api('me');

}).then(function(p){

// Diplay
document.getElementById('login').innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to "+ r.network+" as " + p.name;
document.getElementById('login').innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to "+ network+" as " + p.name;
});

}
Expand Down
Loading