Skip to content

Commit

Permalink
Merge pull request #146 from yellowtree/autosave
Browse files Browse the repository at this point in the history
Implement AJAX Override Autosave (Ref #94 #140)
  • Loading branch information
benjaminpick authored Sep 22, 2021
2 parents 8382d57 + 846c00a commit e8d3c10
Show file tree
Hide file tree
Showing 37 changed files with 5,301 additions and 3,479 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ wp-cli.local.yml
/.vscode
*.code
/coverage
/.parcel-cache

/lib/vendor

Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ install:

script:
- composer test
- yarn test
- yarn test
- yarn build
23 changes: 1 addition & 22 deletions ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,13 @@ function _geoip_detect2_enqueue_javascript() {
return true;
}

function _geoip_detect_parcel_get_dist_js($handle) {
$urlFile = GEOIP_PLUGIN_DIR . '/js/dist/parcel.json';
if (!is_readable($urlFile)) return false;

$json = file_get_contents($urlFile);
$urls = json_decode($json, true);

if (isset($urls[$handle]))
return '/js/dist/' .$urls[$handle];
return false;
}

function _geoip_detect_register_javascript() {
// What about CORS usage?
// if (!get_option('geoip-detect-ajax_enabled')) {
// return;
// }

$file_uri = _geoip_detect_parcel_get_dist_js('frontendJS');
if (!$file_uri) {
if (WP_DEBUG) {
trigger_error('Warning by the geoip-detect-Plugin: the file frontend.js could not be found, JS API will not work.', E_USER_NOTICE);
die();
}
return;
}

wp_register_script('geoip-detect-js', GEOIP_DETECT_PLUGIN_URI . $file_uri, array(), GEOIP_DETECT_VERSION, true);
wp_register_script('geoip-detect-js', GEOIP_DETECT_PLUGIN_URI . 'js/dist/frontend.js', array(), GEOIP_DETECT_VERSION, true);
$data = [
'ajaxurl' => admin_url('/admin-ajax.php'),
'default_locales' => apply_filters('geoip_detect2_locales', null),
Expand Down
4 changes: 0 additions & 4 deletions babel.config.js

This file was deleted.

9 changes: 9 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
"transform-class-properties",
"@babel/plugin-transform-runtime"
],
"presets": [
"@babel/preset-env"
]
}
19 changes: 16 additions & 3 deletions js/body_classes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { domReady } from './lib/html';
import { get_info } from './lookup';
import { get_info } from './lookup/get_info';

export function calc_classes(record) {
return {
Expand All @@ -10,6 +10,11 @@ export function calc_classes(record) {
};
}

function remove_css_classes_by_prefix(el, prefix) {
const classes = el.className.split(" ").filter(c => !c.startsWith(prefix));
el.className = classes.join(" ").trim();
}

export async function add_body_classes() {
const record = await get_info();

Expand All @@ -18,11 +23,19 @@ export async function add_body_classes() {
return;
}

const css_classes = calc_classes(record);

await domReady;

add_classes_to_body(record);
}

export function add_classes_to_body(record) {
const css_classes = calc_classes(record);

const body = document.getElementsByTagName('body')[0];

// Remove old classes in case there are any
remove_css_classes_by_prefix(body, 'geoip-');

for (let key of Object.keys(css_classes)) {
const value = css_classes[key];
if (value) {
Expand Down
18 changes: 17 additions & 1 deletion js/body_classes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*/

import { getTestRecord, getTestRecordError } from "./test-lib/test-records";
import { calc_classes } from "./body_classes";
import { add_classes_to_body, calc_classes } from "./body_classes";
import Record from "./models/record";
import { set_override_with_merge, get_info_stored_locally_record } from "./lookup/override";

const emptyRecord = new Record();
const defaultRecord = getTestRecord();
Expand All @@ -29,5 +30,20 @@ test('calc_classes', () => {
"country-is-in-european-union": false,
"province": "",
});
});

test('css_classes', () => {
const body = document.getElementsByTagName('body')[0];

add_classes_to_body(defaultRecord);

expect(body.classList.contains('geoip-country-DE')).toBe(true);
expect(body.classList.contains('geoip-country-FR')).toBe(false);

set_override_with_merge('country.iso_code', 'FR');
let record = get_info_stored_locally_record();
add_classes_to_body(record);

expect(body.classList.contains('geoip-country-FR')).toBe(true);
expect(body.classList.contains('geoip-country-DE')).toBe(false);
});
4 changes: 0 additions & 4 deletions js/dist/backend.f84e1103.js

This file was deleted.

1 change: 0 additions & 1 deletion js/dist/backend.f84e1103.js.map

This file was deleted.

41 changes: 0 additions & 41 deletions js/dist/frontend.fb1bdc4e.js

This file was deleted.

1 change: 0 additions & 1 deletion js/dist/frontend.fb1bdc4e.js.map

This file was deleted.

Loading

0 comments on commit e8d3c10

Please sign in to comment.