// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. (function($, cloudStack) { /** * Instance wizard */ cloudStack.uiCustom.instanceWizard = function(args) { return function(listViewArgs) { var context = listViewArgs.context; var instanceWizard = function(data) { var $wizard = $('#template').find('div.instance-wizard').clone(); var $progress = $wizard.find('div.progress ul li'); var $steps = $wizard.find('div.steps').children().hide(); var $diagramParts = $wizard.find('div.diagram').children().hide(); var $form = $wizard.find('form'); $form.validate(); // Close instance wizard var close = function() { $wizard.dialog('destroy'); $('div.overlay').fadeOut(function() { $('div.overlay').remove(); }); }; // Save instance and close wizard var completeAction = function() { var data = cloudStack.serializeForm($form); var $wizardLoading = $('
').addClass('loading-overlay').appendTo($wizard).css('z-index', '10000'); // Pass network IPs data['my-network-ips'] = []; $form.find('.my-networks .select .specify-ip input[type=text]').each(function() { var $input = $(this); if (!$input.closest('.select').find('input[type=checkbox]').is(':checked')) return true; data['my-network-ips'].push( $input.closest('.select').hasClass('advanced') ? $input.val() : null ); }); data['new-network-ip'] = $form.find('.new-network .select.advanced .specify-ip input[type=text]').val(); // Handle multi-disk service offerings if ($form.find('.multi-disk-select-container').length) { data['disk-offerings-multi'] = []; var $diskGroups = $form.find('.disk-select-group'); var $selectedDisks = $.grep($diskGroups, function (diskGroup) { return $(diskGroup).find('input[type=checkbox]:checked').length; }); $selectedDisks.map(function (disk) { data['disk-offerings-multi'].push( $.extend($(disk).data('json-obj'), { _diskOfferingId: $(disk).find('input[type=radio]:checked').val() }) ); }); } args.action({ // Populate data context: context, data: data, $wizard: $wizard, response: { success: function(args) { var $listView = $('.list-view.instances'); if ($listView.length) { var $loading = $('.list-view.instances').listView('prependItem', { data: [{ name: data.displayname ? data.displayname : _l('label.new.vm'), zonename: $wizard.find('select[name=zoneid] option').filter(function() { return $(this).val() == data.zoneid; }).html(), state: 'Creating' }], actionFilter: function(args) { return []; } }); } listViewArgs.complete({ _custom: args._custom, messageArgs: cloudStack.serializeForm($form), $item: $listView.length ? $loading : $('
') }); close(); }, error: function(message) { $wizard.remove(); $('div.overlay').remove(); if (message) { cloudStack.dialog.notice({ message: message }); } } } }); }; var makeSelects = function(name, data, fields, options, selectedObj, selectedObjNonEditable) { var $selects = $('
'); options = options ? options : {}; $(data).each(function() { var item = this; var id = item[fields.id]; var $select = $('
') .addClass('select') .append( $('') .attr({ type: (function(type) { return type ? type : 'radio'; })(options ? options.type : null), name: name, 'wizard-field': options['wizard-field'] }) .val(id) .click(function() { var $select = $(this).closest('.select'); var isSingleSelect = $select.hasClass('single-select'); var $radio = $select.find('input[type=radio]'); var $newNetwork = $(this).closest('.content').find('.select.new-network'); var $otherSelects = $select.siblings().filter(':visible'); var isCheckbox = $(this).attr('type') == 'checkbox'; if (isCheckbox) { if (isSingleSelect) { $select.siblings('.single-select:visible').find('input[type=checkbox]') .attr('checked', false); var $checkedOtherSelect = $otherSelects.filter(function() { return $(this).not('.single-select') && $(this).find('input[type=checkbox]:checked').length && $(this).find('input[type=radio]:checked').length; }); if (!$checkedOtherSelect.length && !$('.new-network:visible input[type=radio]:checked').length) { $(this).closest('.select').find('input[type=radio]').click(); } } if ((!$otherSelects.length) && $newNetwork.find('input[type=checkbox]').is(':unchecked')) { // Set as default $(this).closest('.select').find('input[type=radio]').click(); } } if ($radio.is(':checked') && !$(this).is(':checked')) { if (!$radio.closest('.select').index()) { return false; } else { $otherSelects.filter(':first') .find('input[type=radio]').click(); } } return true; }) ) .append( $('
').addClass('select-desc') .append($('
').addClass('name').html(_s(this[fields.name]))) .append($('
').addClass('desc').html(_s(this[fields.desc]))) ) .data('json-obj', this); if (selectedObj != null && selectedObj.id == item.id) { $select.find('input[type=checkbox]').attr('checked', 'checked'); if (selectedObjNonEditable) { $select.find('input[type=checkbox]').attr('disabled', 'disabled'); $select.find('input[type=checkbox]').clone().attr({ type: 'hidden', disabled: false }).appendTo($selects); } } $selects.append($select); if (item._singleSelect) { $select.addClass('single-select'); } if (options.secondary) { var $secondary = $('
').addClass('secondary-input').append( $('') .attr({ type: options.secondary.type, name: options.secondary.name, 'wizard-field': options.secondary['wizard-field'] }) .val(id) .click(function() { var $checkbox = $(this).closest('.select').find('input[type=checkbox]'); if (!$checkbox.is(':checked')) { $checkbox.attr('checked', true); } if ($(this).closest('.select-container').hasClass('single-select')) { $(this).closest('.select').siblings().find('input[type=checkbox]') .attr('checked', false); } if ($select.hasClass('single-select')) { $select.siblings('.single-select:visible').find('input[type=checkbox]') .attr('checked', false); } }) .after( $('
').addClass('name').html(options.secondary.desc) ) ).appendTo($select); } }); cloudStack.evenOdd($selects, 'div.select', { even: function($elem) { $elem.addClass('even'); }, odd: function($elem) { $elem.addClass('odd'); } }); return $selects.children(); }; var dataProvider = function(step, providerArgs, callback) { // Call appropriate data provider args.steps[step - 1]($.extend(providerArgs, { currentData: cloudStack.serializeForm($form), initArgs: args, context: context })); }; var dataGenerators = { setup: function($step, formData) { var originalValues = function(formData, initialValue) { var selectedValue = formData.zoneid || initialValue; $step.find('select').val(selectedValue); $step.find('input[type=radio]').filter(function() { return $(this).val() == formData['select-template']; }).click(); }; return { response: { success: function(args) { // Zones var initialValue = ''; $(args.data.zones).each(function( index ) { if(index == 0){ initialValue = this.id; } $step.find('.select-zone select').append( $('