Skip to content
This repository was archived by the owner on Mar 16, 2023. It is now read-only.

Commit 4b14fe1

Browse files
authored
Merge pull request #50 from octalmage/firebase
Replace Parse.com with Firebase.
2 parents 328829a + 1870281 commit 4b14fe1

File tree

2 files changed

+39
-88
lines changed

2 files changed

+39
-88
lines changed

assets/js/app.js

Lines changed: 36 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ if (process.platform === "darwin")
1414
win.menu = mb;
1515
}
1616

17-
//Parse Variables.
18-
Parse.initialize("VvmNgHcupWn43L9ThaNDiIldMSjOXiLvd7DR7wTq", "DTAv28KMYt5pYlY3Q1yDJ3Tvm2FOViL4io9deBBt");
19-
var Parse_Notes = Parse.Object.extend("Notes");
20-
var Private_Parse_Notes = new Parse_Notes();
17+
// Firebase setup.
18+
var config = {
19+
apiKey: "AIzaSyDtYLpfW_yRF273-N6iwZDHDX7xG8Nt5_g",
20+
authDomain: "marknote-681e3.firebaseapp.com",
21+
databaseURL: "https://marknote-681e3.firebaseio.com",
22+
storageBucket: "marknote-681e3.appspot.com",
23+
messagingSenderId: "809473583891"
24+
};
25+
firebase.initializeApp(config);
26+
2127
var syncing;
2228
var wrap = true;
23-
var parsenoteid;
24-
2529
var marked = require("marked");
2630
var highlight = require("highlight.js");
2731
var editor;
@@ -458,113 +462,60 @@ function render(markdown)
458462
}
459463

460464
/**
461-
* Tries to login to Parse.com
465+
* Tries to login to Firebase
462466
* @param {string} username Parse.com username.
463467
* @param {string} password Parse.com password.
464468
*/
465469
function login(username, password)
466470
{
467-
Parse.User.logIn(username, password,
468-
{
469-
success: function(user)
470-
{
471-
currentuser = Parse.User.current();
472-
if (syncing)
473-
{
474-
parse_getnotes();
475-
}
476-
477-
},
478-
error: function(user, error)
479-
{
480-
signup(username, password);
481-
}
471+
firebase.auth().signInWithEmailAndPassword(username, password).then(function() {
472+
cloud_getnotes();
473+
}).catch(function(error) {
474+
signup(username, password);
482475
});
483476
}
484477

485478
/**
486-
* Parse.com signup.
479+
* Firebase signup.
487480
* @param {string} username Desired username.
488481
* @param {string} password Desired password.
489482
*/
490483
function signup(username, password)
491484
{
492-
var user = new Parse.User();
493-
user.set("username", username);
494-
user.set("password", password);
485+
firebase.auth().createUserWithEmailAndPassword(username, password).catch(function(error) {
486+
var errorCode = error.code;
487+
var errorMessage = error.message;
495488

496-
user.signUp(null,
497-
{
498-
success: function(user)
499-
{
500-
login(username, password);
501-
},
502-
error: function(user, error)
503-
{
504-
if (error.code === 202)
505-
{
506-
$("#syncing").prop("checked", false);
507-
alert("Username taken or password is incorrect.");
508-
}
509-
}
489+
$("#syncing").prop("checked", false);
490+
alert("Username taken or password is incorrect.");
510491
});
511492
}
512493

513494
/**
514495
* Syncs notes from Parse.com to local notes array.
515496
*/
516-
function parse_getnotes()
517-
{
518-
var query = new Parse.Query(Parse_Notes);
519-
query.find(
520-
{
521-
success: function(results)
522-
{
523-
if (results.length < 1)
524-
{
525-
Private_Parse_Notes.set("content", notes);
526-
Private_Parse_Notes.setACL(new Parse.ACL(Parse.User.current()));
527-
Private_Parse_Notes.save(null,
528-
{
529-
success: function(parsenote)
530-
{
531-
parsenoteid = parsenote.id;
532-
}
533-
});
534-
}
535-
else
536-
{
537-
parsenoteid = results[0].id;
538-
if (_.isEqual(notes, results[0].get("content")) === false) //Cloud copy does not match local copy.
539-
{
540-
notes = results[0].get("content"); //Copy the cloud copy into memory.
541-
preloadCache(); //Update the cache.
542-
updateList(); //Update the list.
543-
loadNote(current); //Update the note you're currently viewing.
544-
}
545-
}
546-
},
547-
error: function(error)
548-
{
549-
alert("Error: " + error.code + " " + error.message);
497+
function cloud_getnotes()
498+
{
499+
var userId = firebase.auth().currentUser.uid;
500+
firebase.database().ref('/notes/' + userId).once('value').then(function(snapshot) {
501+
if (snapshot.val()) {
502+
notes = snapshot.val();
503+
preloadCache(); //Update the cache.
504+
updateList(); //Update the list.
505+
loadNote(current); //Update the note you're currently viewing.
506+
} else {
507+
cloud_savenotes();
550508
}
551509
});
552510
}
553511

554512
/**
555513
* Save local notes array to Parse.com
556514
*/
557-
function parse_savenotes()
515+
function cloud_savenotes()
558516
{
559-
var query = new Parse.Query(Parse_Notes);
560-
query.get(parsenoteid,
561-
{
562-
success: function(n)
563-
{
564-
n.set("content", notes);
565-
n.save();
566-
}
567-
});
517+
var userId = firebase.auth().currentUser.uid;
518+
firebase.database().ref('notes/' + userId).set(notes);
568519
}
569520

570521
/**
@@ -633,7 +584,7 @@ function saveNotes()
633584
{
634585
if (syncing)
635586
{
636-
parse_savenotes();
587+
cloud_savenotes();
637588
}
638589
store.save(
639590
{

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<head>
55
<title>Marknote</title>
66
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
7-
7+
88
<!-- Code Themes. -->
99
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/styles/github.min.css">
1010
<!--<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/styles/monokai.min.css">-->
@@ -21,7 +21,7 @@
2121
<script src="http://cdn.craig.is/js/mousetrap/mousetrap.min.js"> </script>
2222
<script src="assets/js/jquery.nearest.js"></script>
2323
<script src="assets/js/jquery.blockUI.js"></script>
24-
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.3.0.min.js"></script>
24+
<script src="https://www.gstatic.com/firebasejs/3.6.4/firebase.js"></script>
2525
<script src="assets/js/underscore.js"></script>
2626
<script src="assets/js/app.js"></script>
2727

@@ -76,7 +76,7 @@
7676
<h3>Settings</h3><br>
7777
Editor word wrap <input id="wrap" type="checkbox"><br>
7878
Syncing <input id="syncing" type="checkbox"><br>
79-
<input type="text" id="username" placeholder="Username:" ><br>
79+
<input type="text" id="username" placeholder="Email:" ><br>
8080
<input type="password" id="password" placeholder="Password:" style="margin-top:10px;"><br>
8181
<button type="button" id="saveButton" class="btn btn-info btn-xs" style="margin-top:10px;">Save</button>
8282

0 commit comments

Comments
 (0)