thd/web/js/.svn/text-base/uc.utils.js.svn-base
changeset 104 8e4fe6f3337d
parent 103 d2af8a210f5d
child 105 c8f710cd1fb1
equal deleted inserted replaced
103:d2af8a210f5d 104:8e4fe6f3337d
     1 uc = uc || {};
       
     2 uc.utils = uc.utils || {};
       
     3 
       
     4 uc.utils.centeredContainer = function(html_container) {
       
     5 	this.html_container = html_container;
       
     6 }
       
     7 
       
     8 uc.utils.centeredContainer.currentContainer = null;
       
     9 
       
    10 uc.utils.centeredContainer.centerCurrent = function() {
       
    11 	// Center popup in the browser window
       
    12 	if (uc.utils.centeredContainer.currentContainer == null) return;
       
    13 	var jq_container = uc.utils.centeredContainer.currentContainer;
       
    14 	var jq_window = jQuery(window);
       
    15 	var popup_x = jq_window.scrollLeft() + ((jq_window.width() - jq_container.width()) / 2);
       
    16 	var popup_y = jq_window.scrollTop() + ((jq_window.height() - jq_container.height()) / 2);
       
    17 	jq_container.css('top', popup_y);
       
    18 	jq_container.css('left', popup_x);
       
    19 }
       
    20 
       
    21 uc.utils.centeredContainer.showCurrent = function() {
       
    22 	if (uc.utils.centeredContainer.currentContainer == null) return;
       
    23 	var jq_container = uc.utils.centeredContainer.currentContainer;
       
    24 	var jq_window = jQuery(window);
       
    25 	jq_window.bind("resize", uc.utils.centeredContainer.centerCurrent);
       
    26 	jq_window.bind("scroll", uc.utils.centeredContainer.centerCurrent);
       
    27 	uc.utils.centeredContainer.centerCurrent();
       
    28 	jq_container.show();
       
    29 }
       
    30 
       
    31 uc.utils.centeredContainer.hideCurrent = function() {
       
    32 	if (uc.utils.centeredContainer.currentContainer == null) return;
       
    33 	var jq_container = uc.utils.centeredContainer.currentContainer;
       
    34 	var jq_window = jQuery(window);
       
    35 	jq_container.hide();
       
    36 	jq_container.remove();
       
    37 	jq_window.unbind("resize", uc.utils.centeredContainer.centerCurrent);
       
    38 	jq_window.unbind("scroll", uc.utils.centeredContainer.centerCurrent);
       
    39 }
       
    40 
       
    41 uc.utils.centeredContainer.prototype = {
       
    42 	show: function() {
       
    43 		this.hide();
       
    44 		var jq_body = jQuery("body");
       
    45 		var jq_container = jQuery(this.html_container);
       
    46 		jq_container.hide();
       
    47 		jq_container.css('position', 'absolute');
       
    48 		jq_container.css('display', 'block');
       
    49 		jq_body.append(jq_container);
       
    50 		uc.utils.centeredContainer.currentContainer = jq_container;
       
    51 		uc.utils.centeredContainer.showCurrent();
       
    52 	},
       
    53 	hide: function() {
       
    54 		uc.utils.centeredContainer.hideCurrent();
       
    55 	}
       
    56 }
       
    57 
       
    58 uc.utils.showModalWrapper = function() {
       
    59 	if (jQuery('#modal-wrapper').length > 0) return false;
       
    60 	var jq_body = jQuery("body");
       
    61 	var jq_modal_wrapper = jQuery('<div id="modal-wrapper"></div>');
       
    62 	jq_modal_wrapper.height(jq_body.height());
       
    63 	jq_body.append(jq_modal_wrapper);
       
    64 }
       
    65 
       
    66 uc.utils.hideModalWrapper = function() {
       
    67 	jQuery('#modal-wrapper').remove();
       
    68 }
       
    69 
       
    70 uc.utils.showLoader = function() {
       
    71 	var loader = new uc.utils.centeredContainer('<img id="loader" src="/images/pictos/loader.gif" />')
       
    72 	loader.show();
       
    73 }
       
    74 
       
    75 uc.utils.hideLoader = function() {
       
    76 	jQuery('#loader').remove();
       
    77 }
       
    78 
       
    79 uc.utils.executeServerJS = function(http_url, http_data, http_method, complete_callback) {
       
    80 	if (http_method == null) http_method = 'GET';
       
    81 	if (complete_callback == null) complete_callback = function () {};
       
    82 
       
    83 	if (http_method == 'POST') uc.utils.showModalWrapper();
       
    84 	uc.utils.showLoader();
       
    85 
       
    86 	jQuery.ajax({
       
    87 		url: http_url,
       
    88 		type: http_method,
       
    89 		data: http_data,
       
    90 		beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "application/javascript");},
       
    91 		complete: function (XMLHttpRequest, textStatus) {uc.utils.hideLoader(); complete_callback();},
       
    92 		success: function (data, textStatus) {},
       
    93 		dataType: "script"
       
    94 	});
       
    95 }
       
    96 
       
    97 uc.utils.ajaxifyLink = function(jq_selector, http_method, complete_callback) {
       
    98 	jq_selector.removeClass('ajax').removeClass('ajax-post');
       
    99 	jq_selector.parents('.ajax').removeClass('ajax').removeClass('ajax-post');
       
   100 
       
   101 	jq_selector.each(function () {
       
   102 		var jq_this = jQuery(this);
       
   103 		var ajax_url = jq_this.attr('href');
       
   104 
       
   105 		if (ajax_url.indexOf('javascript:') == 0) return false;
       
   106 
       
   107 		jq_this.attr('href', 'javascript:void(0);');
       
   108 		jq_this.removeClass('ajax');
       
   109 		jq_this.click(function() {
       
   110 			uc.utils.executeServerJS(ajax_url, {}, http_method, complete_callback);
       
   111 			return false;
       
   112 		});
       
   113 	});
       
   114 }
       
   115 
       
   116 uc.utils.ajaxifyForm = function(jq_selector, complete_callback) {
       
   117 	jq_selector.removeClass('ajax');
       
   118 	jq_selector.submit(function() {
       
   119 		var jq_this = jQuery(this);
       
   120 		var ajax_url = jq_this.attr('action');
       
   121 		var ajax_data = {};
       
   122 
       
   123 		// Extract data of form
       
   124 		jQuery('textarea, input:text,  input:password, input:hidden, select, input:checked, input:selected', jq_this).each(function () {
       
   125 			var jq_input = jQuery(this);
       
   126 			ajax_data[jq_input.attr('name')] = jq_input.val();
       
   127 		});
       
   128 
       
   129 		uc.utils.executeServerJS(ajax_url, ajax_data, 'POST', complete_callback);
       
   130 
       
   131 		// Do not process normal http request
       
   132 		return false;
       
   133 	});
       
   134 }
       
   135 
       
   136 uc.utils.buildSelectList = function(jq_selector) {
       
   137 	// Wait 1 second to make sure css is loaded
       
   138 	window.setTimeout(function() {
       
   139 		jq_selector.each(function() {
       
   140 			var jq_select = jQuery(this);
       
   141 
       
   142 			// Check for an existing selectedItem
       
   143 			var jq_selected_item = jQuery('li.selected', jq_select);
       
   144 			if (jq_selected_item.length == 1) {
       
   145 				// Scroll to this item
       
   146 				var top = jq_selected_item.position().top;
       
   147 				if (top > 200) jq_select.scrollTop(top-200);
       
   148 			}
       
   149 
       
   150 			jQuery('li a', jq_select).click(function() {
       
   151 				jQuery('li.selected', jq_select).toggleClass('selected');
       
   152 				jQuery(this).parents('li').toggleClass('selected');
       
   153 			});
       
   154 		});}, 1000);
       
   155 }