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

feature(storage): add storage API to app element #30

Closed
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ workflows.
Example Usage:

```html
<firebase-app auth-domain="polymerfire-test.firebaseapp.com"
<firebase-app
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com/"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g">
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
storage-url="polymerfire-test.appspot.com">
</firebase-app>
<firebase-auth id="auth" user="{{user}}" provider="google" on-error="handleError">
</firebase-auth>
Expand Down Expand Up @@ -120,5 +122,3 @@ Polymer({
});
</script>
```


7 changes: 5 additions & 2 deletions demo/firebase-auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@
</head>
<body>
<template id="app" is="dom-bind">
<firebase-app auth-domain="polymerfire-test.firebaseapp.com"
<firebase-app
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com/"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"></firebase-app>
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
storage-url="polymerfire-test.appspot.com">
</firebase-app>
<firebase-auth id="auth" user="{{user}}" provider="google" on-error="showError"></firebase-auth>

<div class="error" hidden$="[[!error]]">[[error.code]]: [[error.message]]</div>
Expand Down
79 changes: 79 additions & 0 deletions demo/firebase-storage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>
<!--
@license
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://github.com/firebase/polymerfire/blob/master/LICENSE
-->

<html>
<head>
<meta charset="utf-8">
<title>firebase-auth demo</title>
<script src="../../webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../firebase-app.html">
<link rel="import" href="../firebase-storage.html">
<style media="screen">
body {
font-family: Roboto, sans-serif;
padding: 36px;
}
button {
background: #2196F3;
color: white;
border: 0;
border-radius: 3px;
text-transform: uppercase;
padding: 8px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
}

</style>
</head>
<body>
<template id="app" is="dom-bind">
<firebase-app
name="storage-app"
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com/"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
storage-url="polymerfire-test.appspot.com">
</firebase-app>
<firebase-storage
id="storage"
app-name="storage-app"
path="images/">
</firebase-storage>

<button on-tap="openDialog">Upload File</button>
<input type="file" id="fileInput" on-change="fileChange" hidden>
</template>
<script type="text/javascript">
document.addEventListener('WebComponentsReady', function() {
var app = document.getElementById('app');
app.error = null;

app.fileChange = function(e) {
var file = e.target.files[0];
this.$.storage._uploadFile(file).then(function(response) {
console.log("Success!", response);
}, function(error) {
console.error("Failed!", error);
});
};
app.openDialog = function() {
var elem = this.$.fileInput;
if (elem && document.createEvent) { // sanity check
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
elem.dispatchEvent(evt);
}
};
});
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
name="notes"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com">
database-url="https://polymerfire-test.firebaseio.com/"
storage-url="polymerfire-test.appspot.com">
</firebase-app>

<!-- Instantiate the Notes app: -->
Expand Down
88 changes: 88 additions & 0 deletions firebase-storage-behavior.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!--
@license
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://github.com/firebase/polymerfire/blob/master/LICENSE
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="firebase-common-behavior.html">
<link rel="import" href="firebase.html">
<script>
(function() {
'use strict';

Polymer.FirebaseStorageBehaviorImpl = {
properties: {

/**
* App storage
*/
storage: {
type: Object,
computed: '__computeStorage(app)'
},

/**
* Storage reference to a certain path
*/
ref: {
type: Object,
computed: '__computeRef(storage, path)'
},

/**
* Path to a storage root or endpoint. N.B. `path` is case sensitive.
*/
path: {
type: String,
value: null
},

/**
* Progress of the current transaction in percents
*/
progress: {
type: Number,
value: 0
}
},

__computeStorage: function(app) {
return app ? app.storage() : null;
},

__computeRef: function(storage, path) {

if (storage == null ||
path == null ||
path.split('/').slice(1).indexOf('') >= 0) {
return null;
}
return storage.ref(path);
},

_uploadFile: function(file, metadata) {

var uploadTask = this.storage.ref(this.path + file.name).put(file, metadata);

return new Promise(function(resolve, reject) {
uploadTask.on('state_changed', function(snapshot){
this.progress = snapshot.bytesTransferred / snapshot.totalBytes * 100;
}, function(error) {
reject(error);
}, function() {
resolve(uploadTask.snapshot.downloadURL);
});
}.bind(this));
},

};

Polymer.FirebaseStorageBehavior = [
Polymer.FirebaseStorageBehaviorImpl,
Polymer.FirebaseCommonBehavior
];
})();
</script>
31 changes: 31 additions & 0 deletions firebase-storage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
@license
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://github.com/firebase/polymerfire/blob/master/LICENSE
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="firebase-storage-behavior.html">
<link rel="import" href="firebase.html">

<dom-module id="firebase-storage">

<template>

</template>
<script>
(function() {
'use strict';

Polymer({
is: 'firebase-storage',

behaviors: [
Polymer.FirebaseStorageBehavior
],
});
})();
</script>
</dom-module>
1 change: 0 additions & 1 deletion test/firebase-auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,3 @@
});
</script>
</body>

9 changes: 6 additions & 3 deletions test/firebase-common-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@
name="test"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com">
database-url="https://polymerfire-test.firebaseio.com"
storage-url="polymerfire-test.appspot.com">
</firebase-app>

<firebase-app
name="alt"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com">
database-url="https://polymerfire-test.firebaseio.com"
storage-url="polymerfire-test.appspot.com">
</firebase-app>

<firebase-app
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com">
database-url="https://polymerfire-test.firebaseio.com"
storage-url="polymerfire-test.appspot.com">
</firebase-app>

<test-fixture id="BasicCommon">
Expand Down
3 changes: 2 additions & 1 deletion test/firebase-database-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
name="test"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com">
database-url="https://polymerfire-test.firebaseio.com"
storage-url="polymerfire-test.appspot.com">
</firebase-app>

<test-fixture id="BasicDatabase">
Expand Down
3 changes: 2 additions & 1 deletion test/firebase-document.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
name="test"
api-key="AIzaSyDTP-eiQezleFsV2WddFBAhF_WEzx_8v_g"
auth-domain="polymerfire-test.firebaseapp.com"
database-url="https://polymerfire-test.firebaseio.com">
database-url="https://polymerfire-test.firebaseio.com"
storage-url="polymerfire-test.appspot.com">
</firebase-app>

<test-fixture id="BasicStorage">
Expand Down
3 changes: 2 additions & 1 deletion test/firebase-query.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
name="test"
api-key="AIzaSyBzKhxNa2k9pA3m9_Ji3POFAKyGGFnyshI"
auth-domain="note-app-firebase-3a483.firebaseapp.com"
database-url="https://note-app-firebase-3a483.firebaseio.com">
database-url="https://note-app-firebase-3a483.firebaseio.com"
storage-url="note-app-firebase-3a483.appspot.com">
</firebase-app>

<test-fixture id="BasicQuery">
Expand Down