//-- copyright // OpenProject is a project management system. // // Copyright (C) 2012-2013 the OpenProject Team // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License version 3. // // See doc/COPYRIGHT.rdoc for more details. //++ var ModalHelper = (function() { var ModalHelper = function(timeline, options) { this.options = options; this.timeline = timeline; } /** display the loading modal (spinner in a box) * also fix z-index so it is always on top. */ ModalHelper.prototype.showLoadingModal = function() { jQuery('#ajax-indicator').show().css('zIndex', 1020); }; /** hide the loading modal */ ModalHelper.prototype.hideLoadingModal = function() { jQuery('#ajax-indicator').hide(); }; /** submit a form in the background. * @param form: form element * @param url: url to submit to. can be undefined if so, url is taken from form. * @param callback: called with results */ //TODO fix this inconsistency w/ optional url. ModalHelper.prototype.submitBackground = function(form, url, callback) { var data = form.serialize(); if (typeof url === 'function') { callback = url; url = undefined; } if (typeof url === 'undefined') { url = form.attr('action'); } jQuery.ajax({ type: 'POST', url: url, data: data, error: function(obj, error) { callback(obj.status, obj.responseText); }, success: function(response) { callback(null, response); } }); }; /** create a planning modal * @param type either new, edit or show. * @param projectId id of the project to create the modal for. * @param elementId element id to create the modal for. not needed for new type. * @param callback called when done */ ModalHelper.prototype.createPlanningModal = function(type, projectId, elementId, callback) { var modalHelper = this; var timeline = modalHelper.timeline; var non_api_url = modalHelper.options.url_prefix + modalHelper.options.project_prefix + "/" + projectId + '/planning_elements/'; var api_url = modalHelper.options.url_prefix + modalHelper.options.api_prefix + modalHelper.options.project_prefix + "/" + projectId + '/planning_elements/'; if (typeof elementId === 'function') { callback = elementId; elementId = undefined; } // in the following lines we create the url to get the data from // also we create the url we submit the data to for the edit action. //TODO: escape projectId and elementId. if (type === 'new') { non_api_url += 'new.js'; } else if (type === 'edit') { if (typeof elementId === 'string' || typeof elementId === 'number') { non_api_url += elementId + '/edit.js'; api_url += elementId + '.json'; } else { throw new Error('need an element id for editing.'); } } else if (type === 'show') { if (typeof elementId === 'string' || typeof elementId === 'number') { non_api_url += elementId + '.js'; } else { throw new Error('need an element id for showing.'); } } else { throw new Error('invalid action. allowed: new, show, edit'); } //create the modal by using the html the url gives us. modalHelper.createModal(non_api_url, function(ele) { var projects = timeline.projects; var project; var projectSelect; var fields = ele.find(':input'); ele.data('changed', false); var submitFunction = function(e) { modalHelper.showLoadingModal(); if (type === 'new') { api_url = modalHelper.options.url_prefix + modalHelper.options.api_prefix + modalHelper.options.project_prefix + "/" + projectSelect.val() + '/planning_elements.json'; } modalHelper.submitBackground(jQuery(this), api_url, function(err, res) { var element; modalHelper.hideLoadingModal(); // display errors correctly. if (!err) { currentURL = ''; timeline.reload(); if (elementId === undefined) { try { // internet explorer has a text attribute instead of textContent. element = res.getElementsByTagName('id')[0]; elementId = element.textContent || element.text; } catch (e) { console.log(e); } } if (elementId !== undefined) { modalHelper.createPlanningModal('show', projectId, elementId); } } else if (err !== '500') { ele.find('.errorExplanation').remove(); var error = jQuery('
').attr('class', 'errorExplanation').attr('id', 'errorExplanation'); var json = jQuery.parseJSON(res); var i, errorSpan, errorFormEle; var errorField; for (errorField in json.errors) { if (json.errors.hasOwnProperty(errorField)) { //for (i = 0; i < json.errors.length; i += 1) { error.append( jQuery('