Commits

watermelon committed 717614d

adding bookmarks works, but there is still a bug in deleting

Comments (0)

Files changed (4)

src/main/client/controller/Controller.js

  **/
 bookmaak.controller.Controller.bookmark_dao = null;
 
+/**
+ * bookmaak.controller.Controller.last_search_result -> Array
+ * 
+ * the latest search result.
+ **/
+bookmaak.controller.Controller.last_search_result = new Array();
+
 
 
 /**
 				
 				bookmaak.controller.Controller.login_name = login;
 				
-				//TODO for testing we send some first...
-//				bookmaak.controller.Controller.bookmark_dao.
-//				addBookmark(new bookmaak.domain.Bookmark(
-//						1,
-//						'Wikipedia - Universal Software Radio Peripheral',
-//						'http://en.wikipedia.org/wiki/Universal_Software_Radio_Peripheral',
-//						['hardware','gnu','radio','usrp','universal','software','radio','periferal'],
-//						['radio', 'gnu', 'wiki', 'wikipedia']));
-//
-//				bookmaak.controller.Controller.bookmark_dao.
-//				addBookmark(new bookmaak.domain.Bookmark(
-//						2,
-//						'Wikipedia - Universal Software Radio Peripheral',
-//						'http://en.wikipedia.org/wiki/Universal_Software_Radio_Peripheral',
-//						['hardware','gnu','radio','usrp','universal','software','radio','periferal'],
-//						['radio', 'gnu', 'wiki', 'wikipedia']));
-				
-//				bookmaak.controller.Controller.bookmark_dao.commit();
-
-				
 				//TODO on server error disconnect... in some clever way....
 			}
 			
 			
-			
 		}
 		else {
 			console.error("bookmaak.controller.Controller.login: Failed to retrieved user file!");
 bookmaak.controller.Controller.logout = function() {
 	
 	console.debug("bookmaak.controller.Controller.logout: logging out user...");
+	
 	bookmaak.controller.Controller.login_name = "";
 	bookmaak.controller.Controller.communicator = null;
 };
 
 
+
 /**
- * bookmaak.controller.Controller.get_bookmarks()
- *
- * Returns an array of bookmarks
+ * bookmaak.dao.cotroller.Controller.deleteBookmark(Bookmark)
+ * 
+ * **bookmark** the bookmark to delete
  **/
-bookmaak.controller.Controller.get_bookmarks = function() {
+bookmaak.controller.Controller.deleteBookmark = function(bookmark) {
 	
-	return bookmaak.controller.Controller.bookmark_dao.getAllBookmark() || new Array();
+	console.debug("bookmaak.dao.cotroller.Controller.deleteBookmark: deleting...");
+	console.debug(bookmark);
+	
+	bookmaak.controller.Controller.bookmark_dao.deleteBookmark(bookmark);
+	
+	bookmaak.controller.Controller.bookmark_dao.commit();
+	
+	//TODO research with the last options
+	bookmaak.controller.Controller.
+	last_search_result = bookmaak.controller.Controller.bookmark_dao.
+						getAllBookmark();
 };
 
+/**
+ * bookmaak.controller.Controller.searchBookmark(Object)
+ * 
+ * **options** the search options
+ **/
+bookmaak.controller.Controller.searchBookmark = function(options) {
+	
+	console.debug("bookmaak.controller.Controller.searchBookmark: getting all bookmarks...");
+	
+	//TODO get options
+	//perform search...
+	//TODO research with the last options
+	bookmaak.controller.Controller.
+	last_search_result = bookmaak.controller.Controller.bookmark_dao.
+						getAllBookmark();
+};
 
 
-
-

src/main/client/dao/BookmarkDao.js

 	
 	
 	/**
-	 * bookmaak.dao.BookmarkDao#addBookmark(Bookmark) -> Boolean
+	 * bookmaak.dao.BookmarkDao#addBookmark(Object) -> Boolean
 	 *
 	 * Adds new bookmark to the store.
 	 *
-	 * **bm** the bookmark to be added {
+	 * **bm_data** the bookmark to be added {
 	 * 		title: String,
 	 * 		url: String,
 	 * 		tags: Array
 	 * }<br/>
 	 * returns Boolean true on success, else false.
 	 **/
-	this.addBookmark = function(bm) {
+	this.addBookmark = function(bm_data) {
 		
-		max_id++;
+		//check if it does not already exist
+		for(var i = 0; i < bookmarks.length; i++) {
+			
+			if(bookmarks[i].getUrl() === bm_data.url)
+				return false;
+		}
 		
-		var new_bm = new bookmaak.domain.Bookmark(max_id, bm.title, bm.url, bm.tags, bm.metas);
+		var new_bm = new bookmaak.domain.Bookmark(
+				max_id++, 
+				bm_data.title, 
+				bm_data.url, 
+				bm_data.tags, 
+				bm_data.metas);
 		
 		return bookmarks.push(new_bm);
 	};
 	 *
 	 * Changes the bookmark bookmark and stores it.
 	 *
-	 * **bm** the bookmark to be updated
-	 *
+	 * **bm** the bookmark to be updated<br/>
 	 * returns Boolean true on success, else false.
 	 **/
 	this.updateBookmark = function(bm) {
-		//TODO implement update
-		return true;
+		
+		for(var i = 0; i < bookmarks.length; i++) {
+			
+			if(bookmarks[i].getId() === bm.getId()) {
+				
+				//replace the old bm with the new one
+				bookmarks.splice(i, 1, bm);
+				return true;
+			}
+		}
+		
+		return false;
 	};
 	
 	
 	/**
 	 * bookmaak.dao.BookmarkDao#deleteBookmark(Bookmark) -> Boolean
 	 *
-	 * Deletes the bookmark.
+	 * Deletes the bookmark specified **bookmark**.
 	 *
-	 * **bm** the bookmark to be deleted
-	 *
+	 * **bookmark** the bookmark<br/>
 	 * returns Boolean true on success, else false.
 	 **/
 	this.deleteBookmark = function(bm) {
-		//TODO implement delete
-		return true;
+		
+		for(var i = 0; i < bookmarks.length; i++) {
+			
+			if(bookmarks[i].getId() === bm.getId()) {
+				
+				bookmarks.splice(i, 1);
+				return true;
+			}
+		}
+		
+		return false;
 	};
 	
 	
 	
 	
 	/**
+	 * bookmaak.dao.BookmarkDao#getBookmarks_by_title(Number) -> Bookmark
+	 *
+	 * Gets the bookmark with **id**.
+	 *
+	 * **id** the id
+	 *
+	 * returns a bookmark or null
+	 **/
+	this.getBookmarks_by_id = function(id) {
+		
+		for(var i = 0; i < bookmarks.length; i++) {
+			
+			if(bookmarks[i].getId() === id)
+				return bookmarks[i];
+		}
+		
+		return null;
+	};
+	
+	
+	/**
 	 * bookmaak.dao.BookmarkDao#getBookmarks_by_title(String) -> Array
 	 *
-	 * Gets all bookmarks with the specified title.
+	 * Gets all bookmarks with the specified title.<br/>
+	 * (Matching .*title.*)
 	 *
-	 * **title** the title
-	 *
+	 * **title** the title<br/>
 	 * returns an array of bookmarks
 	 **/
 	this.getBookmarks_by_title = function(title) {
-		//TODO implement find by title
-		return new Array();
+		
+		var result = new Array();
+		
+		for(var i = 0; i < bookmarks.length; i++) {
+			
+			if(bookmarks[i].getTitle().match(new RegExp(".*"+title+".*")))
+				result.push(bookmarks[i]);
+		}
+		
+		return result;
 	};
 	
 	
 	/**
 	 * bookmaak.dao.BookmarkDao#getBookmarks_by_tag(String) -> Array
 	 *
-	 * Gets all bookmarks with the specified tag.
+	 * Gets all bookmarks with the specified tag.<br/>
+	 * (Matching .*tag.*)
 	 *
-	 * **tag** the tag
-	 *
-	 * returns an array of bookmarks
+	 * **tag** the tag<br/>
+	 *returns an array of bookmarks
 	 **/
 	this.getBookmarks_by_tag = function(tag) {
-		//TODO implement find by tags
-		return new Array();
+		
+		var result = new Array();
+		
+//		for(var i = 0; i < bookmarks.length; i++) {
+//			
+//			if(bookmarks[i].getTags().match(new RegExp(".*"+tag+".*")))
+//				result.push(bookmarks[i]);
+//		}
+		return result;
 	};
 	
 	

src/main/client/ui/index.jade

   body(onClick='bookmaak.controller.Controller.rng.rng_seed_time();',
     onKeyPress='bookmaak.controller.Controller.rng.rng_seed_time();')
     
-  div#components
+  div#components.hidden
   
-    div#messages
-      div.successbox
-      div.noticebox
-      div.warningbox
-      div.errorbox
+    div#component_messages
+      div#messages
+        div.successbox
+        div.noticebox
+        div.warningbox
+        div.errorbox
+        
+      div#success_message
+        div.successbox
+      div#notice_message
+        div.noticebox
+      div#warning_message
+        div.warningbox
+      div#error_message
+        div.errorbox
       
       
+    div#component_bookmark_buttons
+    
+      span#bookmark_buttons_.bookmark_buttons
+        input#edit_btn_.edit_btn(value='edit', type='button')
+        input#delete_btn_.delete_btn(value='delete', type='button')
+        
+        
     div#loading.hidden
       img(src="http://upload.wikimedia.org/wikipedia/commons/d/de/Ajax-loader.gif")
       
+      
     input#backbutton(value='back', type='button',
       onClick='bookmaak.ui.onClickBack()')
       
+      
   div#views
     
     include connect_view

src/main/client/ui/manage_bookmarks_presenter.js

 	//TODO get all option values
 	bookmaak.ui.printNoticeMessage("(debug) searching...");
 	
-	jQuery("#bookmarks").empty();
+	bookmaak.controller.Controller.searchBookmark(null);
 	
-	var all_bookmarks = bookmaak.controller.Controller.get_bookmarks();
+	bookmaak.ui.refreshSearchResult();
+};
+
+/**
+ * createBookmark_buttonsFor(Number) -> String
+ * 
+ * Returns an instance of the bookmark_buttons component
+ * **bookmark_id** the bookmark id
+ * returns the bookmark buttons
+ **/
+function createBookmark_buttonsFor(bookmark_id) {
 	
-	for(var i = 0; i < all_bookmarks.length; i++) {
+	//get an instance of the component
+	var bookmark_buttons_jQ = jQuery("#component_bookmark_buttons :first-child").clone();
+	var bookmark_buttons = bookmark_buttons_jQ[0];
+	var the_buttons = bookmark_buttons_jQ.children();
+	
+	console.debug("v link buttons for id "+bookmark_id);
+	console.debug(bookmark_buttons);
+	console.debug(the_buttons);
+	
+	//prepare the instance
+	//adapt component id
+	bookmark_buttons.id += bookmark_id;
+	//adapt all button ids
+	for(var i = 0; i < the_buttons.length; i++) {
 		
-		var bookmark = all_bookmarks[i];
+		the_buttons[i].id += bookmark_id;
+		var onClickFunction = null;
+		switch(the_buttons[i].className) {
+			case 'edit_btn':
+				onClickFunction = "bookmaak.ui.onClickEditBookmark("+bookmark_id+")";
+				break;
+			case 'delete_btn':
+				onClickFunction = "bookmaak.ui.onClickDeleteBookmark("+bookmark_id+")";
+				break;
+			default:
+				throw new SyntaxError("manage_bookmarks_presenter_createBookmark_buttonsFor(): unhandled class "+the_buttons[i].className);
+		}
 		
-		jQuery("#bookmarks").append(
-			'<li id="'+bookmark.getId()+'" class="bookmark">' +
-				'<input class="selectlink" type="checkbox" />' +
-				'<a class="link" href="'+bookmark.getUrl()+'">'+bookmark.getTitle()+'</a>' +
-				'<br/>' +
-				'<strong>tags:</strong><span class="tags">'+bookmark.getTags().toString()+'</span>' +
-				'<br/>' +
-				'<strong>meta tags:</strong><span class="metas">'+bookmark.getMetas().toString()+'</span>' +
-			"</li>");
+		the_buttons[i].setAttribute("onClick", onClickFunction);
+		
+		//thats a better way, but it did not work
+//		the_buttons[i].id += bookmark_id;
+//		var onClickFunction = null;
+//		switch(the_buttons[i].className) {
+//			case 'edit_btn':
+//				onClickFunction = new Function("bookmaak.ui.onClickEditBookmark(bookmark_id)");
+//				break;
+//			case 'delete_btn':
+//				onClickFunction = new Function("bookmaak.ui.onClickDeleteBookmark(bookmark_id)");
+//				break;
+//			default:
+//				throw new SyntaxError("manage_bookmarks_presenter_createBookmark_buttonsFor(): unhandled class "+the_buttons[i].className);
+//		}
+//		
+//		the_buttons[i].onclick = onClickFunction;
 	}
 	
-	jQuery("#search_result").show();
-};
+	//make a string of it
+	return bookmark_buttons.outerHTML;
+}
+
 
 
 /**
 	});
 	
 	bookmaak.controller.Controller.bookmark_dao.commit();
+	
+	//reset fields
+	jQuery("#bookmark_title").val("");
+	jQuery("#bookmark_url").val("");
+	jQuery("#bookmark_tags").val("");
+	jQuery("#bookmark_metas").val("");
+	
+	bookmaak.ui.refreshSearchResult();
 };
 
 
 };
 
 
+/**
+ * bookmaak.ui.onClickDeleteBookmark(Number)
+ * 
+ * Removes the bookmark specified by **index**.
+ * 
+ * **index** the index
+ **/
+bookmaak.ui.onClickDeleteBookmark = function(index) {
+	
+	console.log("onClickDeleteBookmark(index = "+index+")");
+	console.debug(bookmaak.controller.Controller.last_search_result);
+	//get the bookmark thats meant to be deleted
+	var bookmark_to_delete = bookmaak.controller.Controller.last_search_result[index];
+	
+	//call the controller to delete it
+	bookmaak.controller.Controller.deleteBookmark(bookmark_to_delete);
+	
+	bookmaak.ui.refreshSearchResult();
+};
+
+
+
+
+/**
+ * bookmaak.ui.onClickEditBookmark(Number)
+ * 
+ * Shows a view to edit the bookmark specified by **index**.
+ * 
+ * **index** the index of the last_search_result
+ **/
+bookmaak.ui.onClickEditBookmark = function(index) {
+	
+	console.log("onClickEditBookmark(index = "+index+")");
+	
+	//get the bookmark to be edited
+	var bookmark_to_edit = bookmaak.controller.Controller.last_search_result[index];
+
+	//TODO show component... perhaps componentize the new bookmark controls
+	//TODO show edit view
+};
 
 /*
  * Shows the manage_bookmarks_view
 //	
 //	jQuery("#manage_bookmarks_view").fadeIn();
 //}
+/**
+ * bookmaak.ui.refreshSearchResult()
+ * 
+ * Refreshes the current search result(using the current options and phrase)
+ **/
+bookmaak.ui.refreshSearchResult = function() {
+	
+	jQuery("#bookmarks").empty();
+	
+	var result = bookmaak.controller.Controller.last_search_result;
+	
+	for(var i = 0; i < result.length; i++) {
+		
+		var bookmark = result[i];
+		
+		jQuery("#bookmarks").append(
+			'<li id="'+i+'" class="bookmark">' +
+				'<input class="selectlink" type="checkbox" />' +
+				'<a class="link" href="'+bookmark.getUrl()+'">'+bookmark.getTitle()+'</a>' +
+				createBookmark_buttonsFor(i) +
+				'<br/>' +
+				'<strong>tags:</strong><span class="tags">'+bookmark.getTags().toString()+'</span>' +
+				'<br/>' +
+				'<strong>meta tags:</strong><span class="metas">'+bookmark.getMetas().toString()+'</span>' +
+			"</li>");
+	}
+	
+	jQuery("#search_result").show();
+};
 
 
+