working on 20Q drag n drop
This commit is contained in:
@@ -16,6 +16,11 @@ export class TwentyQuestionsDialog extends FormApplication {
|
|||||||
*/
|
*/
|
||||||
errors = {};
|
errors = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache for items (techniques, adv...)
|
||||||
|
*/
|
||||||
|
cache = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assign the default options
|
* Assign the default options
|
||||||
* @override
|
* @override
|
||||||
@@ -43,6 +48,7 @@ export class TwentyQuestionsDialog extends FormApplication {
|
|||||||
this.actor = actor;
|
this.actor = actor;
|
||||||
this.object = new TwentyQuestions(actor);
|
this.object = new TwentyQuestions(actor);
|
||||||
this.errors = this.object.validateForm();
|
this.errors = this.object.validateForm();
|
||||||
|
this._constructCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,6 +96,7 @@ export class TwentyQuestionsDialog extends FormApplication {
|
|||||||
data: this.object.data,
|
data: this.object.data,
|
||||||
errors: this.errors,
|
errors: this.errors,
|
||||||
hasErrors: Object.keys(this.errors).length > 0,
|
hasErrors: Object.keys(this.errors).length > 0,
|
||||||
|
cache: this.cache,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,6 +130,14 @@ export class TwentyQuestionsDialog extends FormApplication {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete a dnd element
|
||||||
|
html.find(`.property-delete`).on("click", (event) => {
|
||||||
|
const stepName = $(event.currentTarget).parents(".tq-drag-n-drop")[0].name;
|
||||||
|
const itemId = $(event.currentTarget).parents(".property").data("propertyId");
|
||||||
|
this._deleteOwnedItem(stepName, itemId);
|
||||||
|
this.render(false);
|
||||||
|
});
|
||||||
|
|
||||||
// Submit button
|
// Submit button
|
||||||
html.find("#generate").on("click", async (event) => {
|
html.find("#generate").on("click", async (event) => {
|
||||||
this.object.toActor(this.actor);
|
this.object.toActor(this.actor);
|
||||||
@@ -134,10 +149,13 @@ export class TwentyQuestionsDialog extends FormApplication {
|
|||||||
* Handle dropped items
|
* Handle dropped items
|
||||||
*/
|
*/
|
||||||
_onDropItem(type, event) {
|
_onDropItem(type, event) {
|
||||||
console.log("*** _onDrop event", event, type);
|
|
||||||
if (!["item", "technique", "peculiarity"].includes(type)) {
|
if (!["item", "technique", "peculiarity"].includes(type)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (event.target.name === "undefined") {
|
||||||
|
console.log("event.target.name is undefined");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Try to extract the data
|
// Try to extract the data
|
||||||
// {type: "Item", id: "pC37smMSCqu3aSRM"}
|
// {type: "Item", id: "pC37smMSCqu3aSRM"}
|
||||||
@@ -147,15 +165,18 @@ export class TwentyQuestionsDialog extends FormApplication {
|
|||||||
if (data.type !== "Item") {
|
if (data.type !== "Item") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Get item
|
||||||
const item = game.items.get(data.id);
|
const item = game.items.get(data.id);
|
||||||
if (item || item.data.type !== type) {
|
if (!item || item.data.type !== type) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Add the item (step and cache)
|
||||||
|
this._addOwnedItem(item, event.target.name);
|
||||||
|
|
||||||
// TODO
|
// TODO specific event (no added honor if tech selected etc)
|
||||||
console.log("** OK ", item);
|
console.log(this.object.data.step3.techniques, this.cache);
|
||||||
// sub_type === 'peculiarity'
|
|
||||||
|
this.render(false);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
console.warn(err);
|
||||||
}
|
}
|
||||||
@@ -188,4 +209,66 @@ export class TwentyQuestionsDialog extends FormApplication {
|
|||||||
}
|
}
|
||||||
this.render(false);
|
this.render(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct the cache tree with Items full object
|
||||||
|
*/
|
||||||
|
_constructCache() {
|
||||||
|
this.cache = {};
|
||||||
|
TwentyQuestions.itemsList.forEach((stepName) => {
|
||||||
|
// Check if current step value is a array
|
||||||
|
const store = getProperty(this.object.data, stepName);
|
||||||
|
if (!store || !Array.isArray(store)) {
|
||||||
|
setProperty(this.object.data, stepName, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init cache if not exist
|
||||||
|
if (!hasProperty(this.cache, stepName)) {
|
||||||
|
setProperty(this.cache, stepName, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get linked Item, and store it in cache
|
||||||
|
const step = getProperty(this.object.data, stepName);
|
||||||
|
step.forEach((id, idx) => {
|
||||||
|
const item = game.items.get(id);
|
||||||
|
if (!item) {
|
||||||
|
step.splice(idx, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
getProperty(this.cache, stepName).push(item);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a owned item reference in step and cache
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_addOwnedItem(item, stepName) {
|
||||||
|
// Add to Step (uniq id only)
|
||||||
|
const step = getProperty(this.object.data, stepName);
|
||||||
|
if (step.some((e) => e === item.id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
step.push(item.id);
|
||||||
|
|
||||||
|
// Add to cache
|
||||||
|
getProperty(this.cache, stepName).push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a owned item reference in step and cache
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_deleteOwnedItem(stepName, itemId) {
|
||||||
|
// Delete from current step
|
||||||
|
let step = getProperty(this.object.data, stepName);
|
||||||
|
step = step.filter((e) => e !== itemId);
|
||||||
|
setProperty(this.object.data, stepName, step);
|
||||||
|
|
||||||
|
// Delete from cache
|
||||||
|
let cache = getProperty(this.cache, stepName);
|
||||||
|
cache = cache.filter((e) => e.id !== itemId);
|
||||||
|
setProperty(this.cache, stepName, cache);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ export class TwentyQuestions {
|
|||||||
"step17.skill",
|
"step17.skill",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut for all Items to build cache
|
||||||
|
*/
|
||||||
|
static itemsList = ["step3.techniques"];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Steps datas
|
* Steps datas
|
||||||
*/
|
*/
|
||||||
@@ -62,9 +67,9 @@ export class TwentyQuestions {
|
|||||||
maho: false,
|
maho: false,
|
||||||
ninjutsu: false,
|
ninjutsu: false,
|
||||||
},
|
},
|
||||||
techniques: "",
|
techniques: [],
|
||||||
school_ability: "",
|
school_ability: [],
|
||||||
equipment: "",
|
equipment: [],
|
||||||
social_honor: 0,
|
social_honor: 0,
|
||||||
},
|
},
|
||||||
step4: {
|
step4: {
|
||||||
|
|||||||
@@ -236,17 +236,19 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label>
|
|
||||||
{{localize 'l5r5e.twenty_questions.startech'}}
|
{{localize 'l5r5e.twenty_questions.startech'}}
|
||||||
<textarea name="step3.techniques">{{data.step3.techniques}}</textarea>
|
<fieldset name="step3.techniques" class="tq-drag-n-drop">
|
||||||
</label>
|
<ul class="item-list">
|
||||||
<!-- TODO Drag n drop techniques-->
|
{{#each cache.step3.techniques as |item id|}}
|
||||||
<div class="techniques 20q-droppable">
|
{{> 'systems/l5r5e/templates/items/property/property-entry.html' item=item id=id }}
|
||||||
<span class="techniques 20q-droppable-text">
|
{{/each}}
|
||||||
drop item here
|
</ul>
|
||||||
</span>
|
</fieldset>
|
||||||
{{data.step3.techniques}}
|
|
||||||
</div>
|
<fieldset class="techniques" name="step3.techniques" style="min-height: 50px">
|
||||||
|
<legend class="section-header"><i class="fa fa-arrow-down" aria-hidden="true"></i> {{ localize 'l5r5e.drophere' }}</legend>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user