All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.webfirmframework.wffweb.server.page.js.wffClientCRUDUtil.js Maven / Gradle / Ivy

There is a newer version: 12.0.1
Show newest version
/**
 * 
 */

var wffClientCRUDUtil = new function() {

	var encoder = wffGlobal.encoder;
	var decoder = wffGlobal.decoder;

	var getStringFromBytes = function(utf8Bytes) {
		return decoder.decode(new Uint8Array(utf8Bytes));
	};

	this.applyUpdates = function(wffBMBytes) {
		console.log('applying updates', document.getElementsByTagName('*'));

		var nameValues = wffBMUtil.parseWffBinaryMessageBytes(wffBMBytes);

		var taskNameValue = nameValues[0];
		// var taskName = getStringFromBytes(taskNameValue.name);
		var taskValue = taskNameValue.values[0][0];
		console.log('taskNameValue.name', taskNameValue.name);

		if (taskNameValue.name[0] != wffGlobal.taskValues.TASK) {
			return;
		}
		if (taskValue == wffGlobal.taskValues.ATTRIBUTE_UPDATED) {
			console.log('taskValue == "ATTRIBUTE_UPDATED"');

			for (var i = 1; i < nameValues.length; i++) {

				console.log('i', i);
				console.log('nameValues[i].values', nameValues[i].values);

				var attrNameValue = getStringFromBytes(nameValues[i].name);
				console.log('attrNameValue', attrNameValue);
				var indexOfSeparator = attrNameValue.indexOf('=');

				var attrNameValueArry = wffTagUtil
						.splitAttrNameValue(attrNameValue);
				var attrName = attrNameValueArry[0];
				var attrValue = attrNameValueArry[1];

				// if (indexOfSeparator != -1) {
				// attrName = attrNameValue.substring(0, indexOfSeparator);
				// attrValue = attrNameValue.substring(indexOfSeparator + 1,
				// attrNameValue.length);
				// } else {
				// attrName = attrNameValue;
				// attrValue = '';
				// }

				console.log('attrName', attrName, 'attrValue', attrValue);

				// var tagId = wffBMUtil
				// .getIntFromOptimizedBytes(nameValues[i].name);
				var wffIds = nameValues[i].values;

				// var tagName = getStringFromBytes(wffIds[0]);

				for (var j = 0; j < wffIds.length; j++) {
					console.log('j', j);

					var wffId = wffTagUtil.getWffIdFromWffIdBytes(wffIds[j]);

					var applicableTag = wffTagUtil.getTagByWffId(wffId);

					if (indexOfSeparator != -1) {
						applicableTag.setAttribute(attrName, attrValue);
					} else {
						applicableTag.setAttribute(attrName, "");
					}
				}

			}

		} else if (taskValue == wffGlobal.taskValues.REMOVED_ATTRIBUTE) {
			console.log('taskValue == "REMOVED_ATTRIBUTE"');

			for (var i = 1; i < nameValues.length; i++) {

				console.log('i', i);
				console.log('nameValues[i].values', nameValues[i].values);

				var attrName = getStringFromBytes(nameValues[i].name);

				console.log('attrName', attrName);

				// var tagId = wffBMUtil
				// .getIntFromOptimizedBytes(nameValues[i].name);
				var wffIds = nameValues[i].values;

				// var tagName = getStringFromBytes(wffIds[0]);

				for (var j = 0; j < wffIds.length; j++) {
					console.log('j', j);

					var wffId = wffTagUtil.getWffIdFromWffIdBytes(wffIds[j]);

					var applicableTag = wffTagUtil.getTagByWffId(wffId);

					applicableTag.removeAttribute(attrName);

				}

			}

		} else if (taskValue == wffGlobal.taskValues.APPENDED_CHILD_TAG
				|| taskValue == wffGlobal.taskValues.APPENDED_CHILDREN_TAGS) {

			// TODO comment this later
			if (taskValue == wffGlobal.taskValues.APPENDED_CHILD_TAG) {
				console.log('APPENDED_CHILD_TAG');
			} else if (taskValue == wffGlobal.taskValues.APPENDED_CHILDREN_TAGS) {
				console.log('APPENDED_CHILDREN_TAGS');
			}
			// end

			for (var i = 1; i < nameValues.length; i++) {
				var wffId = wffTagUtil
						.getWffIdFromWffIdBytes(nameValues[i].name);
				var values = nameValues[i].values;
				var tagName = getStringFromBytes(values[0]);

				var parent = wffTagUtil.getTagByTagNameAndWffId(tagName, wffId);

				for (var j = 1; j < values.length; j++) {
					// var htmlString = getStringFromBytes(values[j]);

					// var div = document.createElement('div');
					// div.innerHTML = htmlString;
					// var htmlNodes = div.firstChild;

					if (parent == undefined) {
						console.log('parent == undefined', 'tagName', tagName,
								'wffId', wffId);
					}

					var htmlNodes = wffTagUtil
							.createTagFromWffBMBytes(values[j]);

					parent.appendChild(htmlNodes);
				}
			}
		} else if (taskValue == wffGlobal.taskValues.REMOVED_TAGS) {

			console.log('wffGlobal.taskValues.REMOVED_TAGS nameValues.length '
					+ nameValues.length);

			for (var i = 1; i < nameValues.length; i++) {
				var wffId = wffTagUtil
						.getWffIdFromWffIdBytes(nameValues[i].name);
				var values = nameValues[i].values;
				var tagName = getStringFromBytes(values[0]);

				var tagToRemove = wffTagUtil.getTagByTagNameAndWffId(tagName,
						wffId);

				console.log('tagToRemove', tagToRemove, 'tagName', tagName,
						'wffId', wffId, 'count i', i);
				var parent = tagToRemove.parentNode;
				parent.removeChild(tagToRemove);

			}

		} else if (taskValue == wffGlobal.taskValues.REMOVED_ALL_CHILDREN_TAGS) {

			console.log('wffGlobal.taskValues.REMOVED_ALL_CHILDREN_TAGS');

			for (var i = 1; i < nameValues.length; i++) {
				var wffId = wffTagUtil
						.getWffIdFromWffIdBytes(nameValues[i].name);
				var values = nameValues[i].values;
				var tagName = getStringFromBytes(values[0]);

				var parentTag = wffTagUtil.getTagByTagNameAndWffId(tagName,
						wffId);
				while (parentTag.firstChild) {
					parentTag.removeChild(parentTag.firstChild);
				}
			}

		} else if (taskValue == wffGlobal.taskValues.MOVED_CHILDREN_TAGS) {

			console
					.log('wffGlobal.taskValues.MOVED_CHILDREN_TAGS nameValues.length '
							+ nameValues.length);

			for (var i = 1; i < nameValues.length; i++) {
				var currentParentWffId = wffTagUtil
						.getWffIdFromWffIdBytes(nameValues[i].name);
				var values = nameValues[i].values;
				var currentParentTagName = getStringFromBytes(values[0]);

				var currentParentTag = wffTagUtil.getTagByTagNameAndWffId(
						currentParentTagName, currentParentWffId);

				var childWffId = wffTagUtil.getWffIdFromWffIdBytes(values[1]);
				var childTagName = getStringFromBytes(values[2]);
				var childTag = wffTagUtil.getTagByTagNameAndWffId(childTagName,
						childWffId);

				if (childTag !== undefined) {
					var previousParent = childTag.parentNode;
					console.log('childTag !== undefined', childTag);
					previousParent.removeChild(childTag);
				} else {
					console.log('childTag === undefined', childTag);
					childTag = wffTagUtil.createTagFromWffBMBytes(values[3]);
				}
				currentParentTag.appendChild(childTag);

			}

		} else if (taskValue == wffGlobal.taskValues.ADDED_ATTRIBUTES) {
			console.log('taskValue == "ADDED_ATTRIBUTES"');

			for (var i = 1; i < nameValues.length; i++) {

				var nameValue = nameValues[i];
				if (nameValue.name[0] == wffGlobal.taskValues.MANY_TO_ONE) {
					var tagName = getStringFromBytes(nameValue.values[0]);
					var wffId = wffTagUtil
							.getWffIdFromWffIdBytes(nameValue.values[1]);

					var applicableTag = wffTagUtil.getTagByTagNameAndWffId(
							tagName, wffId);

					for (var j = 2; j < nameValue.values.length; j++) {

						var attrNameValue = getStringFromBytes(nameValue.values[j]);

						var attrNameValueArry = wffTagUtil
								.splitAttrNameValue(attrNameValue);
						var attrName = attrNameValueArry[0];
						var attrValue = attrNameValueArry[1];
						applicableTag.setAttribute(attrName, attrValue);
					}
				}

			}

		} else if (taskValue == wffGlobal.taskValues.REMOVED_ATTRIBUTES) {
			console.log('taskValue == "REMOVED_ATTRIBUTES"');

			for (var i = 1; i < nameValues.length; i++) {

				var nameValue = nameValues[i];
				if (nameValue.name[0] == wffGlobal.taskValues.MANY_TO_ONE) {
					var tagName = getStringFromBytes(nameValue.values[0]);
					var wffId = wffTagUtil
							.getWffIdFromWffIdBytes(nameValue.values[1]);

					var applicableTag = wffTagUtil.getTagByTagNameAndWffId(
							tagName, wffId);

					for (var j = 2; j < nameValue.values.length; j++) {

						var attrName = getStringFromBytes(nameValue.values[j]);

						applicableTag.removeAttribute(attrName);
					}
				}

			}

		} else if (taskValue == wffGlobal.taskValues.ADDED_INNER_HTML) {

			console.log('wffGlobal.taskValues.ADDED_INNER_HTML');

			for (var i = 1; i < nameValues.length; i++) {
				var wffId = wffTagUtil
						.getWffIdFromWffIdBytes(nameValues[i].name);
				var values = nameValues[i].values;
				var tagName = getStringFromBytes(values[0]);
				// var innerHtml = getStringFromBytes(values[1]);

				// console.log('innerHtml', innerHtml);

				var parentTag = wffTagUtil.getTagByTagNameAndWffId(tagName,
						wffId);
				// it should be case sensitive node.innerHTML
				// parentTag.innerHTML = innerHtml;

				while (parentTag.firstChild) {
					parentTag.removeChild(parentTag.firstChild);
				}

				var htmlNodes = wffTagUtil.createTagFromWffBMBytes(values[1]);
				
				//if length is 3 then there is an existing tag with this id
				if (values.length == 3) {
					console.log('values.length == 3');
					var existingTag = wffTagUtil.getTagByTagNameAndWffId(
							htmlNodes.nodeName, htmlNodes
									.getAttribute("data-wff-id"));
					var parentOfExistingTag = existingTag.parentNode;
					parentOfExistingTag.removeChild(existingTag);
					
				}
				
				parentTag.appendChild(htmlNodes);
			}

		} else if (taskValue == wffGlobal.taskValues.INSERTED_BEFORE_TAG) {

			console.log('wffGlobal.taskValues.INSERTED_BEFORE_TAG');

			for (var i = 1; i < nameValues.length; i++) {
				var wffId = wffTagUtil
						.getWffIdFromWffIdBytes(nameValues[i].name);
				var values = nameValues[i].values;
				var tagName = getStringFromBytes(values[0]);
				// var innerHtml = getStringFromBytes(values[1]);

				// console.log('innerHtml', innerHtml);

				var parentTag = wffTagUtil.getTagByTagNameAndWffId(tagName,
						wffId);
				
				var beforeTagName = getStringFromBytes(values[2]);
				var beforeTagWffId = wffTagUtil.getWffIdFromWffIdBytes(values[3]);
				
				var beforeTag = wffTagUtil.getTagByTagNameAndWffId(beforeTagName,
						beforeTagWffId);
				
				
				var htmlNodes = wffTagUtil.createTagFromWffBMBytes(values[1]);
				
				//if length is 3 then there is an existing tag with this id
				if (values.length == 5) {
					console.log('values.length == 5');
					var existingTag = wffTagUtil.getTagByTagNameAndWffId(
							htmlNodes.nodeName, htmlNodes
									.getAttribute("data-wff-id"));
					var parentOfExistingTag = existingTag.parentNode;
					parentOfExistingTag.removeChild(existingTag);
					
				}
				
				parentTag.insertBefore(htmlNodes, beforeTag);
			}

		} else if (taskValue == wffGlobal.taskValues.RELOAD_BROWSER) {
			location.reload(true);
		} else if (taskValue == wffGlobal.taskValues.RELOAD_BROWSER_FROM_CACHE) {
			location.reload();
		}  else if (taskValue == wffGlobal.taskValues.EXECURE_JS) {
			
			var js = getStringFromBytes(taskNameValue.values[1]);
			
			if (window.execScript) {
				window.execScript(js);
			} else {
				eval(js);
			}
			
		} else {
			return false;
		}
		
		return true;

		// else if (taskValue == 'DA') {
		//
		// for (var i = 1; i < nameValues.length; i++) {
		//
		// var tagId = wffBMUtil
		// .getIntFromOptimizedBytes(nameValues[i].name);
		// var attrNames = nameValues[i].values;
		//
		// var tagName = getStringFromBytes(attrNames[0]);
		//
		// for (var j = 1; j < attrNames.length; j++) {
		//
		// var attrName = getStringFromBytes(attrNames[j]);
		//
		// var applicableTag = wffTagUtil.getTagByTagNameAndWffId(
		// tagName, tagId);
		//
		// applicableTag.removeAttribute(attrName);
		// console.log('attr removed');
		// }
		//
		// }
		// }

	};

	this.getAttributeUpdates = function(wffBMBytes) {
		var nameValue = wffBMUtil.parseWffBinaryMessageBytes(wffBMBytes)[1];
	};

};




© 2015 - 2025 Weber Informatics LLC | Privacy Policy