Browse Source

comment work

Nathaniel van Diepen 11 years ago
parent
commit
1b03b42896
4 changed files with 73 additions and 35 deletions
  1. 6 0
      api.php
  2. 9 15
      index.php
  3. 32 17
      js/index.js
  4. 26 3
      php/functions.php

+ 6 - 0
api.php

@@ -177,6 +177,12 @@
 								}
 								retj($ret,$id);
 							break;
+							case 'comment':
+								$ret = Array(
+									'state'=>stateObj($_GET['comment_type'],$_GET['comment_id'])
+								);
+								retj($ret,$ret['state']['title']);
+							break;
 							default:
 								retj(Array(
 									'error'=>'Invalid action.'

+ 9 - 15
index.php

@@ -22,19 +22,13 @@
 		}
 		switch($get){
 			case 'state':
-				$json = Array();
-				$json['state'] = Array();
-				$json['state']['data'] = $_GET;
-				switch($type){
-					case 'user':$url='~'.$id;break;
-					case 'group':$url='+'.$id;break;
-					case 'issue':$url='!'.$id;break;
-					case 'page':$url='page-'.$id;break;
-					default:$url=$type.'-'.$id;
-				}
-				$json['state']['url'] = $url;
-				$json['state']['title'] = ucwords($type.' - '.$id);
-				die(json_encode($json));
+				die(
+					json_encode(
+						Array(
+							'state'=>stateObj($type,$id)
+						)
+					)
+				);
 			break;
 			case 'api':
 				require_once('api.php');
@@ -125,8 +119,8 @@
 		<div id="dialog"></div>
 		<div id="comment">
 			<form>
-				<input type="hidden" name="type"/>
-				<input type="hidden" name="id"/>
+				<input type="hidden" name="comment_type"/>
+				<input type="hidden" name="comment_id"/>
 				<textarea name="message"></textarea>
 			</form>
 		</div>

+ 32 - 17
js/index.js

@@ -101,6 +101,7 @@
 					if(exists(d['error'])){
 						error(d);
 					}else{
+						console.log(d);
 						d.state.title = d.state.title.capitalize();
 						if(location.href.substr(location.href.lastIndexOf('/')+1) != d.state.url){
 							console.log('Forced redirection to '+d.state.url);
@@ -350,13 +351,15 @@
 					});
 				},
 				dialog: function(id,type,title){
-					$('#comment').find('form').find('input[name=type]').val(type);
-					$('#comment').find('form').find('input[name=id]').val(id);
+					loading(true);
+					flag('ignore_statechange',true);
+					$('#comment').find('form').find('input[name=comment_type]').val(type);
+					$('#comment').find('form').find('input[name=comment_id]').val(id);
 					$('#comment').find('form').find('textarea[name=message]').val('');
 					$('#comment').dialog({
 						close: function(){
-							$('#comment').find('form').find('input[name=type]').val('');
-							$('#comment').find('form').find('input[name=id]').val('');
+							$('#comment').find('form').find('input[name=comment_type]').val('');
+							$('#comment').find('form').find('input[name=comment_id]').val('');
 						},
 						resizable: false,
 						draggable: false,
@@ -366,19 +369,29 @@
 								text: 'Ok',
 								class: 'recommend-force',
 								click: function(){
-									var context = $(this).find('form').serializeObject();
-									if(context.message!=''){
-										console.log(context);
-										// TODO - Handle sending to server and then refresh page
-										$(this).dialog('close');
+									var diag = $(this),
+										context = diag.find('form').serializeObject();
+									if(context.message !== ''){
+										context.type = 'action';
+										context.id = 'comment';
+										context.url = State.url;
+										context.title = State.title;
+										apiCall(context,function(d){
+											if(!exists(d.error)){
+												diag.dialog('close');
+												flag('ignore_statechange',false);
+												loading(false);
+											}
+										});
 									}
 								}
-							},
-							{
+							},{
 								text: 'Cancel',
 								class: 'cancel-force',
 								click: function(){
 									$(this).dialog('close');
+									flag('ignore_statechange',false);
+									loading(false);
 								}
 							}
 						]
@@ -500,12 +513,14 @@
 			render.dialog('#dialog',title,callback);
 		},
 		loading = function(state){
-			state = exists(state)?state:false;
-			console.log('loading state '+state);
-			if(!flag('error') && !state){
-				$('#loading').hide();
-			}else if(state){
-				$('#loading').show();
+			if(!flag('ignore_statechange')){
+				state = exists(state)?state:false;
+				console.log('loading state '+state);
+				if(!flag('error') && !state){
+					$('#loading').hide();
+				}else if(state){
+					$('#loading').show();
+				}
 			}
 		};
 	if(exists($.cookie('key'))){

+ 26 - 3
php/functions.php

@@ -22,10 +22,12 @@
 		}
 		// Title
 		if(is_null($title)){
-			if(!isset($context['title'])){
-				$title = $_GET['id'];
+			if(isset($json['title'])){
+				$title = $json['title'];
+			}elseif(isset($json['state']['title'])){
+				$title = $json['state']['title'];
 			}else{
-				$title = $context['title'];
+				$title = $_GET['id'];
 			}
 		}
 		$json['state']['title'] = $title;
@@ -93,4 +95,25 @@
 			)
 		));
 	}
+	function stateObj($type,$id){
+		$json = Array(
+			'data'=>$_GET
+		);
+		$title = ucwords($type.' - '.$id);
+		switch($type){
+			case 'user':$url='~'.$id;break;
+			case 'group':$url='+'.$id;break;
+			case 'issue':$url='!'.$id;break;
+			case 'page':$url='page-'.$id;break;
+			case 'project':
+				$url=$type.'-'.$id;
+				$title = projectObj($id);
+				$title = 'Project - '.$title['title'];
+			break;
+			default:$url=$type.'-'.$id;
+		}
+		$json['url'] = $url;
+		$json['title'] = $title;
+		return $json;
+	}
 ?>