1 //require <jquery.packed.js>
  2 //require <xatajax.core.js>
  3 //require <xatajax.form.core.js>
  4 (function(){
  5 	
  6 	var $ = jQuery;
  7 	
  8 	/**
  9 	 * @class
 10 	 * @name actions
 11 	 * @memberOf XataJax
 12 	 * @description Utility functions for dealing with actions and selected actions.
 13 	 */
 14 	if ( typeof(XataJax.actions) == 'undefined' ){
 15 		XataJax.actions = {};
 16 	}
 17 	
 18 	XataJax.actions.doSelectedAction = doSelectedAction;
 19 	XataJax.actions.handleSelectedAction = handleSelectedAction;
 20 	XataJax.actions.hasRecordSelectors = hasRecordSelectors;
 21 	XataJax.actions.getSelectedIds = getSelectedIds;
 22 	
 23 	/**
 24 	 * @function
 25 	 * @memberOf XataJax.actions
 26 	 * @name ConfirmCallback
 27 	 * @description
 28 	 * A callback function that can be passed to doSelectedAction() to serve as 
 29 	 * a confirmation to the user that they want to proceed with the action.
 30 	 *
 31 	 * @param {Array} recordIds An array of record ids that are to be acted upon.
 32 	 * @returns {Boolean} true if the user confirms that they want to proceed.  False otherwise.
 33 	 */
 34 	
 35 	
 36 	/**
 37 	 * @function
 38 	 * @memberOf XataJax.actions
 39 	 * @description
 40 	 * In a result list with checkboxes to select record ids, this gets an array
 41 	 * of the recordIds of the checked records (or a newline-delimited string).
 42 	 *
 43 	 * <p>This is useful for sending to Xataface actions in the --selected-ids parameter
 44 	 * because the df_get_selected_records() function is set up to read the --selected-ids
 45 	 * parameter and return the corresponding records.</p>
 46 	 *
 47 	 * @param {HTMLElement} container The HTML DOM element that contains the checkboxes.
 48 	 * This may be the result list table or a container thereof.
 49 	 * @param {boolean} asString If false, this will return an array of record ids.  If true,
 50 	 * this will return the ids as a newline-delimited string.
 51 	 * @return {mixed} Either an array of record ids or a newline-delimited string of
 52 	 * record ids depending on the value of the <var>asString</var> parameter.
 53 	 *
 54 	 * @example
 55 	 * var ids = XataJax.actions.getSelectedIds($('#result_list'), true);
 56 	 * $.post(DATAFACE_SITE_HREF, {'-action': 'myaction', '--selected-ids': ids}, function(res){
 57 	 *		alert("we did it");
 58 	 * });
 59 	 */
 60 	function getSelectedIds(/*HTMLElement*/ container, asString){
 61 		if ( typeof(asString) == 'undefined' ) asString = false;
 62 		var ids = [];
 63 		var checkboxes = $('input.rowSelectorCheckbox', container);
 64 		checkboxes.each(function(){
 65 			if ( $(this).is(':checked') && $(this).attr('xf-record-id') ){
 66 				ids.push($(this).attr('xf-record-id'));
 67 			}
 68 		});
 69 		if ( asString ) return ids.join("\n");
 70 		return ids;
 71 	
 72 	}
 73 	
 74 	/**
 75 	 * @function
 76 	 * @memberOf XataJax.actions
 77 	 * @description
 78 	 * Performs an action on the currently selected records in a container.
 79 	 *
 80 	 * <p>Note that the selected IDs will be sent to the action in the --selected-ids
 81 	 * POST parameter.  One record ID per line.  See df_get_selected_records() PHP function to load these records.</p>
 82 	 *
 83 	 * @param {Object} params The POST parameters to send to the action.
 84 	 * @param {HTMLElement} container The container that houses the checkboxes.
 85 	 * @param {XataJax.actions.ConfirmCallback} confirmCallback Optional callback function that can be used to prompt the user to confirm that they would like to proceed.
 86 	 * @param {Function} emptyCallback Callback to be called if there are no records currently selected.
 87 	 * @return {void}
 88 	 *
 89 	 * @example
 90 	 * // This will perform the my_special_action action on all selected records in 
 91 	 * // the result_list section of the page.  It looks through the checkboxes.
 92 	 *
 93 	 * XataJax.actions.doSelectedAction({
 94 	 *     '-action': 'my_special_action'
 95 	 *     },
 96 	 *     jQuery('#result_list'),
 97 	 *     function(ids){
 98 	 *         return confirm('This will perform my special action on '+ids.length+' records.  Are you sure you want to proceed?');
 99 	 *     }
100 	 * });
101 	 * 
102 	 */
103 	function doSelectedAction(/*Object*/ params, /*HTMLElement*/container, /*XataJax.actions.ConfirmCallback*/confirmCallback, /*Function*/emptyCallback){
104 		var ids = [];
105 		var checkboxes = $('input.rowSelectorCheckbox', container);
106 		checkboxes.each(function(){
107 			if ( $(this).is(':checked') && $(this).attr('xf-record-id') ){
108 				ids.push($(this).attr('xf-record-id'));
109 			}
110 		});
111 
112 		if ( ids.length == 0 ){
113 			if ( typeof(emptyCallback) == 'function' ){
114 				emptyCallback(params, container);
115 			} else {
116 				alert('No records are currently selected.  Please first select the records that you wish to act upon.');
117 			}
118 			
119 			return;
120 		}
121 		
122 		if ( typeof(confirmCallback) == 'function' ){
123 			if ( !confirmCallback(ids) ){
124 				return;
125 			}
126 		}
127 		//alert(ids);
128 		params['--selected-ids'] = ids.join("\n");
129 		
130 		XataJax.form.submitForm('post', params);
131 	
132 	}
133 	
134 	/**
135 	 * @function
136 	 * @memberOf XataJax.actions
137 	 * @description
138 	 * Checks to see if the given element contains any selector checkboxes for selecting records.
139 	 *
140 	 * @param {HTMLElement} container  The html element to check.
141 	 * @return {boolean} True if it contains at least one selector checkbox.
142 	 */
143 	function hasRecordSelectors(/*HTMLElement*/container){
144 		return ($('input.rowSelectorCheckbox', container).size() > 0);
145 	}
146 	
147 	
148 	/**
149 	 * @function
150 	 * @memberOf XataJax.actions
151 	 * @description
152 	 * Handles a selected action that was triggered using a given link.  The link itself
153 	 * should contain the information about the action to be performed.
154 	 *
155 	 * @param {HTMLElement} aTag The html link that was clicked to invoke the action.  The 
156 	 *   href tag for this link is used as the target action to perform - except the parameters
157 	 *   are parsed out so that the action will ultimately be submitted via POST.
158 	 *
159 	 * @param {String} selector The selector to the container thart contains the checkboxes
160 	 *   representing the selected records on which to perform this action.
161 	 */
162 	function handleSelectedAction(/*HTMLElement*/ aTag, selector){
163 		var href = $(aTag).attr('href');
164 		var confirmMsg = $(aTag).attr('data-xf-confirm-message');
165 		var confirmCallback = null;
166 		if ( confirmMsg ){
167 			confirmCallback = function(){
168 				return confirm(confirmMsg);
169 			};
170 		}
171 		//alert(confirmMsg);
172 		var params = XataJax.util.getRequestParams(href);
173 
174 		XataJax.actions.doSelectedAction(params, $(selector), confirmCallback);
175 		return false;
176 	
177 	}
178 	
179 })();