Commit e096541e authored by Ana Rute Mendes's avatar Ana Rute Mendes 🌈

Add snipe-it custom JavaScript validation

The only way I found to add a custom js file was directly in the
webroot folder. To make sure it never generate conflicts when updaitng
phab, I created an extensions folder in the js directory.

This file is called by the SnipeHelper class.
parent d1178006
......@@ -514,6 +514,7 @@ return array(
'rsrc/js/core/darkconsole/DarkMessage.js' => '26cd4b73',
'rsrc/js/core/darkconsole/behavior-dark-console.js' => '457f4d16',
'rsrc/js/core/phtize.js' => '2f1db1ed',
'rsrc/js/extensions/snipe-fields-validation.js' => 'c7f28d14',
'rsrc/js/phui/behavior-phui-dropdown-menu.js' => '5cf0501a',
'rsrc/js/phui/behavior-phui-file-upload.js' => 'e150bd50',
'rsrc/js/phui/behavior-phui-selectable-list.js' => 'b26a41e4',
......@@ -906,6 +907,7 @@ return array(
'releeph-request-differential-create-dialog' => '0ac1ea31',
'releeph-request-typeahead-css' => 'bce37359',
'setup-issue-css' => '5eed85b2',
'snipe-fields-validation-js' => 'c7f28d14',
'sprite-login-css' => '18b368a6',
'sprite-tokens-css' => 'f1896dc5',
'syntax-default-css' => '055fc231',
......
/**
* @provides snipe-fields-validation-js
*/
function shipping_validations() {
let styles = `
.snipe_assets_select {
display: block;
}
.item_card {
border-bottom: 1px dashed #ccc;
padding-bottom: 1em;
margin-bottom: 1em;
clear:both;
}
`;
var styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
asset_id = document.getElementsByName("std:maniphest:shipping:asset-id")[0];
asset_id.parentNode.parentNode.style.display = "none";
items_container = document.createElement("div");
items_container.setAttribute("id", "items_container");
tags_container = document.createElement("div");
items_container.appendChild(tags_container);
assets = document.getElementById("snipe_assets");
let asset_ids_array = asset_id.value.split(",");
for (var i = 0; i < assets.options.length; i++) {
assets.options[i].selected =
asset_ids_array.indexOf(assets.options[i].value) >= 0;
}
let ship_to_customer = document.getElementsByName(
"std:maniphest:shipping:ship-to-customer"
)[0];
ship_to_customer.onchange = function() {
if (ship_to_customer.checked) {
ship_to_customer.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling.style.display =
"none";
} else {
ship_to_customer.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling.style.display =
"block";
}
};
let import_asset = document.getElementsByName(
"std:maniphest:shipping:import-asset"
)[0];
let el_assets_value = document.getElementsByName(
"std:maniphest:shipping:item-value"
)[0];
let el_country_of_origin = document.getElementsByName(
"std:maniphest:shipping:item-origin"
)[0];
let el_currency = document.getElementsByName(
"std:maniphest:shipping:currency"
)[0];
let el_description = document.getElementsByName(
"std:maniphest:shipping:item-description"
)[0];
let el_dimensions = document.getElementsByName(
"std:maniphest:shipping:item-dimensions"
)[0];
let el_weight = document.getElementsByName(
"std:maniphest:shipping:item-weight"
)[0];
if (!import_asset.checked) {
assets.parentNode.parentNode.style.display = "none";
}
import_asset.onchange = function() {
if (import_asset.checked) {
items_container.style.display = "block";
assets.parentNode.parentNode.style.display = "block";
} else {
items_container.style.display = "none";
assets.parentNode.parentNode.style.display = "none";
items_container.innerText = "";
assets.value = "";
asset_id.value = "";
}
};
assets.onchange = function() {
document
.querySelectorAll(".asset_tag, .item_card")
.forEach(e => e.remove());
el_description.value = "";
let values = Array.from(assets.selectedOptions).map(v => v.value);
let items = Array.from(assets.selectedOptions).map(option => {
el = document.createElement("a");
el.textContent = option.text;
el.classList.add(
"asset_tag",
"jx-tokenizer-token",
"jx-tokenizer-token-object",
"green"
);
el.setAttribute("target", "blank");
el.setAttribute("href", option.attributes["data-src"].value);
close_btn = document.createElement("a");
close_btn.className = "jx-tokenizer-x";
close_btn.setAttribute("data-sigil", "remove");
close_btn.text = "x";
close_btn.onclick = function() {
close_btn.parentNode.remove();
};
el.appendChild(close_btn);
return {
name: option.textContent,
href: option.attributes["data-src"].value,
props: {
purchase_cost: {
value: option.attributes["data-purchase-cost"].value,
label: "Purchase cost"
},
currency: {
value: option.attributes["data-currency"].value,
label: "Currency"
},
country_of_origin: {
value: option.attributes["data-country-of-origin"].value,
label: "Country of origin"
},
weight: {
value: option.attributes["data-weight"].value,
label: "Weight"
},
dimensions: {
value: option.attributes["data-dimensions"].value,
label: "Dimensions"
}
}
};
});
items.forEach(function(item) {
item_element = document.createElement("div");
item_element.className = "item_card";
item_url = document.createElement("a");
item_url.textContent = item["name"];
item_url.setAttribute("target", "blank");
item_url.setAttribute("href", item["href"]);
item_element.appendChild(item_url);
el_description.value = el_description.value + item["name"];
for (var key in item.props) {
prop = document.createElement("p");
prop.textContent = item.props[key].label + ": " + item.props[key].value;
item_element.appendChild(prop);
el_description.value = el_description.value + "\n" + prop.textContent;
}
items_container.appendChild(item_element);
el_description.value = el_description.value + "\n\n";
if (items.length == 1) {
el_country_of_origin.value = item.props.country_of_origin.value;
el_dimensions.value = item.props.dimensions.value;
el_weight.value = item.props.weight.value;
el_currency.value = item.props.currency.value;
}
});
if (items.length > 1) {
el_country_of_origin.value = "";
el_dimensions.value = "";
el_weight.value = "";
el_currency.value = "";
}
assets.parentNode.appendChild(items_container);
el_assets_value.value = items.reduce(function(sum, i) {
return sum + Number(i.props.purchase_cost.value);
}, 0);
asset_id.value = values;
};
}
function purchasing_validations() {
let should_create = document.getElementsByName(
"std:maniphest:purchasing:create-item"
)[0];
let asset_name_parent = document.getElementsByName(
"std:maniphest:purchasing:asset-name"
)[0].parentNode.parentNode;
let item_owner_parent = document.getElementsByName(
"std:maniphest:purchasing:item-owner"
)[0].parentNode.parentNode;
let dimensions_parent = document.getElementsByName(
"std:maniphest:shipping:item-dimensions"
)[0].parentNode.parentNode;
let model_parent = document.getElementsByName(
"std:maniphest:purchasing:model"
)[0].parentNode.parentNode;
let country_of_origin_parent = document.getElementsByName(
"std:maniphest:purchasing:country-origin"
)[0].parentNode.parentNode;
let weight_parent = document.getElementsByName(
"std:maniphest:shipping:item-weight"
)[0].parentNode.parentNode;
if (!should_create.checked) {
asset_name_parent.style.display = "none";
item_owner_parent.style.display = "none";
dimensions_parent.style.display = "none";
model_parent.style.display = "none";
country_of_origin_parent.style.display = "none";
weight_parent.style.display = "none";
}
should_create.onchange = function() {
if (should_create.checked) {
asset_name_parent.style.display = "block";
item_owner_parent.style.display = "block";
model_parent.style.display = "block";
} else {
asset_name_parent.style.display = "none";
item_owner_parent.style.display = "none";
model_parent.style.display = "none";
}
};
}
window.onload = function() {
/* Make sure this will load only in the Purchase and Shipping forms */
let current_path = window.location.pathname;
const allowed_paths = [
"/maniphest/task/edit/form/4/",
"/maniphest/task/edit/form/7/"
];
if (allowed_paths.indexOf(current_path) == -1) {
return;
}
if (current_path == "/maniphest/task/edit/form/7/") {
shipping_validations();
} else {
purchasing_validations();
}
};
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