Skip to content
This repository has been archived by the owner on Oct 8, 2022. It is now read-only.

Commit

Permalink
Issue #133 fixed update_field function in general.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lnewmanheggie committed Apr 12, 2021
1 parent dc2c057 commit ec9dbb7
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 49 deletions.
30 changes: 22 additions & 8 deletions src/application/helpers/interface_helper.php
Expand Up @@ -215,11 +215,8 @@ function create_edit_field(string $field_name, ?string $value, ?string $label, $
$id = $options ['id'];
}
/* The id is split with the "-" delimiter in javascript when the field is clicked */
$output[] = format_string('<@envelope class="field-envelope" id="@table__@field_name__@id">', [
$output[] = format_string('<@envelope class="field-envelope">', [
'@envelope' => $envelope,
'@table' => $table,
'@field_name' => $field_name,
'@id' => $id,
]);
if ($label != "") {
$output [] = format_string('<label>@label:&nbsp;</label>', ['@label' => $label]);
Expand All @@ -242,8 +239,18 @@ function create_edit_field(string $field_name, ?string $value, ?string $label, $
$format = sprintf("format='%s'", $options ["format"]);
}
$data_attributes = '';

$primary_data_items = [
'id' => $id,
'table' => $table,
'field' => $field_name,
];
if(empty($options['data'])){
$options['data'] = [];
}
$options['data'] = array_merge($options['data'], $primary_data_items);
if (array_key_exists('data', $options)) {
$data_items = [];

$data = $options['data'];
if (!is_array($data)) {
$data = [$data];
Expand All @@ -253,6 +260,7 @@ function create_edit_field(string $field_name, ?string $value, ?string $label, $
}
$data_attributes = implode(" ", $data_items);
}

$title = '';
if (array_key_exists('title', $options)) {
//$title = format_string(' title="@title" ', ['@title' => $options['title']]);
Expand Down Expand Up @@ -289,9 +297,12 @@ function theme_edit_field($data): string {
'value',
];
ksort($data);
if (!empty(array_diff($required_keys, array_keys($data)))) {
return '';
}
try {
empty(array_diff($required_keys, array_keys($data)));
} catch (Exception $e) {
print 'Caught exception: ' . $e->getMessage() . "\n";
};

$field = NULL;
$id = NULL;
$table = NULL;
Expand Down Expand Up @@ -373,6 +384,9 @@ function live_field(string $field_name, ?string $value, string $table, string $i
$input = form_input($field_name, $value, $attributes);
$output = str_replace('[input]', $input, $output_wrapper);
}
} else {
$input = form_input($field_name, $value, $attributes);
$output = str_replace('[input]', $input, $output_wrapper);
}
}
else {
Expand Down
44 changes: 30 additions & 14 deletions src/application/views/category/list.php
@@ -1,15 +1,15 @@
<?php
$buttons[] = [
'text' => 'New Category',
'class' => [
'new',
'button',
'dialog',
'create'
], 'style' => 'new',
'href' => site_url('category/create')
];
print create_button_bar($buttons);
$buttons[] = [
'text' => 'New Category',
'class' => [
'new',
'button',
'dialog',
'create'
], 'style' => 'new',
'href' => site_url('category/create')
];
print create_button_bar($buttons);
?>

<ul class="field-list list">
Expand All @@ -18,9 +18,25 @@
<?php print live_field('category', $category->category, 'category', $category->id, [
'size' => '200',
'envelope' => 'span'
]); ?>
]);
?>
<?php $data = [
'field' => 'category',
'id' => $category->id,
'table' => 'category',
'value' => $category->category,
'options' => [
'size' => '200',
'envelope' => 'span',
'data' => [
'type' => 'dropdown',
],
],
];
print theme_edit_field($data);
?>
<?php print create_button([
'text' => 'Add Subcategory',
'text' => 'Add Subcategory to ' . $category->category,
'class' => 'new button dialog create small',
'href' => site_url('subcategory/create/' . $category->id)
]); ?>
Expand All @@ -30,7 +46,7 @@
<li class="list-item">
<?php print live_field('subcategory', $subcategory->subcategory, 'subcategory', $subcategory->id, [
'size' => "auto",
'envelope' => 'span'
'envelope' => 'span',
]); ?>
</li>
<?php endforeach; ?>
Expand Down
104 changes: 77 additions & 27 deletions src/js/general.js
Expand Up @@ -140,8 +140,7 @@
let form_data;
if ($("body").hasClass("editor")) {
me = $(this);
my_parent = me.parent().attr("id");
my_attr = my_parent.split("__");
my_parent = me.parent();
my_type = "text";
my_category = me.attr('menu');

Expand Down Expand Up @@ -169,9 +168,9 @@
my_type = "pot-size";
}
form_data = {
table: my_attr[0],
field: my_name,
id: my_attr[2],
table: $(this).data('table'),
field: $(this).data('field'),
id: $(this).data('id'),
type: my_type,
category: my_category,
value: me.html()
Expand All @@ -181,10 +180,10 @@
url: base_url + "menu/edit_value",
data: form_data,
success: function (data) {
console.log(data);
$("#" + my_parent + " .edit-field").html(data).removeClass("edit-field").removeClass("field").addClass("live-field").addClass("text");
$("#" + my_parent + " .live-field input").focus();

// console.log(data);
// console.log(my_parent.children());
my_parent.children(".edit-field").html(data).removeClass("edit-field").removeClass("field").addClass("live-field").addClass("text");
//$("#" + my_parent + " .live-field input").focus();
}
});
}
Expand Down Expand Up @@ -485,16 +484,11 @@ function show_popup(my_title, data, popup_width, x, y) {
}

function update_field(me, my_type) {
let my_parent = $(me).parents(".field-envelope").attr("id");
let my_attr = my_parent.split("__");
let my_value = $("#" + my_parent).children(".live-field").children("input" | "textarea").val();
console.log(my_value);
let my_parent = $(me).parent();
let my_value = $(me).val();

let my_category = false;
if (my_type === "autocomplete") {
my_value = $("#" + my_parent).children(".live-field").children("input").val();
} else if (my_type === "multiselect") {
my_value = $("#" + my_parent).children(".multiselect").children("select").val();
} else if (my_type === "category-dropdown") {
if (my_type === "category-dropdown") {
my_category = "category";
} else if (my_type === "subcategory-dropdown") {
my_category = "subcategory";
Expand All @@ -512,19 +506,18 @@ function update_field(me, my_type) {
my_value = 'no';
}
}
let is_persistent = $(me).hasClass("persistent");
let is_persistent = my_parent.hasClass("persistent");

//don't do anything if the value is empty and it is a persistent field
if (is_persistent && my_value === "") {
return false;
}

let override = $(me).hasClass("override");

let override = my_parent.hasClass("override");
let form_data = {
table: my_attr[0],
field: my_attr[1],
id: my_attr[2],
table: my_parent.data('table'),
field: my_parent.data('field'),
id: my_parent.data('id'),
value: my_value,
override: override,
category: my_category,
Expand All @@ -533,17 +526,74 @@ function update_field(me, my_type) {

$.ajax({
type: "post",
url: base_url + my_attr[0] + "/update_value",
url: base_url + my_parent.data('table') + "/update_value",
data: form_data,
success: function (data) {
console.log(data);
if (!is_persistent) {
$("#" + my_parent + " .live-field").html(data);
$("#" + my_parent + " .live-field").addClass("edit-field field").removeClass("live-field text");
$(my_parent).html(data)
$(my_parent).removeClass("live-field text");
$(my_parent).addClass("edit-field field");
}
}
});
}

// let my_parent = $(me).parents(".field-envelope").attr("id");
// let my_value = $("#" + my_parent).children(".live-field").children("input" | "textarea").val();
// let my_category = false;
// if (my_type === "autocomplete") {
// my_value = $("#" + my_parent).children(".live-field").children("input").val();
// } else if (my_type === "multiselect") {
// my_value = $("#" + my_parent).children(".multiselect").children("select").val();
// } else if (my_type === "category-dropdown") {
// my_category = "category";
// } else if (my_type === "subcategory-dropdown") {
// my_category = "subcategory";
// } else if (my_type === "checkbox") {
// my_category = "checkbox";
// if ($(me).attr("checked") === true) {
// my_value = 1;
// } else {
// my_value = 0;
// }
// } else if(my_type === 'boolean'){
// if ($(me).attr("checked") === true) {
// my_value = 'yes';
// } else {
// my_value = 'no';
// }
// }
// let is_persistent = $(me).hasClass("persistent");

// //don't do anything if the value is empty and it is a persistent field
// if (is_persistent && my_value === "") {
// return false;
// }

// let override = $(me).hasClass("override");
// let form_data = {
// table: $(me).data('table'),
// field: $(me).data('field'),
// id: $(me).data('id'),
// value: my_value,
// override: override,
// category: my_category,
// type: my_type,
// };

// $.ajax({
// type: "post",
// url: base_url + $(me).data('table') + "/update_value",
// data: form_data,
// success: function (data) {
// console.log(data);
// if (!is_persistent) {
// $("#" + my_parent + " .live-field").html(data);
// $("#" + my_parent + " .live-field").addClass("edit-field field").removeClass("live-field text");
// }
// }
// });


function create_dropdown(my_field, my_category, my_value) {
Expand Down

0 comments on commit ec9dbb7

Please sign in to comment.