From ac39ecec1efc4e37020f38cc2b0ee47af649d874 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Fri, 8 Sep 2023 10:45:22 +0200 Subject: [PATCH] Refactor: Remove old js load more button code --- source/php/Module/Posts/Posts.php | 1 - .../assets/mod-posts-load-more-button.js | 125 ------------------ webpack.config.js | 1 - 3 files changed, 127 deletions(-) delete mode 100644 source/php/Module/Posts/assets/mod-posts-load-more-button.js diff --git a/source/php/Module/Posts/Posts.php b/source/php/Module/Posts/Posts.php index 9d53c6ef9..87bcdfbb3 100644 --- a/source/php/Module/Posts/Posts.php +++ b/source/php/Module/Posts/Posts.php @@ -24,7 +24,6 @@ public function init() $this->namePlural = __('Posts', 'modularity'); $this->description = __('Outputs selected posts in specified layout', 'modularity'); - add_action('Modularity/Module/' . $this->moduleSlug . '/enqueue', array($this, 'enqueueScripts')); add_action('add_meta_boxes', array($this, 'addColumnFields')); add_action('save_post', array($this, 'saveColumnFields')); diff --git a/source/php/Module/Posts/assets/mod-posts-load-more-button.js b/source/php/Module/Posts/assets/mod-posts-load-more-button.js deleted file mode 100644 index 17ddbc866..000000000 --- a/source/php/Module/Posts/assets/mod-posts-load-more-button.js +++ /dev/null @@ -1,125 +0,0 @@ -var Modularity = Modularity || {}; -Modularity.Posts = Modularity.Posts || {}; - -Modularity.Posts.LoadMoreButton = (function ($) { - function LoadMoreButton() { - this.init(); - } - - LoadMoreButton.prototype.init = function() { - $(document).on('click', '.js-mod-posts-load-more', function(e) { - var button = $(e.target).closest('.js-mod-posts-load-more'); - - var attributes = JSON.parse(window.atob(button.attr('data-mod-posts-load-more'))); - //Make sure required attributes exists - var requiredKeys = ['postsPerPage', 'offset', 'target', 'ajaxUrl', 'nonce', 'bladeTemplate']; - if (!this.attributesExists(requiredKeys, Object.keys(attributes))) { - return; - } - - //Make sure target exists - var target = $(attributes.target)[0]; - if (typeof(target) === 'undefined') { - throw 'Error: Could not find target "' + attributes.target + '"'; - return; - } - - this.toggleLoader(button); - this.loadMorePosts(button, target, attributes); - - }.bind(this)); - }; - - LoadMoreButton.prototype.toggleLoader = function(button) - { - if (button.hasClass('hidden')) { - button.removeClass('hidden'); - - this.removeLoader(button); - - return; - } - - button.addClass('hidden'); - - //Create loader - button.after('
'); - }; - - /** - * Load more posts - * @param button - * @param target - * @param attributes - * @param rawdata - */ - LoadMoreButton.prototype.loadMorePosts = function(button, target, attributes) - { - - var data = attributes; - data.action = 'mod_posts_load_more'; - - $.ajax({ - type : "post", - url : data.ajaxUrl, - data : data, - dataType: "html", - success : function(posts, status) { - - if (status === 'success') { - - //Append posts - $(target).append(JSON.parse(posts)); - - //Remove button if number of posts is less then queried post count - if (attributes.postsPerPage > posts.length) { - this.removeLoader(button); - button.remove(); - return; - } - - this.toggleLoader(button); - - //Increment offset - attributes.offset = parseInt(attributes.offset) + parseInt(attributes.postsPerPage); - button.attr('data-mod-posts-load-more', window.btoa(JSON.stringify(attributes))); - return; - } - - if (status === 'nocontent') { - this.removeLoader(button); - button.after('

No more posts to show…

'); - button.remove(); - return; - } - - }.bind(this), - error : function(jqXHR, status, error) { - console.log(error); - } - }); - } - - LoadMoreButton.prototype.removeLoader = function(button) { - if ($(button.next()).hasClass('loading')) { - button.next().remove(); - } - }; - - LoadMoreButton.prototype.attributesExists = function(requiredKeys, attributeKeys) { - var exists = true; - - requiredKeys.forEach(function(key) { - if (!attributeKeys.includes(key)) { - exists = false; - - throw 'ValidationError: Missing required data-attribute key "' + key + '"'; - } - }); - - return exists; - }; - - return new LoadMoreButton(); - -})(jQuery); diff --git a/webpack.config.js b/webpack.config.js index ca86fce02..ace85d61b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -27,7 +27,6 @@ module.exports = { //Modules 'js/mod-posts-taxonomy': './source/php/Module/Posts/assets/mod-posts-taxonomy.js', - 'js/mod-posts-load-more-button': './source/php/Module/Posts/assets/mod-posts-load-more-button.js', 'js/mod-curator-load-more': './source/php/Module/Curator/assets/mod-curator-load-more.js', 'js/table-init': './source/php/Module/Table/assets/table-init.js', 'css/table': './source/php/Module/Table/assets/table.scss',