2014-12-02 17:59:03 +01:00
|
|
|
/**
|
|
|
|
* Created by avim on 12/2/2014.
|
|
|
|
*/
|
2014-12-07 14:12:50 +01:00
|
|
|
/*eslint global-strict:0, no-alert:0*/
|
2014-12-03 17:45:29 +01:00
|
|
|
/*global Firebase:true,alert:true*/
|
2014-12-07 14:12:50 +01:00
|
|
|
define(['react', 'firebase', 'lodash', './fiddle.rt'], function (React, Firebase, _, fiddleTemplate) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function generateRandomId() {
|
|
|
|
var uuid = 'xxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
|
|
var r = _.random(0, 15);
|
|
|
|
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
|
|
});
|
|
|
|
return uuid;
|
|
|
|
}
|
2014-12-03 17:45:29 +01:00
|
|
|
|
2014-12-07 14:12:50 +01:00
|
|
|
var Fiddle = React.createClass({
|
|
|
|
displayName: 'Fiddle',
|
|
|
|
componentDidMount: function () {
|
|
|
|
if (window.location.hash) {
|
|
|
|
var newHash = window.location.hash.replace('#', '');
|
|
|
|
var firebase = new Firebase('https://reacttemplates.firebaseio-demo.com/');
|
|
|
|
firebase.child('fiddles').child(newHash).on('value', function (snapshot) {
|
|
|
|
this.refs.playground.setState(snapshot.val());
|
|
|
|
Firebase.goOffline();
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
Firebase.goOffline();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
save: function () {
|
|
|
|
var newHash = generateRandomId();
|
|
|
|
window.location.hash = newHash;
|
|
|
|
Firebase.goOnline();
|
|
|
|
|
|
|
|
var playgroundState = this.refs.playground.state;
|
2014-12-02 17:59:03 +01:00
|
|
|
var firebase = new Firebase('https://reacttemplates.firebaseio-demo.com/');
|
2014-12-07 14:12:50 +01:00
|
|
|
firebase.child('fiddles').child(newHash).set(playgroundState, function () {
|
2014-12-02 17:59:03 +01:00
|
|
|
Firebase.goOffline();
|
2014-12-07 14:12:50 +01:00
|
|
|
alert('saved the fiddle, you can share your url');
|
2014-12-02 17:59:03 +01:00
|
|
|
}.bind(this));
|
|
|
|
|
2014-12-07 14:12:50 +01:00
|
|
|
},
|
2014-12-02 17:59:03 +01:00
|
|
|
|
2014-12-07 14:12:50 +01:00
|
|
|
render: function () {
|
|
|
|
return fiddleTemplate.apply(this);
|
|
|
|
}
|
|
|
|
});
|
2014-12-02 17:59:03 +01:00
|
|
|
|
2014-12-07 14:12:50 +01:00
|
|
|
return Fiddle;
|
2014-12-02 17:59:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|