Skip to content

Commit

Permalink
feat: course id fields restriction & js uncaught element bug fixed (#46)
Browse files Browse the repository at this point in the history
* feat: course id fields restriction & js bug fixed

* refactor: whitespace error fixed
  • Loading branch information
julianramirez2 authored Oct 10, 2023
1 parent eedf105 commit 9551c99
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
48 changes: 48 additions & 0 deletions admin/js/course-id-restriction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var originalValue = "";


var inputElementForm = document.getElementById("openedx_enrollment_course_id");
var inputElementProduct = document.getElementById("_course_id");
var inputElement = null;

if (inputElementForm !== null) {
inputElement = inputElementForm;
} else if (inputElementProduct !== null) {
inputElement = inputElementProduct;
}

if (inputElement !== null) {

inputElement.addEventListener("input", function() {
updateCourseId(this);
});


inputElement.addEventListener("focus", function() {
saveOriginalValue(this);
});


inputElement.addEventListener("blur", function() {
restoreOriginalValue(this);
});

function saveOriginalValue(input) {
originalValue = input.value;
}

function restoreOriginalValue(input) {
if (input.value !== originalValue && !input.value.startsWith("course-v1:")) {
input.value = originalValue;
}
}

function updateCourseId(input) {
var prefix = "course-v1:";
var currentValue = input.value;

if (currentValue !== "" && !currentValue.startsWith(prefix)) {
input.value = prefix;
}
}
}
8 changes: 6 additions & 2 deletions admin/js/product-type.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
document.addEventListener('DOMContentLoaded', function() {
var checkbox = document.getElementById("is_openedx_course");
handleCheckboxChange(checkbox);
if (checkbox !== null) {
handleCheckboxChange(checkbox);
}
});

document.addEventListener('change', function() {
var checkbox = document.getElementById("is_openedx_course");
handleCheckboxChange(checkbox);
if (checkbox !== null) {
handleCheckboxChange(checkbox);
}
});

function handleCheckboxChange(checkbox) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function render_enrollment_info_form( $post ) {
<tr>
<td class="first"><label for="openedx_enrollment_course_id">Course ID</label></td>
<td>
<input type="text" id="openedx_enrollment_course_id" name="enrollment_course_id" value="<?php echo esc_attr( $course_id ); ?>" oninput="updateCourseId(this)" onfocus="saveOriginalValue(this)" onblur="restoreOriginalValue(this)"
<input type="text" id="openedx_enrollment_course_id" name="enrollment_course_id" value="<?php echo esc_attr( $course_id ); ?>"
<?php
if ( '' !== $course_id ) {
?>
Expand Down Expand Up @@ -225,26 +225,7 @@ public function render_enrollment_info_form( $post ) {
</div>

<script>
var originalValue = "";

function saveOriginalValue(input) {
originalValue = input.value;
}

function restoreOriginalValue(input) {
if (input.value !== originalValue && !input.value.startsWith("course-v1:")) {
input.value = originalValue;
}
}

function updateCourseId(input) {
var prefix = "course-v1:";
var currentValue = input.value;

if (currentValue !== "" && !currentValue.startsWith(prefix)) {
input.value = prefix;
}
}

</script>

<?php
Expand Down
3 changes: 3 additions & 0 deletions includes/class-openedx-woocommerce-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ private function define_public_hooks() {
private function define_enqueue_scripts() {
wp_register_script( 'product-type-script', plugin_dir_url( __FILE__ ) . '../admin/js/product-type.js', array(), $this->get_version(), true );
wp_enqueue_script( 'product-type-script' );

wp_register_script( 'course-id-restriction-script', plugin_dir_url( __FILE__ ) . '../admin/js/course-id-restriction.js', array(), $this->get_version(), true );
wp_enqueue_script( 'course-id-restriction-script' );
}

/**
Expand Down

0 comments on commit 9551c99

Please sign in to comment.