HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/www.winghung.com/wp-content/plugins/chatbot/js/jquery.repeatable.js
(function ($) {



	$.fn.repeatable = function (userSettings) {



		/**

		 * Default settings

		 * @type {Object}

		 */

		var defaults = {

			addTrigger: ".add",

			deleteTrigger: ".delete",

			max: null,

			startWith: 0,

			template: null,

			itemContainer: ".field-group",

			onAdd: function () {},

			onDelete: function () {}

		};



		/**

		 * Iterator used to make each added

		 * repeatable element unique

		 * @type {Number}

		 */

		var i = 0;

		

		/**

		 * DOM element into which repeatable

		 * items will be added

		 * @type {jQuery object}

		 */

		var target = $(this);

			

		/**

		 * Blend passed user settings with defauly settings

		 * @type {array}

		 */

		var settings = $.extend({}, defaults, userSettings);

		

		/**

		 * Total templated items found on the page

		 * at load. These may be created by server-side

		 * scripts.

		 * @return null

		 */

		var total = function () {

			return $(target).find(settings.itemContainer).length;

		}();



		

		/**

		 * Add an element to the target

		 * and call the callback function

		 * @return null

		 */

		var addOne = function () {

			createOne();

			settings.onAdd.call(this);

		};



		/**

		 * Delete the parent element

		 * and call the callback function

		 * @return null

		 */

		var deleteOne = function () {

			$(this).parents(settings.itemContainer).first().remove();

			total--;

			maintainAddBtn();

			settings.onDelete.call(this);

		};



		/**

		 * Add an element to the target

		 * @return null

		 */

		var createOne = function() {

			getUniqueTemplate().appendTo(target);

			total++;

			maintainAddBtn();

		};



		/**

		 * Alter the given template to make

		 * each form field name unique

		 * @return {jQuery object}

		 */

		var getUniqueTemplate = function () {

			var template = $(settings.template).html();

			template = template.replace(/{\?}/g, "new" + i++); 	// {?} => iterated placeholder

			template = template.replace(/\{[^\?\}]*\}/g, ""); 	// {valuePlaceholder} => ""

			return $(template);

		};



		/**

		 * Determines if the add trigger

		 * needs to be disabled

		 * @return null

		 */

		var maintainAddBtn = function () {

			if (!settings.max) {

				return;

			}



			if (total === settings.max) {

				$(settings.addTrigger).attr("disabled", "disabled");

			} else if (total < settings.max) {

				$(settings.addTrigger).removeAttr("disabled");

			}

		};



		/**

		 * Setup the repeater

		 * @return null

		 */

		(function () {

			$(settings.addTrigger).on("click", addOne);

			$("form").on("click", ".delete", deleteOne);



			if (!total) {

				var toCreate = settings.startWith - total;

				for (var j = 0; j < toCreate; j++) {

					createOne();

				}

			}

			

		})();

	};



})(jQuery);