var app = angular.module('noteApp', []);
app.directive('notepad', function(notesFactory) {
return {
restrict: 'AE',
scope: {},
link: function(scope, elem, attrs) {
scope.openEditor = function(index){
scope.editMode = true;
if (index !== undefined) {
scope.noteText = notesFactory.get(index).content;
scope.index = index;
} else
scope.noteText = undefined;
};
scope.save = function() {
if (scope.noteText !== "" && scope.noteText !== undefined) {
var note = {};
note.title = scope.noteText.length > 10 ? scope.noteText.substring(0, 10) + '. . .' : scope.noteText;
note.content = scope.noteText;
note.id = scope.index != -1 ? scope.index : localStorage.length;
scope.notes = notesFactory.put(note);
}
scope.restore();
};
scope.restore = function() {
scope.editMode = false;
scope.index = -1;
scope.noteText = "";
};
var editor = elem.find('#editor');
scope.restore();
scope.notes = notesFactory.getAll();
editor.bind('keyup keydown', function() {
scope.noteText = editor.text().trim();
});
},
templateUrl: 'templateurl.html'
};
});
app.factory('notesFactory', function() {
return {
put: function(note) {
localStorage.setItem('note' + note.id, JSON.stringify(note));
return this.getAll();
},
get: function(index) {
return JSON.parse(localStorage.getItem('note' + index));
},
getAll: function() {
var notes = [];
for (var i = 0; i < localStorage.length; i++) {
if (localStorage.key(i).indexOf('note') !== -1) {
var note = localStorage.getItem(localStorage.key(i));
notes.push(JSON.parse(note));
}
}
return notes;
}
};
});
<!DOCTYPE html>
<html ng-app="noteApp">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
<script src="app.js"></script>
</head>
<body>
<h1 class="title">The Note Making App</h1>
<notepad/>
</body>
</html>
@import url(http://fonts.googleapis.com/css?family=The+Girl+Next+Door);
* {
font-family: 'The Girl Next Door', cursive;
}
.title {
text-align: center;
}
notepad {
display: block;
width: 500px;
padding: 10px 10px;
margin: auto;
}
.note-area {
height: 450px;
background: url("http://extremecss.com/demos/paper.jpg") center center no-repeat;
overflow-y: scroll;
margin-bottom: 10px;
}
body {
background: #f2f2f2;
}
#editor {
padding: 10px;
font-weight: bold;
padding-left: 40px;
font-size: 20px;
}
span a {
display: block;
margin-top: 10px;
border-top: 1px solid #96d1f8;
background: #65a9d7;
background: -webkit-gradient(linear, left top, left bottom, from(#3e779d), to(#65a9d7));
background: -webkit-linear-gradient(top, #3e779d, #65a9d7);
background: -moz-linear-gradient(top, #3e779d, #65a9d7);
background: -ms-linear-gradient(top, #3e779d, #65a9d7);
background: -o-linear-gradient(top, #3e779d, #65a9d7);
padding: 10px 20px 5px 20px;
-webkit-border-radius: 11px;
-moz-border-radius: 11px;
border-radius: 11px;
-webkit-box-shadow: rgba(0, 0, 0, 1) 0 1px 0;
-moz-box-shadow: rgba(0, 0, 0, 1) 0 1px 0;
box-shadow: rgba(0, 0, 0, 1) 0 1px 0;
text-shadow: rgba(0, 0, 0, .4) 0 1px 0;
color: white;
font-size: 17px;
text-decoration: none;
vertical-align: middle;
width: 90px;
text-align: center;
}
span a: hover {
border-top-color: #28597a;
background: #28597a;
color: #ccc;
}
span a: active {
border-top-color: #1b435e;
background: #1b435e;
}
ul li {
list-style-type: none;
}
ul li a {
display: block;
color: #222222;
font-weight: bold;
font-size: 22px;
}
<div class="note-area" ng-show="!editMode">
<ul>
<li ng-repeat="note in notes|orderBy:'id'">
<a href="#" ng-click="openEditor(note.id)">{{note.title}}</a>
</li>
</ul>
</div>
<div id="editor" ng-show="editMode" class="note-area" contenteditable="true" ng-bind="noteText"></div>
<span><a href="#" ng-click="save()" ng-show="editMode">Back</a></span>
<span><a href="#" ng-click="openEditor()" ng-show="!editMode">Add Note</a></span>