View file upload/js/XenCentral/Feedback/Tools.js

File size: 7.92Kb
/*
 * @package XenCentral Feedback System
 * @author DNF Technology
 * @copyright Drnoyan & Nalyan LDA, Portugal, EU
 * @license http://dnf.technology/terms/
 * @link http://customers.dnf.technology
 * @version 2.0.0 Beta 10
 * @revision 12
 */
$(function() {
	$('a.disableLink').click(function(evt) {
		evt.preventDefault();
	});

	$('input[name=usernameFilter]').autocomplete({
		serviceUrl: $('input[name=usernameFilter]').attr('data-href'),
		minChars: 2,
		delimiter: '; ',
		params: {
			
		},
		zIndex:99999,
		onSelect: function(value, data) {
			$('input[name=usernameFilter]').val($('input[name=usernameFilter]').val()+'; ').focus();
		}
	});

	$('div#typeFilterPopup ul li a').click(function(evt){
		evt.preventDefault();
		evt.stopPropagation();

		if($(this).attr('data-filter')==='all') {
			if($(this).hasClass('checked')) {
				$('div#typeFilterPopup ul li a').removeClass('checked');
			} else {
				$('div#typeFilterPopup ul li a').addClass('checked');
			}
		} else {
			if($(this).hasClass('checked')) {
				$(this).removeClass('checked');
				// un-check all checked
				$('div#typeFilterPopup ul li a').each(function(index, firstElem){
					if($(firstElem).attr('data-filter')=='all') {
						$(firstElem).removeClass('checked');
						return false;
					}
				});
			} else {
				$(this).addClass('checked');
			}
		}

		var hasClass=false;
		$('div#typeFilterPopup ul li a').each(function(index, el){
			if($(el).hasClass('checked')) {
				hasClass=true;
			}
		});

		if(hasClass) {
			$('a.typeFilterPopup').addClass('active');
		} else {
			$('a.typeFilterPopup').removeClass('active');
		}
	});

	//  make sure to select tabs based on hash value from the URL
	if(window.location.hash)
	{
		var tabName=window.location.hash.slice(1)+'Tab';
		
		$('.'+tabName).click();
	}

	$('.configureButton').each(function(elementIndex, element){
		// get title bar before the button
		$title=$(element).prev('.titleBar');

		$configureSection=$(element).next('form').find('div.configureSection:first');

		$('a', element).on('click', function(evt){
			evt.preventDefault();
			$configureSection.slideToggle();
		});

		//$configureSection.slideToggle();

		$(element).appendTo($('h1', $title)).fadeIn('slow');
	});

	$('.configureList .row h3 a').each(function(index, link){
		// close all open ones
		$(link).click(function(){

			$('.configureList .row h3.open').each(function(openIndex, openElement){
				$(openElement).removeClass('open');
				$(openElement).nextAll('.options').slideUp();
			});

			if($(link).parent().hasClass('open')) {
				$(link).parent().removeClass('open');
				$(link).parent().nextAll('.options').slideUp();
			} else {
				$(link).parent().addClass('open');
				$(link).parent().nextAll('.options').slideDown();
			}
		});
	});

	$('.configureList input[type=checkbox], .configureList input.date').change(function(){
		setTimeout(function(){
			$('.configureSection').trigger('configupdated');
		}, 100);
	});

	$('.configureSection').bind('configupdated', function(){
		// make sure any feedback type is selected
		if($('#selecttype input:checked', this).length==0) {
			$('.updateButton .ui-button').removeClass('ui-state-active');
			if(!$('.updateButton .ui-button').hasClass('ui-state-disabled')) {
				$('.updateButton .ui-button').addClass('ui-state-disabled')
			}
			return;
		}

		if($('#selectamount input:checked', this).length==0) {
			$('.updateButton .ui-button').removeClass('ui-state-active');
			if(!$('.updateButton .ui-button').hasClass('ui-state-disabled')) {
				$('.updateButton .ui-button').addClass('ui-state-disabled')
			}
			return;
		}

		/*if(!$('input[name=from]', this).val()) {
			$('.updateButton .ui-button').removeClass('ui-state-active');
			if(!$('.updateButton .ui-button').hasClass('ui-state-disabled')) {
				$('.updateButton .ui-button').addClass('ui-state-disabled')
			}
			return;
		}*/

		/*if(!$('input[name=to]', this).val()) {
			$('.updateButton .ui-button').removeClass('ui-state-active');
			if(!$('.updateButton .ui-button').hasClass('ui-state-disabled')) {
				$('.updateButton .ui-button').addClass('ui-state-disabled')
			}
			return;
		}*/
		
		$('div.updateButton .error').html('').fadeOut();

		$('.updateButton .ui-button').removeClass('ui-state-disabled');
		if(!$('.updateButton .ui-button').hasClass('ui-state-active')) {
			$('.updateButton .ui-button').addClass('ui-state-active')
		}
	});

	$('#feedbackReload').submit(function(evt){
        // disabled to implement view more link
        return true;
		var form=this;
		evt.preventDefault();

		var link=$('div.updateButton input', this).get(0);

		var $button=$(link).parent().parent();

		if($(link).parent().length==0) {
			$('div.updateButton .error').html($('.configureSection').attr('data-error')).fadeIn();
			return false;
		}

		var serializedData=$(this).serializeArray();

		serializedData[serializedData.length]={name:'_xfResponseType', value:'json'};

		$('.configureSection').addClass('progress');

		$.ajax({
			url:$(form).attr('action'),
			'success':function(response) {
				if (response.error) {
					if(response.error instanceof Array) {
						$('.error', $button).html(response.error[0]);
					} else {
						$('.error', $button).html(response.error);
					}
					$('.error', $button).fadeIn();
					return;
				}

				$('div.configureButton a').click();

				$('.error', $button).fadeOut();

				$('ol.section.reviewList.updateList').html('').html(response.templateHtml);
			},
			complete:function(response, status) {

				$('.configureSection', form).removeClass('progress');

				if (status === 'success' && status === 'notmodified') {
					try {
						var data = $.parseJSON(response.responseText);
						console.log(data);
						try {
							if(data.error instanceof Array) {
								console.log('is array');
								$('.error', $button).html(data.error[0]);
							} else {
								console.log('not array');
								$('.error', $button).html(data.error);
							}

						} catch($ex1) {
							$('.error', $button).html(data);
						}
					} catch ($ex2) {
						$('.error', $button).html(response.responseText);
					}
					$('.error', $button).fadeIn();
				}
			},
			data:serializedData,
			type:$(this).attr('method'),
			dataType:'json'
		});
	});

	$('div.viewMore a').click(function() {

		var link = this;

        var $reviewList=$(link).parents('div.viewMore').prev('ol.reviewList');

		$(link).parent().addClass('progress');

		var serializedData;
		
		serializedData = [];

		serializedData[serializedData.length] = {name:'_xfResponseType', value:'json'};

		serializedData[serializedData.length] = {name:'_xfToken', value:XF.config.csrf};
		
		serializedData[serializedData.length] = {name:'lastid', value:$('li.primaryContent:last', $reviewList).attr('data-id')};

		$.ajax({
			url:$(link).attr('href'),
			'success':function(response) {

				if (response.error) {
					if (response.error instanceof Array) {
						$(link).parent().html(response.error[0]);
					} else {
						$(link).parent().html(response.error);
					}
					return;
				}

				if(response.empty) {
					$(link).parent().html(response.message);
				}

                /*if(!response.html.replace(/[\s\r\n\t]+/gi, '')) {
                    return;
                }*/

				$(response.html).each(function(index, el){
                    $reviewList.append(el.content);
                    console.log(el.content);
				});
			},
			complete:function(response, status) {

				$(link).parent().removeClass('progress');

				if (status != 'success' && status != 'notmodified') {
					try {
						var data = $.parseJSON(response.responseText);
						try {
							if (data.error instanceof Array) {
								$(link).parent().html(data.error[0]);
							} else {
								$(link).parent().html(data.error);
							}

						} catch($ex1) {
							$(link).parent().html(data);
						}
					} catch ($ex2) {
						$(link).parent().html(response.responseText);
					}
				}
			},
			data:serializedData,
			type:'GET',
			dataType:'json'
		});
	});

	$("#selecttype,#selectamount").buttonset();

	$('.configureSection').trigger('configupdated');
});