On Tue, Feb 14, 2012 at 11:43 AM, Thomas Tanghus <[email protected]> wrote:

> > Exactly!
> > I have redesigned the code to use action registration conception like the
> > file app does. Now I'm cutting down my UI as the first approach turned
> out
> > to be excessive a bit :)
>
> lol - I won't dare to think what that means ;-)
>
> > As soon as it looks like the files section I'll share it.
>
> Looking forward to seeing it.
>
>
I mean there were added too many useless controls. I rewrite it almost
completely to meet the  KISS ideology  :)
The patch is attached.
diff --git a/apps/contacts/ajax/deletecards.php b/apps/contacts/ajax/deletecards.php
new file mode 100644
index 0000000..8e182b1
--- /dev/null
+++ b/apps/contacts/ajax/deletecards.php
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Copyright (c) 2012 Victor Dubiniuk <[email protected]>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+
+require_once('../../../lib/base.php');
+
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+
+$ids = $_POST['ids'];
+foreach ($ids as $id){
+	$card = OC_Contacts_App::getContactObject( intval($id) );
+	OC_Contacts_VCard::delete( intval($id) );
+}
+
+OC_JSON::success(array('data' => array()));
diff --git a/apps/contacts/ajax/movecards.php b/apps/contacts/ajax/movecards.php
new file mode 100644
index 0000000..abb1dab
--- /dev/null
+++ b/apps/contacts/ajax/movecards.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Copyright (c) 2012 Victor Dubiniuk <[email protected]>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+
+require_once('../../../lib/base.php');
+
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+
+$ids = $_POST['ids'];
+$aid = intval($_POST['target']);
+OC_Contacts_App::getAddressbook($aid);
+
+foreach ($ids as $id){
+	$card = OC_Contacts_App::getContactObject( intval($id) );
+}
+OC_Contacts_VCard::moveToAddressBook($aid, $ids);
+
+OC_JSON::success(array('data' => array()));
diff --git a/apps/contacts/ajax/movetoaddressbook.php b/apps/contacts/ajax/movetoaddressbook.php
new file mode 100644
index 0000000..5c278f2
--- /dev/null
+++ b/apps/contacts/ajax/movetoaddressbook.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Copyright (c) 2012 Victor Dubiniuk <[email protected]>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+require_once('../../../lib/base.php');
+$l10n = new OC_L10N('contacts');
+OC_JSON::checkLoggedIn();
+OC_JSON::checkAppEnabled('contacts');
+
+$output = new OC_TEMPLATE("contacts", "movetoaddressbook");
+$output -> printpage();
+
diff --git a/apps/contacts/css/contacts.css b/apps/contacts/css/contacts.css
index 384541f..9552c64 100644
--- a/apps/contacts/css/contacts.css
+++ b/apps/contacts/css/contacts.css
@@ -3,6 +3,9 @@
 }*/
 
 #contacts { padding-left:2px; padding-top: 5px; background: #fff; }
+#contacts li { position:relative; }
+#contacts input[type="checkbox"]{ display:none;position:absolute;top:9px;margin-left:0.2em; }
+#contacts .action{ display:none;float:right;position:absolute;top:7px }
 #leftcontent a { height: 23px; display: block; margin: 0 0 0 0; padding: 0 0 0 25px; }
 #chooseaddressbook {margin-right: 170px; float: right;}
 #contacts_deletecard {position:absolute;top:15px;right:25px;}
diff --git a/apps/contacts/index.php b/apps/contacts/index.php
index c5115d1..2b77db3 100644
--- a/apps/contacts/index.php
+++ b/apps/contacts/index.php
@@ -47,6 +47,7 @@ OC_Util::addScript('contacts','jquery.combobox');
 OC_Util::addScript('contacts','jquery.inview');
 OC_Util::addScript('contacts','jquery.Jcrop');
 OC_Util::addStyle('','jquery.multiselect');
+OC_Util::addScript('contacts','checkrange');
 //OC_Util::addStyle('contacts','styles');
 OC_Util::addStyle('contacts','jquery.combobox');
 OC_Util::addStyle('contacts','jquery.Jcrop');
diff --git a/apps/contacts/js/checkrange.js b/apps/contacts/js/checkrange.js
new file mode 100644
index 0000000..0e7b625
--- /dev/null
+++ b/apps/contacts/js/checkrange.js
@@ -0,0 +1,60 @@
+/**
+ * Copyright (c) 2012 Victor Dubiniuk <[email protected]>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ */
+
+(function( $ ){ 
+	$.checkRange = {
+		init: function(selector){
+			//TODO: lazy init when new item is added
+			this.active = false;
+			$(selector).before('<input type="checkbox" />');
+			this._coll = $(selector).siblings('input:checkbox');
+			this._coll.css({opacity: 0, display: 'block'})
+				.mouseenter( function(){ $(this).css({zIndex: 1}).animate({opacity: 1}, 300); } )
+				.mouseleave( function(){ this.checked || $(this).css({zIndex: 0}).animate({opacity: 0}, 300); })
+				.click( $.proxy(this._click, this) );
+			this.lastClicked = null;
+			this.on();
+		},
+		on: function(){
+			this.active = true;
+		},
+		off: function(){
+			this.active = false;
+		},
+		getChecked: function(){
+			if (!this.active){
+				return [];
+			}
+			
+			var selected = [];
+			selected = this._coll.filter(':checked').parent('li').map( function()
+				{ return $(this).attr("data-id"); }
+			);
+			return selected.toArray();
+		},
+		hasChecked: function(){
+			return !!this.getChecked().length;
+		},
+		_click: function(e){
+			if (!this.active){
+				return;
+			}
+			 
+			e.stopPropagation();
+			if (e.shiftKey){
+				var start = this._coll.index(e.target);
+				var end = this._coll.index(this.lastClicked);
+				var state = this.lastClicked.checked;
+
+				$(this._coll.toArray().slice(Math.min(start,end), Math.max(start,end)+ 1)).attr('checked', state);
+				state ? this._coll.trigger('mouseenter') : this._coll.trigger('mouseleave');
+			}
+			
+			this.lastClicked = e.target;
+		}
+	}
+})(jQuery)
+ 
\ No newline at end of file
diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js
index 3079362..4afcadf 100644
--- a/apps/contacts/js/contacts.js
+++ b/apps/contacts/js/contacts.js
@@ -306,7 +306,7 @@ Contacts={
 								if(!added) {
 									$('#leftcontent ul').append(item);
 								}
-								
+								Contacts.UI.Actions.attach();
 							}
 							else{
 								Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
@@ -1110,6 +1110,7 @@ Contacts={
 					if(jsondata.status == 'success'){
 						$('#contacts').html(jsondata.data.page);
 						Contacts.UI.Card.update();
+						Contacts.UI.Actions.attach();
 					}
 					else{
 						Contacts.UI.messageBox(t('contacts', 'Error'),jsondata.data.message);
@@ -1128,11 +1129,130 @@ Contacts={
 					}
 				});
 			}
+		},
+		Actions : {
+			actions:{},
+			icons:{},
+			currentContact:null,
+			attach: function(){
+				$.checkRange.init('#contacts li a');
+				$('#contacts li').mouseenter( $.proxy(this.display, this) );
+				$('#contacts li').mouseleave( $.proxy(this.hide, this) );
+			},
+			register: function(name, icon, action){
+				this.actions[name] = action;
+				this.icons[name] = icon;
+			},
+			display: function(event){
+				this.currentContact = $(event.target);
+				var contact = this.getCurrentId();
+				
+				var offset = 3;
+				for (var action in this.actions){
+					var img = this.icons[action];
+					if (img.call){
+						img = img(contact);
+					}
+					
+					var node = $('<img class="action" style="right:' + offset + 'px" src="' + img + '"/>');
+					node.data('action', action);
+					node.click( $.proxy(this.doAction, this) );
+					node.hide();
+					$(this.currentContact).append(node);
+					offset = offset + 3 + node.width();
+				}
+				$('#contacts .action').css('-o-transition-property','none');
+				$('#contacts .action').fadeIn(200,function(){
+					$('#contacts .action').css('-o-transition-property','opacity');
+				});
+				return false;
+			},
+			hide: function(){
+				$('#contacts .action').fadeOut(200,function(){
+					$(this).remove();
+				});
+			},
+			doAction: function(event){
+				event.stopPropagation();
+				event.preventDefault();
+				var element = $(event.target);
+				this.currentContact = $(event.target).parents('li');
+				var action = this.actions[$(element).data('action')];
+				this.hide();
+				
+				var data; 
+				if ($.checkRange.hasChecked()){
+					data = $.checkRange.getChecked();
+				} else {
+					data = [this.getCurrentId()];
+				}
+				action(data);
+			},
+			getCurrentId: function(){
+				return $(this.currentContact).attr('data-id');
+			}
 		}
 	}
 }
 $(document).ready(function(){
-
+	var actionSet = {
+		delete: function(set){
+			$.post(OC.filePath('contacts', 'ajax', 'deletecards.php'), { ids : set }, 
+				function(data){
+					if (data.status == 'success'){
+						Contacts.UI.Contacts.update();
+					} else {
+						
+					}
+			});
+		},
+		initMove: function(set){
+			this.set = set;
+			var instance = this;
+			$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'movetoaddressbook.php'), function(){
+				$('#movetoaddressbook_dialog').dialog({
+					modal: true,
+					width : 600,
+					open: function(){
+						$('#movetoaddressbook_dialog a').click( $.proxy(instance.move, instance) );
+					},
+					close : function(event, ui) {
+						$(this).dialog('destroy').remove();
+					}
+				});
+				
+			});
+		},
+		move: function(e){
+			var targetBook = $(e.target).attr('bookid');
+			if (!targetBook){
+				return;
+			}
+			$.post(OC.filePath('contacts', 'ajax', 'movecards.php'), { ids : this.set, target : targetBook  }, 
+				function(data){
+					if (data.status == 'success'){
+						Contacts.UI.Contacts.update();
+						$('#movetoaddressbook_dialog').dialog('destroy').remove();
+					} else {
+						
+					}
+				}
+			);
+		}
+	}
+	Contacts.UI.Actions.register(
+		'Delete',
+		function(){return OC.imagePath('core','actions/delete')},
+		function(contactIds){ actionSet.delete(contactIds); }
+	);
+	Contacts.UI.Actions.register(
+		'ChangeAddressbook',
+		function(){return OC.imagePath('core','actions/play-next')},
+		function(contactIds){ actionSet.initMove(contactIds); }
+	);
+	Contacts.UI.Actions.attach();
+	
+	
 	Contacts.UI.loadHandlers();
 
 	/**
diff --git a/apps/contacts/templates/movetoaddressbook.php b/apps/contacts/templates/movetoaddressbook.php
new file mode 100644
index 0000000..eefc5c6
--- /dev/null
+++ b/apps/contacts/templates/movetoaddressbook.php
@@ -0,0 +1,18 @@
+<div id="movetoaddressbook_dialog" title="<?php echo $l->t("Choose Destination Addressbook"); ?>">
+<table width="100%" style="border: 0;">
+<?php
+$option_addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
+for($i = 0; $i < count($option_addressbooks); $i++){
+	echo "<tr>";
+	echo '<td><a href="#" onclick="return false;" bookid="'. $option_addressbooks[$i]["id"] .'" >'.$option_addressbooks[$i]["displayname"].'</a></td>';
+	echo "</tr>";
+}
+?>
+<?php /* ?>
+<tr>
+	<td style="padding: 0.5em;">
+		<a class="button" href="#" onclick="Contacts.UI.Addressbooks.newAddressbook(this);"><?php echo $l->t('New Address Book') ?></a>
+	</td>
+</tr>
+<?php */ ?>
+</table>
_______________________________________________
Owncloud mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/owncloud

Reply via email to