uc-base-libraries
Version:
Typescript libraries for UC projects
212 lines • 9.77 kB
JavaScript
System.register(["./ApiLibrary", "./Debug"], function (exports_1, context_1) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var ApiLibrary, Debug;
var __moduleName = context_1 && context_1.id;
function createAjaxOptions(beforeCall, afterResponse) {
return {
beforeCall,
afterResponse,
};
}
exports_1("createAjaxOptions", createAjaxOptions);
function checkOptions(options) {
const defaults = {
beforeCall: null,
afterResponse: null,
};
const settings = $.extend({}, defaults, options);
if (!settings.beforeCall) {
settings.beforeCall = (item) => {
return false;
};
}
if (!settings.afterResponse) {
settings.afterResponse = (item, data) => {
return;
};
}
return settings;
}
function AddScript(fileName) {
Debug.debugWrite('AddScript: ' + fileName);
const scr = document.createElement('script');
const head = document.head || document.getElementsByTagName('head')[0];
scr.src = fileName;
scr.async = false; // optionally
const p = new Promise((resolve) => {
scr.addEventListener('load', () => {
resolve();
});
});
head.insertBefore(scr, head.firstChild);
return p;
}
exports_1("AddScript", AddScript);
function AddCss(fileName) {
return __awaiter(this, void 0, void 0, function* () {
Debug.debugWrite('AddCss: ' + fileName);
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('link');
style.href = fileName;
style.type = 'text/css';
style.rel = 'stylesheet';
head.append(style);
});
}
exports_1("AddCss", AddCss);
return {
setters: [
function (ApiLibrary_1) {
ApiLibrary = ApiLibrary_1;
},
function (Debug_1) {
Debug = Debug_1;
}
],
execute: function () {
if (jQuery) {
if ($) {
$.extend({
replaceTag: (currentElem, newTagObj, keepProps) => {
const $currentElem = $(currentElem);
const $newTag = $(newTagObj).clone();
if (keepProps) {
$.each($currentElem[0].attributes, (index, it) => {
$newTag.attr(it.name, it.value);
});
}
$currentElem.wrapAll($newTag);
$currentElem.contents().unwrap();
// return node; (Error spotted by Frank van Luijn)
return this; // Suggested by ColeLawrence
},
});
$.fn.extend({
replaceTag: (newTagObj, keepProps) => {
// "return" suggested by ColeLawrence
const elem = this;
return elem.each(() => {
jQuery.replaceTag(elem, newTagObj, keepProps);
});
},
});
}
jQuery.fn.extend({
disable: function (state) {
const items = $(this);
return items.each((i, it) => {
const $this = $(it);
if ($this.is('input, button, textarea, select'))
$this[0].disabled = state;
else
$this.toggleClass('disabled', state);
try {
$this.prop('disabled', state);
}
catch (_a) { }
});
},
});
jQuery.fn.submitUsingAjax = function (options) {
const settings = checkOptions(options);
const form = this;
const clickedItem = this;
if (settings.beforeCall(null, this)) {
return;
}
const clickUrl = ApiLibrary.addFormatToUrl($(clickedItem).attr('action'));
const formData = $(this).serialize();
ApiLibrary.postCall(clickUrl, null, formData, (data) => {
settings.afterResponse(clickedItem, data);
});
};
jQuery.fn.onSubmitUseAjax = function (options) {
const settings = checkOptions(options);
const form = this;
$(form)
.find("[type='submit']")
.click(function () {
$("[type='submit']", $(this).parents('form')).removeAttr('clicked');
$(this).attr('clicked', 'true');
});
$(form).submit(function (evt) {
evt.preventDefault();
const clickedItem = $(this).find('[type=submit][clicked=true]');
if (settings.beforeCall(clickedItem, this)) {
return;
}
$(form).find("input[type='submit'],button[type='submit']").disable(true);
const clickUrl = ApiLibrary.addFormatToUrl($(form).attr('action'));
$(this).append("<input type='hidden' name='" + clickedItem.attr('name') + "' value='" + clickedItem.val() + "'/>");
const formData = $(this).serialize();
ApiLibrary.postCall(clickUrl, null, formData, (data) => {
settings.afterResponse(clickedItem, data);
$(form).find("input[type='submit'],button[type='submit']").disable(false);
});
});
};
jQuery.fn.onClickAjaxGet = function (options) {
const settings = checkOptions(options);
const item = this;
$(item).click(function (evt) {
if (!evt.isDefaultPrevented()) {
evt.preventDefault();
const clickedItem = this;
if (settings.beforeCall(clickedItem, null)) {
return;
}
const clickUrl = ApiLibrary.addFormatToUrl($(clickedItem).attr('href'));
ApiLibrary.getCall(clickUrl, null, (data) => {
settings.afterResponse(clickedItem, data);
});
}
});
};
jQuery.fn.onClickAjaxPost = function (options) {
const settings = checkOptions(options);
const item = this;
$(item).click(function (evt) {
if (!evt.isDefaultPrevented()) {
evt.preventDefault();
const clickedItem = this;
if (settings.beforeCall(clickedItem, this)) {
return;
}
const clickUrl = ApiLibrary.addFormatToUrl($(clickedItem).attr('href'));
ApiLibrary.postCall(clickUrl, null, null, (data) => {
settings.afterResponse(this, data);
});
}
});
};
jQuery.fn.onClickPostAsForm = function (options) {
const settings = checkOptions(options);
const item = this;
$(item).click(function (evt) {
if (!evt.isDefaultPrevented()) {
evt.preventDefault();
const clickedItem = this;
if (settings.beforeCall(clickedItem, this)) {
return;
}
const clickUrl = $(clickedItem).attr('href');
const doc = "<form action='" + clickUrl + "' method='post'></form>";
const form = $(doc).appendTo(document.body);
$(form).submit();
}
});
};
}
}
};
});
//# sourceMappingURL=JqueryEx.js.map