Commits

watermelon committed 6a723c7

add bookmarks implemented, save file issue resolved(my fault)

Comments (0)

Files changed (4)

src/main/client/dao/BookmarkDao.js

 	
 	//the in-memory bookmark store
 	var bookmarks = new Array();
+	var max_id = -1;
 	
 	//fetch all bookmarks
 	fetchBookmarks();
 	 *
 	 * Adds new bookmark to the store.
 	 *
-	 * **bm** the bookmark to be added
-	 * 
+	 * **bm** the bookmark to be added {
+	 * 		title: String,
+	 * 		url: String,
+	 * 		tags: Array
+	 * 		metas: Array
+	 * }<br/>
 	 * returns Boolean true on success, else false.
 	 **/
 	this.addBookmark = function(bm) {
 		
-		return bookmarks.push(bm);
+		max_id++;
+		
+		var new_bm = new bookmaak.domain.Bookmark(max_id, bm.title, bm.url, bm.tags, bm.metas);
+		
+		return bookmarks.push(new_bm);
 	};
 	
 	
 											link.id, link.title, link.url, 
 											link.tags, link.metas));
 						}
+						
+						max_id = links.length - 1;
 					}
 					else {
 						console.log("bookmaak.dao.BookmarkDao.fetchBookmarks: You have no bookmarks yet!");

src/main/client/ui/manage_bookmarks_presenter.js

  **/
 bookmaak.ui.onClickAddBookmark = function() {
 	
-//	showManageBookmarksViewFadeAddBookmark();//TODO add the new bookmark
+	//get fields
+	var _title = jQuery("#bookmark_title").val();
+	var _url = jQuery("#bookmark_url").val();
+	var _tags = jQuery("#bookmark_tags").val();
+	var _metas = jQuery("#bookmark_metas").val();
+	
+	
+	var tags_are_valid = false;
+	
+	//validate fields i think we only need to validate the tag list
+	if(_tags.match(/^(?:\w[ ,]?)*$/))
+		tags_are_valid = true;
+	
+	//TODO make array of tags
+	var _tags_array = new Array();
+	//TODO make array of metas
+	var _metas_array = new Array();
+	
+	//call controller to add the bookmarks...TODO do we need a facade? the controller? yes to control transaction granularity
+	bookmaak.controller.Controller.bookmark_dao.addBookmark({
+		title: _title,
+		url: _url,
+		tags: _tags_array,
+		metas: _metas_array
+	});
+	
+	bookmaak.controller.Controller.bookmark_dao.commit();
 };
 
 
-
+/**
+ * bookmaak.ui.onClickSelectAll()
+ * 
+ * Selects all bookmarks
+ **/
+bookmaak.ui.onClickSelectAll = function() {
+	
+	if(jQuery("#select_all").is(":checked"))
+		jQuery(".selectlink").attr('checked', true);
+	else
+		jQuery(".selectlink").attr('checked', false);
+};
 
 
 

src/main/client/ui/manage_bookmarks_view.jade

         
     div#search_result.hidden
     
-      input#select_all(type='button', value='select all',
+      label(for='select_all')
+        | select all
+      input#select_all(type='checkbox',
         onClick='bookmaak.ui.onClickSelectAll()')
         
       ul#bookmarks
   div#add_bookmark.hidden
   
     label(for='bookmark_title')
-      | title:
+      | title(until we fetch it):
     br
     input#bookmark_title(type='text')
     br

src/main/server/srv.js

 	//write to file
 	req.on('end', function() {
 		
+		fs.writeFile(BASE_DIR + rel_path, body, function (err) {
+			
+			if (err) throw err;
+			
+			res.writeHead(201,default_header({'Content-Type': 'text/plain'}));
+			res.end();
+			
+			console.log('It\'s saved!');
+		});
+		
 		//check if file exists
-		fs.stat(BASE_DIR + rel_path, function(err, stats) {
-			
-			if(err 
-			&& err.errno == 34 
-			&& err.code == 'ENOENT') {//user does not exist...
-				
-				//we overwrite it
-				fs.open(BASE_DIR + rel_path, 'w', 770, function( e, fd ) {
-					
-					fs.write( fd, body, 0, body.length, 0, function(){
-						
-						fs.close(fd, function() {
-							console.log('file closed');
-							res.writeHead(201,default_header({'Content-Type': 'text/plain'}));
-							res.end();
-							
-							console.log('It\'s saved!');
-						});
-					});
-				});
-			}
-			else if(err) {//an other error
-				
-				console.log("Unexpected error!");
-				console.log(err);
-				throw err;
-			}
-			else {//bookmark file exists
-				console.log("stats.isFile(): "+stats.isFile());
-				console.log("stats.isDirectory(): "+stats.isDirectory());
-				
-				if(stats.isFile()) {
-					
-					//we overwrite it 'a' would append...
-					fs.open(BASE_DIR + rel_path, 'w', 666, function( e, id ) {
-						
-						fs.write( id, body, null, 'utf8', function(){
-							
-							fs.close(id, function() {
-								console.log('file closed');
-								res.writeHead(201,default_header({'Content-Type': 'text/plain'}));
-								res.end();
-								
-								console.log('It\'s saved!');
-							});
-						});
-					});
-				}
-				else {
-					throw "bookmarks is not a file";
-				}
-			}
-		});
+//		fs.stat(BASE_DIR + rel_path, function(err, stats) {
+//			
+//			if(err 
+//			&& err.errno == 34 
+//			&& err.code == 'ENOENT') {//user does not exist...
+//				
+//				//we overwrite it
+//				fs.open(BASE_DIR + rel_path, 'w', 770, function( e, fd ) {
+//					
+//					fs.write( fd, body, 0, body.length, 0, function(){
+//						
+//						fs.close(fd, function() {
+//							console.log('file closed');
+//							res.writeHead(201,default_header({'Content-Type': 'text/plain'}));
+//							res.end();
+//							
+//							console.log('It\'s saved!');
+//						});
+//					});
+//				});
+//			}
+//			else if(err) {//an other error
+//				
+//				console.log("Unexpected error!");
+//				console.log(err);
+//				throw err;
+//			}
+//			else {//bookmark file exists
+//				console.log("stats.isFile(): "+stats.isFile());
+//				console.log("stats.isDirectory(): "+stats.isDirectory());
+//				
+//				if(stats.isFile()) {
+//					
+//					fs.writeFile(BASE_DIR + rel_path, body, function (err) {
+//						
+//						if (err) throw err;
+//						
+//						res.writeHead(201,default_header({'Content-Type': 'text/plain'}));
+//						res.end();
+//						
+//						console.log('It\'s saved!');
+//					});
+//					
+////					//we overwrite it, 'a' would append...
+////					fs.open(BASE_DIR + rel_path, 'w', 666, function( e, fd ) {
+////						
+////						fs.write( fd, body, 0, body.length, 0, function(){
+////							
+////							fs.close(fd, function() {
+////								console.log('file closed');
+////								res.writeHead(201,default_header({'Content-Type': 'text/plain'}));
+////								res.end();
+////								
+////								console.log('It\'s saved!');
+////							});
+////						});
+////					});
+//				}
+//				else {
+//					throw "bookmarks is not a file";
+//				}
+//			}
+//		});
 	});
 }