Commit 66211d9a authored by Romain Courteaud's avatar Romain Courteaud

slapos_panel: prefill all Software Product info from JS

parent e2d5020e
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SlapOS product import gadget</title>
<link rel="http://www.renderjs.org/rel/interface" href="interface_erp5_form_content_provider.html">
<link rel="stylesheet" href="gadget_editor.css">
<script src="rsvp.js"></script>
<script src="renderjs.js"></script>
<script src="jiodev.js"></script>
<script src="slapos_master_panel_import_product.js"></script>
</head>
<body>
</body>
</html>
\ No newline at end of file
/*global window, rJS, RSVP, domsugar, jIO, console */
/*jslint nomen: true, maxlen:80, indent:2*/
(function (window, rJS, RSVP, domsugar, jIO, console) {
"use strict";
var DISPLAY_URL = 'display_url',
FIELD_SCOPE = 'url_input';
function renderSoftwareReleaseURLView(gadget) {
return gadget.declareGadget('gadget_html5_input.html', {scope: FIELD_SCOPE})
.push(function (sub_gadget) {
return sub_gadget.render({
value: gadget.state.value,
editable: gadget.state.editable,
required: gadget.state.required,
id: gadget.state.key,
name: gadget.state.key,
type: 'url'
})
.push(function () {
domsugar(gadget.element, [
sub_gadget.element
]);
});
});
}
function getContentFromSoftwareReleaseURLView(gadget) {
var software_url;
return gadget.getDeclaredGadget(FIELD_SCOPE)
.push(function (sub_gadget) {
return sub_gadget.getContent();
})
.push(function (result) {
software_url = result[gadget.state.key];
return jIO.util.ajax({
url: software_url + '.json',
dataType: 'json'
});
})
.push(function (evt) {
var software_json = evt.target.response,
software_type_array = [],
software_type,
result = {};
console.log(software_json);
for (software_type in software_json['software-type']) {
if (software_json['software-type'].hasOwnProperty(software_type)) {
software_type_array.push(software_type);
}
}
result[gadget.state.title_key] = software_json.name;
result[gadget.state.description_key] = software_json.description || "";
result[gadget.state.software_release_key] = software_url;
result[gadget.state.software_type_key] = software_type_array;
console.log(result);
return result;
});
}
rJS(window)
.declareMethod('render', function (options) {
console.log(options);
return this.changeState({
display_step: DISPLAY_URL,
value: options.value,
editable: options.editable,
// required is not a gadget field parameter. TOFIX
required: true,//options.required,
key: options.key,
title_key: options.title_key,
description_key: options.description_key,
software_release_key: options.software_release_key,
software_type_key: options.software_type_key
});
})
.onStateChange(function (modification_dict) {
var gadget = this;
if (modification_dict.display_step === DISPLAY_URL) {
return renderSoftwareReleaseURLView(gadget);
}
if (modification_dict.hasOwnProperty('display_step')) {
throw new Error('Unhandled display step: ' + gadget.state.display_step);
}
})
//////////////////////////////////////////////////
// Used when submitting the form
//////////////////////////////////////////////////
.declareMethod('getContent', function () {
console.log('getContent');
var gadget = this,
display_step = gadget.state.display_step,
queue;
if (gadget.state.display_step === DISPLAY_URL) {
queue = new RSVP.Queue(getContentFromSoftwareReleaseURLView(gadget));
} else {
throw new Error('getContent form not handled: ' + display_step);
}
return queue;
}, {mutex: 'changestate'})
.declareAcquiredMethod("notifyValid", "notifyValid")
.declareAcquiredMethod("notifyInvalid", "notifyInvalid")
.declareMethod('checkValidity', function () {
var gadget = this,
result;
// XXX Cache getContent result (to prevent doing 2 ajax queries)
return new RSVP.Queue(getContentFromSoftwareReleaseURLView(gadget))
.push(function () {
result = true;
return gadget.notifyValid();
}, function (error) {
result = false;
console.warn(error);
return gadget.notifyInvalid('Can not extract software release json');
})
.push(function () {
return result;
});
}, {mutex: 'changestate'});
}(window, rJS, RSVP, domsugar, jIO, console));
\ No newline at end of file
......@@ -11,10 +11,11 @@ software_product.newContent(
title=software_release,
url_string=software_release
)
software_product.newContent(
portal_type="Software Product Type Variation",
title=software_type
)
for software_type in software_type_list:
software_product.newContent(
portal_type="Software Product Type Variation",
title=software_type
)
software_product.validate()
return software_product.Base_redirect(
......
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>title, software_release, software_type, **kw</string> </value>
<value> <string>title, software_release, software_type_list, description="", **kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -90,9 +90,7 @@
<key> <string>left</string> </key>
<value>
<list>
<string>your_title</string>
<string>your_software_release</string>
<string>your_software_type</string>
<string>your_product_import_json</string>
</list>
</value>
</item>
......
......@@ -10,14 +10,17 @@
<key> <string>delegated_list</string> </key>
<value>
<list>
<string>required</string>
<string>gadget_url</string>
<string>renderjs_extra</string>
<string>title</string>
<string>validator_field_id</string>
<string>validator_form_id</string>
</list>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>your_software_release</string> </value>
<value> <string>your_product_import_json</string> </value>
</item>
<item>
<key> <string>message_values</string> </key>
......@@ -57,6 +60,22 @@
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>gadget_url</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>validator_field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>validator_form_id</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
......@@ -66,20 +85,51 @@
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string>my_string_field</string> </value>
<value> <string>my_gadget_field</string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string>Base_viewFieldLibrary</string> </value>
</item>
<item>
<key> <string>required</string> </key>
<value> <int>1</int> </value>
<key> <string>gadget_url</string> </key>
<value> <string>slapos_master_panel_import_product.html</string> </value>
</item>
<item>
<key> <string>renderjs_extra</string> </key>
<value>
<list>
<tuple>
<string>title_key</string>
<string>title</string>
</tuple>
<tuple>
<string>description_key</string>
<string>description</string>
</tuple>
<tuple>
<string>software_release_key</string>
<string>software_release</string>
</tuple>
<tuple>
<string>software_type_key</string>
<string>software_type_list</string>
</tuple>
</list>
</value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Software Release</string> </value>
</item>
<item>
<key> <string>validator_field_id</string> </key>
<value> <string>my_core_mode_text_content_validator</string> </value>
</item>
<item>
<key> <string>validator_form_id</string> </key>
<value> <string>erp5_core/Base_viewFieldLibrary</string> </value>
</item>
</dictionary>
</value>
</item>
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ProxyField" module="Products.ERP5Form.ProxyField"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>delegated_list</string> </key>
<value>
<list>
<string>required</string>
<string>title</string>
</list>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>your_software_type</string> </value>
</item>
<item>
<key> <string>message_values</string> </key>
<value>
<dictionary>
<item>
<key> <string>external_validator_failed</string> </key>
<value> <string>The input failed the external validator.</string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>overrides</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>tales</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>values</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string>my_string_field</string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string>Base_viewFieldLibrary</string> </value>
</item>
<item>
<key> <string>required</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Software Type</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ProxyField" module="Products.ERP5Form.ProxyField"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>delegated_list</string> </key>
<value>
<list>
<string>required</string>
</list>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>your_title</string> </value>
</item>
<item>
<key> <string>message_values</string> </key>
<value>
<dictionary>
<item>
<key> <string>external_validator_failed</string> </key>
<value> <string>The input failed the external validator.</string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>overrides</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>tales</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>values</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string>my_view_mode_title</string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string>Base_viewFieldLibrary</string> </value>
</item>
<item>
<key> <string>required</string> </key>
<value> <int>1</int> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment