feature #3748: Add image create dialog

This commit is contained in:
Daniel Molina committed 2015-05-05 13:16:05 +02:00
1 parent d659ec4dd5
commit d7ad90b6a6
5 files changed
+551 -13

No files matched your search

+4 -1
View File
@@ -35,7 +35,10 @@ require.config({
'jquery.cookie': '../bower_components/foundation/js/vendor/jquery.cookie',
'fastclick': '../bower_components/foundation/js/vendor/fastclick',
'modernizr': '../bower_components/foundation/js/vendor/modernizr',
'placeholder': '../bower_components/foundation/js/vendor/placeholder'
'placeholder': '../bower_components/foundation/js/vendor/placeholder',
/* Resumable */
'resumable': '../bower_components/resumablejs/resumable'
},
shim: {
/* jQuery */
@@ -10,14 +10,6 @@ define(function(require) {
return TemplateHTML({dialogId: DIALOG_ID});
}
var _submit = function() {
var name = $('#zonename', this).val();
var endpoint = $("#endpoint", this).val();
var zoneJSON = {"zone" : {"name" : name, "endpoint" : endpoint}};
Sunstone.runAction("Zone.create", zoneJSON);
return false;
}
var _onShow = function(dialog) {
$("#name", dialog).focus();
+4 -4
View File
@@ -7,9 +7,9 @@ define(function(require) {
var TAB_ID = require('./images-tab/tabId');
var DATATABLE_ID = "dataTableImages";
//var _dialogs = [
// require('./images-tab/dialogs/create')
//];
var _dialogs = [
require('./images-tab/dialogs/create')
];
var _panels = [
require('./images-tab/panels/info'),
@@ -30,7 +30,7 @@ define(function(require) {
actions: Actions,
dataTable: new Table(DATATABLE_ID, {actions: true, info: true}),
panels: _panels,
//dialogs: _dialogs
dialogs: _dialogs
};
return Tab;
@@ -0,0 +1,297 @@
define(function(require) {
var Resumable = require('resumable');
var TemplateHTML = require('hbs!./create/html');
var Sunstone = require('sunstone');
var OpenNebulaError = require('opennebula/error');
var Notifier = require('utils/notifier');
var Locale = require('utils/locale');
var Tips = require('utils/tips');
var ResourceSelect = require('utils/resource-select')
var DIALOG_ID = require('./create/dialogId');
var _html = function() {
return TemplateHTML({dialogId: DIALOG_ID});
}
var _onShow = function(dialog) {
$("#img_name", dialog).focus();
var ds_id = $('#img_datastore .resource_list_select', dialog).val();
var ds_id_raw = $('#img_datastore_raw .resource_list_select', dialog).val();
// Filter out DS with type system (1) or file (2)
var filter_att = ["TYPE", "TYPE"];
var filter_val = ["1", "2"];
ResourceSelect.insert('div#img_datastore', dialog, "Datastore",
ds_id, false, null, filter_att, filter_val);
ResourceSelect.insert('div#img_datastore_raw', dialog, "Datastore",
ds_id_raw, false, null, filter_att, filter_val);
return false;
}
var _setup = function(dialog) {
Tips.setup(dialog);
$('.advanced', dialog).hide();
$('#advanced_image_create', dialog).click(function() {
$('.advanced', dialog).toggle();
return false;
});
$('select#img_type', dialog).change(function() {
var value = $(this).val();
switch (value){
case "DATABLOCK":
$('#datablock_img', dialog).removeAttr("disabled");
break;
default:
$('#datablock_img', dialog).attr('disabled', 'disabled');
$('#path_image', dialog).click();
}
});
$('#img_path,#img_fstype,#img_size,#file-uploader', dialog).closest('.row').hide();
$("input[name='src_path']", dialog).change(function() {
var value = $(this).val();
switch (value){
case "path":
$('#img_fstype,#img_size,#file-uploader', dialog).closest('.row').hide();
$('#img_path', dialog).closest('.row').show();
break;
case "datablock":
$('#img_path,#file-uploader', dialog).closest('.row').hide();
$('#img_fstype,#img_size', dialog).closest('.row').show();
break;
case "upload":
$('#img_path,#img_fstype,#img_size', dialog).closest('.row').hide();
$('#file-uploader', dialog).closest('.row').show();
break;
};
});
$('#path_image', dialog).click();
dialog.on('click', '#add_custom_var_image_button', function() {
var name = $('#custom_var_image_name', dialog).val();
var value = $('#custom_var_image_value', dialog).val();
if (!name.length || !value.length) {
Notifier.notifyError(Locale.tr("Custom attribute name and value must be filled in"));
return false;
}
option = '<option value=\'' + value + '\' name=\'' + name + '\'>' +
name + '=' + value +
'</option>';
$('select#custom_var_image_box', dialog).append(option);
return false;
});
dialog.on('click', '#remove_custom_var_image_button', function() {
$('select#custom_var_image_box :selected', dialog).remove();
return false;
});
var img_obj;
if (_getInternetExplorerVersion() > -1) {
$("#upload_image").attr("disabled", "disabled");
} else {
var uploader = new Resumable({
target: 'upload_chunk',
chunkSize: 10 * 1024 * 1024,
maxFiles: 1,
testChunks: false,
query: {
csrftoken: ""//TODO csrftoken
}
});
uploader.assignBrowse($('#file-uploader-input', dialog));
var fileName = '';
var file_input = false;
uploader.on('fileAdded', function(file) {
fileName = file.fileName;
file_input = fileName;
$('#file-uploader-input', dialog).hide()
$("#file-uploader-label", dialog).html(file.fileName);
});
uploader.on('uploadStart', function() {
$('#upload_progress_bars').append('<div id="' + fileName + 'progressBar" class="row" style="margin-bottom:10px">\
<div id="' + fileName + '-info" class="large-2 columns dataTables_info">\
' + Locale.tr("Uploading...") + '\
</div>\
<div class="large-10 columns">\
<div id="upload_progress_container" class="progress nine radius" style="height:25px !important">\
<span class="meter" style="width:0%"></span>\
</div>\
<div class="progress-text" style="margin-left:15px">' + fileName + '</div>\
</div>\
</div>');
});
uploader.on('progress', function() {
$('span.meter', $('div[id="' + fileName + 'progressBar"]')).css('width', uploader.progress() * 100.0 + '%')
});
uploader.on('fileSuccess', function(file) {
$('div[id="' + fileName + '-info"]').text(Locale.tr("Registering in OpenNebula"));
$.ajax({
url: 'upload',
type: "POST",
data: {
csrftoken: csrftoken,
img : JSON.stringify(img_obj),
file: fileName,
tempfile: file.uniqueIdentifier
},
success: function() {
Notifier.notifyMessage("Image uploaded correctly");
$('div[id="' + fileName + 'progressBar"]').remove();
Sunstone.runAction("Image.refresh");
},
error: function(response) {
Notifier.onError({}, OpenNebulaError(response));
$('div[id="' + fileName + 'progressBar"]').remove();
}
});
});
}
$('#'+DIALOG_ID+'Form', dialog).submit(function() {
var exit = false;
var upload = false;
$('.img_man', this).each(function() {
if (!$('input', this).val().length) {
Notifier.notifyError(Locale.tr("There are mandatory parameters missing"));
exit = true;
return false;
}
});
if (exit) { return false; }
var ds_id = $('#img_datastore .resource_list_select', dialog).val();
if (!ds_id) {
Notifier.notifyError(Locale.tr("Please select a datastore for this image"));
return false;
};
var img_json = {};
var name = $('#img_name', dialog).val();
img_json["NAME"] = name;
var desc = $('#img_desc', dialog).val();
if (desc.length) {
img_json["DESCRIPTION"] = desc;
}
var type = $('#img_type', dialog).val();
img_json["TYPE"] = type;
img_json["PERSISTENT"] = $('#img_persistent:checked', dialog).length ? "YES" : "NO";
var dev_prefix = $('#img_dev_prefix', dialog).val();
if (dev_prefix.length) {
img_json["DEV_PREFIX"] = dev_prefix;
}
var driver = $('#img_driver', dialog).val();
if (driver.length)
img_json["DRIVER"] = driver;
var target = $('#img_target', dialog).val();
if (target)
img_json["TARGET"] = target;
switch ($('#src_path_select input:checked', dialog).val()){
case "path":
path = $('#img_path', dialog).val();
if (path) img_json["PATH"] = path;
break;
case "datablock":
size = $('#img_size', dialog).val();
fstype = $('#img_fstype', dialog).val();
if (size) img_json["SIZE"] = size;
if (fstype) img_json["FSTYPE"] = fstype;
break;
case "upload":
upload = true;
break;
}
//Time to add custom attributes
$('#custom_var_image_box option', dialog).each(function() {
var attr_name = $(this).attr('name');
var attr_value = $(this).val();
img_json[attr_name] = attr_value;
});
img_obj = {"image" : img_json,
"ds_id" : ds_id};
//we this is an image upload we trigger FileUploader
//to start the upload
if (upload) {
Sunstone.hideDialog(DIALOG_ID);
Sunstone.resetDialog(DIALOG_ID);
uploader.upload();
} else {
Sunstone.runAction("Image.create", img_obj);
};
return false;
});
$('#create_image_submit_manual', dialog).click(function() {
var template = $('#template', dialog).val();
var ds_id = $('#img_datastore_raw .resource_list_select', dialog).val();
if (!ds_id) {
Notifier.notifyError(Locale.tr("Please select a datastore for this image"));
return false;
};
var img_obj = {
"image" : {
"image_raw" : template
},
"ds_id" : ds_id
};
Sunstone.runAction("Image.create", img_obj);
return false;
});
return false;
}
var _getInternetExplorerVersion = function(){
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
return {
'dialogId': DIALOG_ID,
'html': _html,
'setup': _setup,
'onShow': _onShow
}
});
@@ -0,0 +1,246 @@
<div id="{{dialogId}}" class="reveal-modal medium" role="dialog" data-reveal >
<div class="row create_image_header">
<div class="large-5 columns">
<h3 class="subheader">{{tr "Create Image"}}</h3>
</div>
<div class="large-7 columns">
<dl class="tabs right" data-tab>
<dd class="active">
<a href="#img_easyTab">{{tr "Wizard"}}</a>
</dd>
<dd>
<a href="#img_manualTab">{{tr "Advanced mode"}}</a>
</dd>
</dl>
</div>
</div>
<form id="{{dialogId}}Form" action="" class="custom creation">
<div class="tabs-content">
<div id="img_easyTab" class="content active">
<div class="row vm_param">
<div class="large-12 columns">
<label for="img_name">
{{tr "Name"}}
<span class="tip">
{{tr "Name that the Image will get. Every image must have a unique name."}}
</span>
</label>
<input type="text" name="img_name" id="img_name" />
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label for="img_desc">
{{tr "Description"}}
<span class="tip">
{{tr "Human readable description of the image for other users."}}
</span>
</label>
<textarea name="img_desc" id="img_desc" rows="4"></textarea>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label for="img_type">
{{tr "Type"}}
<span class="tip">
{{tr "Type of the image."}}
<br/>
<br/>
{{tr "OS images contain a working operative system."}}
<br/>
<br/>
{{tr "CDROM images are readonly data."}}
<br/>
<br/>
{{tr "DATABLOCK images are a storage for data. They can be created from previous existing data, or as an empty drive."}}
</span>
</label>
<select name="img_type" id="img_type">
<option value="OS">{{tr "OS"}}</option>
<option value="CDROM">{{tr "CDROM"}}</option>
<option value="DATABLOCK">{{tr "DATABLOCK"}}</option>
</select>
</div>
<div class="large-6 columns">
<label for="img_datastore">
{{tr "Datastore"}}
<span class="tip">{{tr "Select the datastore for this image"}}</span>
</label>
<div id="img_datastore" name="img_datastore"></div>
</div>
<div class="large-6 columns">
{{#if persistentActionEnabled}}
<input type="checkbox" id="img_persistent" name="img_persistent" value="YES" />
<label for="img_persistent">
{{tr "Persistent"}}
<span class="tip">{{tr "Persistence of the image"}}</span>
</label>
{{/if}}
</div>
</div>
<br>
<fieldset>
<legend>{{tr "Image location"}}:</legend>
<div class="row" id="src_path_select">
<div class="large-12 columns text-center">
<input type="radio" name="src_path" id="path_image" value="path">
<label for="path_image">{{tr "Provide a path"}}</label>
<input type="radio" name="src_path" id="upload_image" value="upload">
<label for="upload_image">{{tr "Upload"}}</label>
<input type="radio" name="src_path" id="datablock_img" value="datablock" disabled>
<label for="datablock_img">{{tr "Empty datablock"}}</label>
</div>
</div>
<br>
<div class="img_param row">
<div class="large-12 columns">
<label for="img_path">
{{tr "Path"}}
<span class="tip">
{{tr "Path to the original file that will be copied to the image repository. If not specified for a DATABLOCK type image, an empty image will be created."}}
</span>
</label>
<input type="text" name="img_path" id="img_path" />
</div>
</div>
<div class="row">
<div id="file-uploader" class="large-12 columns text-center">
<label id="file-uploader-label" for="file-uploader-input"></label>
<input id="file-uploader-input" type="file"/>
</div>
</div>
<div class="img_size row">
<div class="large-6 columns">
<label for="img_size">
{{tr "Size"}}
<span class="tip">{{tr "Size of the datablock in MB."}}</span>
</label>
<input type="text" name="img_size" id="img_size" />
</div>
<div class="large-6 columns">
<label for="img_fstype">
{{tr "FS type"}}
<span class="tip">
{{tr "Type of file system to be built."}}
<br>
<br>
{{tr "Plain. When the disk image is used directly by the hypervisor we can format the image, and so it is ready to be used by the guest OS. Values: ext2, ext3, ext4, ntfs, reiserfs, jfs, swap. Any other fs supported by mkfs will work if no special option is needed."}}
<br>
<br>
{{tr "Formatted. The disk image is stored in a hypervisor specific format VMDK or Qcow2. Then we cannot really make a filesystem on the image, just create the device and let the guest OS format the disk. Use raw to not to format the new image. Values: raw, qcow2, vmdk_*."}}
</span>
</label>
<input type="text" name="img_fstype" id="img_fstype" />
</div>
</div>
</fieldset>
<div class="show_hide" id="advanced_image_create">
<h4>
<small> <i class=" fa fa-caret-down"/>{{tr "Advanced options"}}
<a id="add_os_boot_opts" class="icon_left" href="#"></a>
</small>
</h4>
</div>
<div class="advanced">
<div class="row">
<div class="large-6 columns">
<div class="row">
<div class="large-12 columns">
<label for="img_dev_prefix">
{{tr "Device prefix"}}
<span class="tip">
{{tr "Prefix for the emulated device this image will be mounted at. For instance, “hd”, “sd”. If omitted, the default value is the one defined in oned.conf (installation default is “hd”)."}}
</span>
</label>
<input type="text" name="img_dev_prefix" id="img_dev_prefix" />
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label for="img_driver">
{{tr "Driver"}}
<span class="tip">
{{tr "Specific image mapping driver. KVM: raw, qcow2. XEN: tap:aio, file:"}}
</span>
</label>
<input type="text" name="img_driver" id="img_driver" />
</div>
</div>
</div>
<div class="large-6 columns">
<div class="row">
<div class="large-12 columns">
<label for="img_target">
{{tr "Target"}}
<span class="tip">
{{tr "Target on which the image will be mounted at. For example: hda, sdb..."}}
</span>
</label>
<input type="text" name="img_target" id="img_target" />
</div>
</div>
</div>
</div>
<br>
<div class="row">
<fieldset>
<legend>{{tr "Custom attributes"}}</legend>
<div class="row">
<div class="large-6 columns">
<div class="row">
<div class="large-12 columns">
<label for="custom_var_image_name">{{tr "Name"}}</label>
<input type="text" id="custom_var_image_name" name="custom_var_image_name" />
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label for="custom_var_image_value">{{tr "Value"}}</label>
<input type="text" id="custom_var_image_value" name="custom_var_image_value" />
</div>
</div>
<div class="row">
<div class="large-12 columns">
<button class="add_remove_button add_button secondary button small radius" id="add_custom_var_image_button" type="button" value="add_custom_image_var">{{tr "Add"}}</button>
<button class="add_remove_button secondary button small radius" id="remove_custom_var_image_button" type="button" value="remove_custom_image_var">{{tr "Remove selected"}}</button>
</div>
</div>
</div>
<div class="large-6 columns">
<div class="row">
<div class="eight centered columns">
<select id="custom_var_image_box" name="custom_var_image_box" style="height:180px !important; width:100%" multiple></select>
</div>
</div>
</div>
</div>
</fieldset>
</div>
</div>
<div class="form_buttons">
<button class="button success radius right" id="create_image_submit" type="submit" value="image/create">{{tr "Create"}}</button>
<button id="wizard_image_reset_button" class="button secondary radius" type="reset" value="reset">{{tr "Reset"}}</button>
</div>
</div>
<div id="img_manualTab" class="content">
<div class="row">
<div class="columns large-12">
<label for="img_datastores_raw">{{tr "Datastore"}}:</label>
<div id="img_datastore_raw" name="img_datastore_raw"></div>
</div>
</div>
<div class="row">
<div class="columns large-12">
<textarea id="template" rows="15" style="height:380px !important; width:100%;"></textarea>
</div>
</div>
<div class="form_buttons">
<button class="button success radius right" id="create_image_submit_manual" value="image/create">{{tr "Create"}}</button>
<button id="advanced_image_reset_button" class="button secondary radius" type="reset" value="reset">{{tr "Reset"}}</button>
</div>
</div>
</div>
<a class="close-reveal-modal">&#215;</a>
</form>
</div>