From f33e566ac664bb899eeb29cc5356f3d7bb21f5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 18 Apr 2018 14:15:30 +0300 Subject: [PATCH] Moved npm packages to this repo. --- .../abp.resourcemapping.js | 8 + .../package.json | 7 + .../abp.resourcemapping.js | 29 + .../package.json | 15 + npm/aspnetcore.mvc.ui/gulp/copy-resources.js | 94 +++ npm/aspnetcore.mvc.ui/package.json | 12 + npm/core/abp.resourcemapping.js | 6 + npm/core/package.json | 7 + npm/core/src/abp.js | 570 ++++++++++++++++++ npm/jquery/abp.resourcemapping.js | 12 + npm/jquery/package.json | 8 + npm/jquery/src/abp.ajax.js | 188 ++++++ npm/jquery/src/abp.resource-loader.js | 89 +++ 13 files changed, 1045 insertions(+) create mode 100644 npm/aspnetcore.mvc.ui.theme.basic/abp.resourcemapping.js create mode 100644 npm/aspnetcore.mvc.ui.theme.basic/package.json create mode 100644 npm/aspnetcore.mvc.ui.theme.shared/abp.resourcemapping.js create mode 100644 npm/aspnetcore.mvc.ui.theme.shared/package.json create mode 100644 npm/aspnetcore.mvc.ui/gulp/copy-resources.js create mode 100644 npm/aspnetcore.mvc.ui/package.json create mode 100644 npm/core/abp.resourcemapping.js create mode 100644 npm/core/package.json create mode 100644 npm/core/src/abp.js create mode 100644 npm/jquery/abp.resourcemapping.js create mode 100644 npm/jquery/package.json create mode 100644 npm/jquery/src/abp.ajax.js create mode 100644 npm/jquery/src/abp.resource-loader.js diff --git a/npm/aspnetcore.mvc.ui.theme.basic/abp.resourcemapping.js b/npm/aspnetcore.mvc.ui.theme.basic/abp.resourcemapping.js new file mode 100644 index 0000000000..ca7824597e --- /dev/null +++ b/npm/aspnetcore.mvc.ui.theme.basic/abp.resourcemapping.js @@ -0,0 +1,8 @@ +module.exports = { + imports: [ + "../aspnetcore.mvc.ui.theme.shared/abp.resourcemapping.js" + ], + mappings: { + + } +} \ No newline at end of file diff --git a/npm/aspnetcore.mvc.ui.theme.basic/package.json b/npm/aspnetcore.mvc.ui.theme.basic/package.json new file mode 100644 index 0000000000..7185277caa --- /dev/null +++ b/npm/aspnetcore.mvc.ui.theme.basic/package.json @@ -0,0 +1,7 @@ +{ + "version": "0.2.1", + "name": "@abp/aspnetcore.mvc.ui.theme.basic", + "dependencies": { + "@abp/aspnetcore.mvc.ui.theme.shared": "^0.2.1" + } +} diff --git a/npm/aspnetcore.mvc.ui.theme.shared/abp.resourcemapping.js b/npm/aspnetcore.mvc.ui.theme.shared/abp.resourcemapping.js new file mode 100644 index 0000000000..e95da6cd95 --- /dev/null +++ b/npm/aspnetcore.mvc.ui.theme.shared/abp.resourcemapping.js @@ -0,0 +1,29 @@ +module.exports = { + imports: [ + "../jquery/abp.resourcemapping.js" + ], + mappings: { + //Bootstrap + "@node_modules/bootstrap/dist/css/bootstrap.css": "@libs/bootstrap/css/", + "@node_modules/bootstrap/dist/js/bootstrap.bundle.js": "@libs/bootstrap/js/", + + //Font-Awesome + "@node_modules/font-awesome/css/font-awesome.css": "@libs/font-awesome/css/", + "@node_modules/font-awesome/fonts/*.*": "@libs/font-awesome/fonts/", + + //jQuery-Form + "@node_modules/jquery-form/dist/jquery.form.min.js": "@libs/jquery-form/", + + //jQuery-Validation + "@node_modules/jquery-validation/dist/jquery.validate.js": "@libs/jquery-validation/dist/", //TODO: do not use dist on destination + "@node_modules/jquery-validation/dist/localization/*.*": "@libs/jquery-validation/dist/localization/", //TODO: do not use dist on destination + + //jQuery-Validation-Unobtrusive + "@node_modules/jquery-validation-unobtrusive/dist/jquery.validate.unobtrusive.js": "@libs/jquery-validation-unobtrusive/", + + //Datatables + "@node_modules/datatables.net/js/jquery.dataTables.js": "@libs/datatables.net/js/", + "@node_modules/datatables.net-bs4/css/dataTables.bootstrap4.css": "@libs/datatables.net-bs4/css/", + "@node_modules/datatables.net-bs4/js/dataTables.bootstrap4.js": "@libs/datatables.net-bs4/js/" + } +} \ No newline at end of file diff --git a/npm/aspnetcore.mvc.ui.theme.shared/package.json b/npm/aspnetcore.mvc.ui.theme.shared/package.json new file mode 100644 index 0000000000..3ccd0c1f69 --- /dev/null +++ b/npm/aspnetcore.mvc.ui.theme.shared/package.json @@ -0,0 +1,15 @@ +{ + "version": "0.2.1", + "name": "@abp/aspnetcore.mvc.ui.theme.shared", + "dependencies": { + "@abp/aspnetcore.mvc.ui": "^0.2.1", + "@abp/jquery": "^0.2.1", + "bootstrap": "^4.1.0", + "font-awesome": "^4.7.0", + "jquery-form": "^4.2.2", + "jquery-validation": "^1.17.0", + "jquery-validation-unobtrusive": "^3.2.9", + "datatables.net": "^1.10.16", + "datatables.net-bs4": "^1.10.16" + } +} diff --git a/npm/aspnetcore.mvc.ui/gulp/copy-resources.js b/npm/aspnetcore.mvc.ui/gulp/copy-resources.js new file mode 100644 index 0000000000..2f8f048a3d --- /dev/null +++ b/npm/aspnetcore.mvc.ui/gulp/copy-resources.js @@ -0,0 +1,94 @@ +"use strict"; + +(function () { + + var taskName = 'aspnetcore.mvc.ui.copy-resources'; + + var gulp = require("gulp"), + merge = require("merge-stream"), + rimraf = require("rimraf"), + path = require("path"), + extendObject = require('extend-object'); + + function init(rootMappingFilePath) { + var resourceMapping = buildResourceMapping(rootMappingFilePath); + + function replaceAliases(text) { + if (!resourceMapping.aliases) { + return text; + } + + for (var alias in resourceMapping.aliases) { + if (!resourceMapping.aliases.hasOwnProperty(alias)) { + continue; + } + + text = replaceAll(text, alias, resourceMapping.aliases[alias]); + } + + return text; + } + + function replaceAll(text, search, replacement) { + return text.replace(new RegExp(search, 'g'), replacement); + } + + function cleanFiles() { + if (resourceMapping.clean) { + for (var i = 0; i < resourceMapping.clean.length; i++) { + rimraf.sync(replaceAliases(resourceMapping.clean[i]) + '/**/*', { force: true }); + } + } + } + + function buildResourceMapping(mappingFilePath) { + var resourcemapping = require(mappingFilePath); + + if (resourcemapping.imports && resourcemapping.imports.length) { + + var aliases = {}; + var mappings = {}; + + for (var i = 0; i < resourcemapping.imports.length; i++) { + + var importedMappingFilePath = path.join(path.dirname(mappingFilePath), resourcemapping.imports[i]); + var importedResourceMapping = buildResourceMapping(importedMappingFilePath); + extendObject(aliases, importedResourceMapping.aliases); + extendObject(mappings, importedResourceMapping.mappings); + } + + extendObject(aliases, resourcemapping.aliases); + extendObject(mappings, resourcemapping.mappings); + + resourcemapping.aliases = aliases; + resourcemapping.mappings = mappings; + } + + return resourcemapping; + } + + gulp.task(taskName, + function () { + cleanFiles(); + + var tasks = []; + + for (var mapping in resourceMapping.mappings) { + if (resourceMapping.mappings.hasOwnProperty(mapping)) { + var source = replaceAliases(mapping); + var destination = replaceAliases(resourceMapping.mappings[mapping]); + tasks.push( + gulp.src(source).pipe(gulp.dest(destination)) + ); + } + } + + return merge(tasks); + }); + } + + module.exports = { + taskName: taskName, + init: init + }; +})(); \ No newline at end of file diff --git a/npm/aspnetcore.mvc.ui/package.json b/npm/aspnetcore.mvc.ui/package.json new file mode 100644 index 0000000000..3ee7ff9716 --- /dev/null +++ b/npm/aspnetcore.mvc.ui/package.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.1", + "name": "@abp/aspnetcore.mvc.ui", + "dependencies": { + "ansi-colors": "^1.1.0", + "gulp": "^3.9.1", + "merge-stream": "^1.0.1", + "rimraf": "^2.6.2", + "extend-object": "^1.0.0", + "path": "^0.12.7" + } +} diff --git a/npm/core/abp.resourcemapping.js b/npm/core/abp.resourcemapping.js new file mode 100644 index 0000000000..9fb6d1443f --- /dev/null +++ b/npm/core/abp.resourcemapping.js @@ -0,0 +1,6 @@ +module.exports = { + mappings: { + //@abp/core + "@node_modules/@abp/core/src/abp.js": "@libs/abp/core/src/" //TODO: don't use src in the destination + } +} \ No newline at end of file diff --git a/npm/core/package.json b/npm/core/package.json new file mode 100644 index 0000000000..7efb3194e6 --- /dev/null +++ b/npm/core/package.json @@ -0,0 +1,7 @@ +{ + "version": "0.2.1", + "name": "@abp/core", + "dependencies": { + + } +} diff --git a/npm/core/src/abp.js b/npm/core/src/abp.js new file mode 100644 index 0000000000..dbfbdd6649 --- /dev/null +++ b/npm/core/src/abp.js @@ -0,0 +1,570 @@ +var abp = abp || {}; +(function ($) { + + /* Application paths *****************************************/ + + //Current application root path (including virtual directory if exists). + abp.appPath = abp.appPath || '/'; + + abp.pageLoadTime = new Date(); + + //Converts given path to absolute path using abp.appPath variable. + abp.toAbsAppPath = function (path) { + if (path.indexOf('/') == 0) { + path = path.substring(1); + } + + return abp.appPath + path; + }; + + /* LOGGING ***************************************************/ + //Implements Logging API that provides secure & controlled usage of console.log + + abp.log = abp.log || {}; + + abp.log.levels = { + DEBUG: 1, + INFO: 2, + WARN: 3, + ERROR: 4, + FATAL: 5 + }; + + abp.log.level = abp.log.levels.DEBUG; + + abp.log.log = function (logObject, logLevel) { + if (!window.console || !window.console.log) { + return; + } + + if (logLevel != undefined && logLevel < abp.log.level) { + return; + } + + console.log(logObject); + }; + + abp.log.debug = function (logObject) { + abp.log.log("DEBUG: ", abp.log.levels.DEBUG); + abp.log.log(logObject, abp.log.levels.DEBUG); + }; + + abp.log.info = function (logObject) { + abp.log.log("INFO: ", abp.log.levels.INFO); + abp.log.log(logObject, abp.log.levels.INFO); + }; + + abp.log.warn = function (logObject) { + abp.log.log("WARN: ", abp.log.levels.WARN); + abp.log.log(logObject, abp.log.levels.WARN); + }; + + abp.log.error = function (logObject) { + abp.log.log("ERROR: ", abp.log.levels.ERROR); + abp.log.log(logObject, abp.log.levels.ERROR); + }; + + abp.log.fatal = function (logObject) { + abp.log.log("FATAL: ", abp.log.levels.FATAL); + abp.log.log(logObject, abp.log.levels.FATAL); + }; + + /* LOCALIZATION ***********************************************/ + + abp.localization = abp.localization || {}; + + abp.localization.values = {}; + + abp.localization.localize = function (key, sourceName) { + sourceName = sourceName || abp.localization.defaultResourceName; + + var source = abp.localization.values[sourceName]; + + if (!source) { + abp.log.warn('Could not find localization source: ' + sourceName); + return key; + } + + var value = source[key]; + if (value == undefined) { + return key; + } + + var copiedArguments = Array.prototype.slice.call(arguments, 0); + copiedArguments.splice(1, 1); + copiedArguments[0] = value; + + return abp.utils.formatString.apply(this, copiedArguments); + }; + + abp.localization.getResource = function (name) { + return function () { + var copiedArguments = Array.prototype.slice.call(arguments, 0); + copiedArguments.splice(1, 0, name); + return abp.localization.localize.apply(this, copiedArguments); + }; + }; + + abp.localization.defaultResourceName = undefined; + + /* AUTHORIZATION **********************************************/ + + abp.auth = abp.auth || {}; + + abp.auth.policies = abp.auth.policies || {}; + + abp.auth.grantedPolicies = abp.auth.grantedPolicies || {}; + + abp.auth.isGranted = function (policyName) { + return abp.auth.policies[policyName] != undefined && abp.auth.grantedPolicies[policyName] != undefined; + }; + + abp.auth.isAnyGranted = function () { + if (!arguments || arguments.length <= 0) { + return true; + } + + for (var i = 0; i < arguments.length; i++) { + if (abp.auth.isGranted(arguments[i])) { + return true; + } + } + + return false; + }; + + abp.auth.areAllGranted = function () { + if (!arguments || arguments.length <= 0) { + return true; + } + + for (var i = 0; i < arguments.length; i++) { + if (!abp.auth.isGranted(arguments[i])) { + return false; + } + } + + return true; + }; + + abp.auth.tokenCookieName = 'Abp.AuthToken'; + + abp.auth.setToken = function (authToken, expireDate) { + abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath, abp.domain); + }; + + abp.auth.getToken = function () { + return abp.utils.getCookieValue(abp.auth.tokenCookieName); + } + + abp.auth.clearToken = function () { + abp.auth.setToken(); + } + + /* NOTIFICATION *********************************************/ + //Defines Notification API, not implements it + + abp.notify = abp.notify || {}; + + abp.notify.success = function (message, title, options) { + abp.log.warn('abp.notify.success is not implemented!'); + }; + + abp.notify.info = function (message, title, options) { + abp.log.warn('abp.notify.info is not implemented!'); + }; + + abp.notify.warn = function (message, title, options) { + abp.log.warn('abp.notify.warn is not implemented!'); + }; + + abp.notify.error = function (message, title, options) { + abp.log.warn('abp.notify.error is not implemented!'); + }; + + /* MESSAGE **************************************************/ + //Defines Message API, not implements it + + abp.message = abp.message || {}; + + var showMessage = function (message, title) { + alert((title || '') + ' ' + message); + + if (!$) { + abp.log.warn('abp.message can not return promise since jQuery is not defined!'); + return null; + } + + return $.Deferred(function ($dfd) { + $dfd.resolve(); + }); + }; + + abp.message.info = function (message, title) { + abp.log.warn('abp.message.info is not implemented!'); + return showMessage(message, title); + }; + + abp.message.success = function (message, title) { + abp.log.warn('abp.message.success is not implemented!'); + return showMessage(message, title); + }; + + abp.message.warn = function (message, title) { + abp.log.warn('abp.message.warn is not implemented!'); + return showMessage(message, title); + }; + + abp.message.error = function (message, title) { + abp.log.warn('abp.message.error is not implemented!'); + return showMessage(message, title); + }; + + abp.message.confirm = function (message, titleOrCallback, callback) { + abp.log.warn('abp.message.confirm is not implemented!'); + + if (titleOrCallback && !(typeof titleOrCallback == 'string')) { + callback = titleOrCallback; + } + + var result = confirm(message); + callback && callback(result); + + if (!$) { + abp.log.warn('abp.message can not return promise since jQuery is not defined!'); + return null; + } + + return $.Deferred(function ($dfd) { + $dfd.resolve(); + }); + }; + + /* UI *******************************************************/ + + abp.ui = abp.ui || {}; + + /* UI BLOCK */ + //Defines UI Block API, not implements it + + abp.ui.block = function (elm) { + abp.log.warn('abp.ui.block is not implemented!'); + }; + + abp.ui.unblock = function (elm) { + abp.log.warn('abp.ui.unblock is not implemented!'); + }; + + /* UI BUSY */ + //Defines UI Busy API, not implements it + + abp.ui.setBusy = function (elm, optionsOrPromise) { + abp.log.warn('abp.ui.setBusy is not implemented!'); + }; + + abp.ui.clearBusy = function (elm) { + abp.log.warn('abp.ui.clearBusy is not implemented!'); + }; + + /* SIMPLE EVENT BUS *****************************************/ + + abp.event = (function () { + + var _callbacks = {}; + + var on = function (eventName, callback) { + if (!_callbacks[eventName]) { + _callbacks[eventName] = []; + } + + _callbacks[eventName].push(callback); + }; + + var off = function (eventName, callback) { + var callbacks = _callbacks[eventName]; + if (!callbacks) { + return; + } + + var index = -1; + for (var i = 0; i < callbacks.length; i++) { + if (callbacks[i] === callback) { + index = i; + break; + } + } + + if (index < 0) { + return; + } + + _callbacks[eventName].splice(index, 1); + }; + + var trigger = function (eventName) { + var callbacks = _callbacks[eventName]; + if (!callbacks || !callbacks.length) { + return; + } + + var args = Array.prototype.slice.call(arguments, 1); + for (var i = 0; i < callbacks.length; i++) { + callbacks[i].apply(this, args); + } + }; + + // Public interface /////////////////////////////////////////////////// + + return { + on: on, + off: off, + trigger: trigger + }; + })(); + + + /* UTILS ***************************************************/ + + abp.utils = abp.utils || {}; + + /* Creates a name namespace. + * Example: + * var taskService = abp.utils.createNamespace(abp, 'services.task'); + * taskService will be equal to abp.services.task + * first argument (root) must be defined first + ************************************************************/ + abp.utils.createNamespace = function (root, ns) { + var parts = ns.split('.'); + for (var i = 0; i < parts.length; i++) { + if (typeof root[parts[i]] == 'undefined') { + root[parts[i]] = {}; + } + + root = root[parts[i]]; + } + + return root; + }; + + /* Find and replaces a string (search) to another string (replacement) in + * given string (str). + * Example: + * abp.utils.replaceAll('This is a test string', 'is', 'X') = 'ThX X a test string' + ************************************************************/ + abp.utils.replaceAll = function (str, search, replacement) { + var fix = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + return str.replace(new RegExp(fix, 'g'), replacement); + }; + + /* Formats a string just like string.format in C#. + * Example: + * abp.utils.formatString('Hello {0}','Tuana') = 'Hello Tuana' + ************************************************************/ + abp.utils.formatString = function () { + if (arguments.length < 1) { + return null; + } + + var str = arguments[0]; + + for (var i = 1; i < arguments.length; i++) { + var placeHolder = '{' + (i - 1) + '}'; + str = abp.utils.replaceAll(str, placeHolder, arguments[i]); + } + + return str; + }; + + abp.utils.toPascalCase = function (str) { + if (!str || !str.length) { + return str; + } + + if (str.length === 1) { + return str.charAt(0).toUpperCase(); + } + + return str.charAt(0).toUpperCase() + str.substr(1); + } + + abp.utils.toCamelCase = function (str) { + if (!str || !str.length) { + return str; + } + + if (str.length === 1) { + return str.charAt(0).toLowerCase(); + } + + return str.charAt(0).toLowerCase() + str.substr(1); + } + + abp.utils.truncateString = function (str, maxLength) { + if (!str || !str.length || str.length <= maxLength) { + return str; + } + + return str.substr(0, maxLength); + }; + + abp.utils.truncateStringWithPostfix = function (str, maxLength, postfix) { + postfix = postfix || '...'; + + if (!str || !str.length || str.length <= maxLength) { + return str; + } + + if (maxLength <= postfix.length) { + return postfix.substr(0, maxLength); + } + + return str.substr(0, maxLength - postfix.length) + postfix; + }; + + abp.utils.isFunction = function (obj) { + if ($) { + //Prefer to use jQuery if possible + return $.isFunction(obj); + } + + //alternative for $.isFunction + return !!(obj && obj.constructor && obj.call && obj.apply); + }; + + /** + * parameterInfos should be an array of { name, value } objects + * where name is query string parameter name and value is it's value. + * includeQuestionMark is true by default. + */ + abp.utils.buildQueryString = function (parameterInfos, includeQuestionMark) { + if (includeQuestionMark === undefined) { + includeQuestionMark = true; + } + + var qs = ''; + + function addSeperator() { + if (!qs.length) { + if (includeQuestionMark) { + qs = qs + '?'; + } + } else { + qs = qs + '&'; + } + } + + for (var i = 0; i < parameterInfos.length; ++i) { + var parameterInfo = parameterInfos[i]; + if (parameterInfo.value === undefined) { + continue; + } + + if (parameterInfo.value === null) { + parameterInfo.value = ''; + } + + addSeperator(); + + if (parameterInfo.value.toJSON && typeof parameterInfo.value.toJSON === "function") { + qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value.toJSON()); + } else if (Array.isArray(parameterInfo.value) && parameterInfo.value.length) { + for (var j = 0; j < parameterInfo.value.length; j++) { + if (j > 0) { + addSeperator(); + } + + qs = qs + parameterInfo.name + '[' + j + ']=' + encodeURIComponent(parameterInfo.value[j]); + } + } else { + qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value); + } + } + + return qs; + } + + /** + * Sets a cookie value for given key. + * This is a simple implementation created to be used by ABP. + * Please use a complete cookie library if you need. + * @param {string} key + * @param {string} value + * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session. + * @param {string} path (optional) + */ + abp.utils.setCookieValue = function (key, value, expireDate, path) { + var cookieValue = encodeURIComponent(key) + '='; + + if (value) { + cookieValue = cookieValue + encodeURIComponent(value); + } + + if (expireDate) { + cookieValue = cookieValue + "; expires=" + expireDate.toUTCString(); + } + + if (path) { + cookieValue = cookieValue + "; path=" + path; + } + + document.cookie = cookieValue; + }; + + /** + * Gets a cookie with given key. + * This is a simple implementation created to be used by ABP. + * Please use a complete cookie library if you need. + * @param {string} key + * @returns {string} Cookie value or null + */ + abp.utils.getCookieValue = function (key) { + var equalities = document.cookie.split('; '); + for (var i = 0; i < equalities.length; i++) { + if (!equalities[i]) { + continue; + } + + var splitted = equalities[i].split('='); + if (splitted.length != 2) { + continue; + } + + if (decodeURIComponent(splitted[0]) === key) { + return decodeURIComponent(splitted[1] || ''); + } + } + + return null; + }; + + /** + * Deletes cookie for given key. + * This is a simple implementation created to be used by ABP. + * Please use a complete cookie library if you need. + * @param {string} key + * @param {string} path (optional) + */ + abp.utils.deleteCookie = function (key, path) { + var cookieValue = encodeURIComponent(key) + '='; + + cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString(); + + if (path) { + cookieValue = cookieValue + "; path=" + path; + } + + document.cookie = cookieValue; + } + + /* SECURITY ***************************************/ + abp.security = abp.security || {}; + abp.security.antiForgery = abp.security.antiForgery || {}; + + abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN'; + abp.security.antiForgery.tokenHeaderName = 'X-XSRF-TOKEN'; + + abp.security.antiForgery.getToken = function () { + return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName); + }; + +})(); \ No newline at end of file diff --git a/npm/jquery/abp.resourcemapping.js b/npm/jquery/abp.resourcemapping.js new file mode 100644 index 0000000000..060bc6c565 --- /dev/null +++ b/npm/jquery/abp.resourcemapping.js @@ -0,0 +1,12 @@ +module.exports = { + imports: [ + "../core/abp.resourcemapping.js" + ], + mappings: { + //jQuery + "@node_modules/jquery/dist/jquery.js": "@libs/jquery/", + + //@abp/jquery + "@node_modules/@abp/jquery/src/*.*": "@libs/abp/jquery/" + } +} \ No newline at end of file diff --git a/npm/jquery/package.json b/npm/jquery/package.json new file mode 100644 index 0000000000..4ac5d20277 --- /dev/null +++ b/npm/jquery/package.json @@ -0,0 +1,8 @@ +{ + "version": "0.2.1", + "name": "@abp/jquery", + "dependencies": { + "@abp/core": "^0.2.1", + "jquery": "^3.3.1" + } +} diff --git a/npm/jquery/src/abp.ajax.js b/npm/jquery/src/abp.ajax.js new file mode 100644 index 0000000000..8b7d0d1ec9 --- /dev/null +++ b/npm/jquery/src/abp.ajax.js @@ -0,0 +1,188 @@ +var abp = abp || {}; +(function ($) { + + if (!$) { + return; + } + + /* JQUERY ENHANCEMENTS ***************************************************/ + + // abp.ajax -> uses $.ajax ------------------------------------------------ + + abp.ajax = function (userOptions) { + userOptions = userOptions || {}; + + var options = $.extend(true, {}, abp.ajax.defaultOpts, userOptions); + + options.success = undefined; + options.error = undefined; + + return $.Deferred(function ($dfd) { + $.ajax(options) + .done(function (data, textStatus, jqXHR) { + $dfd.resolve(data); + userOptions.success && userOptions.success(data); + }).fail(function (jqXHR) { + if (jqXHR.getResponseHeader('_AbpErrorFormat') === 'true') { + abp.ajax.handleAbpErrorResponse(jqXHR, userOptions, $dfd); + } else { + abp.ajax.handleNonAbpErrorResponse(jqXHR, userOptions, $dfd); + } + }); + }); + }; + + $.extend(abp.ajax, { + defaultOpts: { + dataType: 'json', + type: 'POST', + contentType: 'application/json', + headers: { + 'X-Requested-With': 'XMLHttpRequest' + } + }, + + defaultError: { + message: 'An error has occurred!', + details: 'Error detail not sent by server.' + }, + + defaultError401: { + message: 'You are not authenticated!', + details: 'You should be authenticated (sign in) in order to perform this operation.' + }, + + defaultError403: { + message: 'You are not authorized!', + details: 'You are not allowed to perform this operation.' + }, + + defaultError404: { + message: 'Resource not found!', + details: 'The resource requested could not found on the server.' + }, + + logError: function (error) { + abp.log.error(error); + }, + + showError: function (error) { + if (error.details) { + return abp.message.error(error.details, error.message); + } else { + return abp.message.error(error.message || abp.ajax.defaultError.message); + } + }, + + handleTargetUrl: function (targetUrl) { + if (!targetUrl) { + location.href = abp.appPath; + } else { + location.href = targetUrl; + } + }, + + handleErrorStatusCode: function (status) { + switch (status) { + case 401: + abp.ajax.handleUnAuthorizedRequest( + abp.ajax.showError(abp.ajax.defaultError401), + abp.appPath + ); + break; + case 403: + abp.ajax.showError(abp.ajax.defaultError403); + break; + case 404: + abp.ajax.showError(abp.ajax.defaultError404); + break; + default: + abp.ajax.showError(abp.ajax.defaultError); + break; + } + }, + + handleNonAbpErrorResponse: function (jqXHR, userOptions, $dfd) { + if (userOptions.abpHandleError !== false) { + abp.ajax.handleErrorStatusCode(jqXHR.status); + } + + $dfd.reject.apply(this, arguments); + userOptions.error && userOptions.error.apply(this, arguments); + }, + + handleAbpErrorResponse: function (jqXHR, userOptions, $dfd) { + var messagePromise = null; + + if (userOptions.abpHandleError !== false) { + messagePromise = abp.ajax.showError(jqXHR.responseJSON.error); + } + + abp.ajax.logError(jqXHR.responseJSON.error); + + $dfd && $dfd.reject(jqXHR.responseJSON.error, jqXHR); + userOptions.error && userOptions.error(jqXHR.responseJSON.error, jqXHR); + + if (jqXHR.status === 401 && userOptions.abpHandleError !== false) { + abp.ajax.handleUnAuthorizedRequest(messagePromise); + } + }, + + handleUnAuthorizedRequest: function (messagePromise, targetUrl) { + if (messagePromise) { + messagePromise.done(function () { + abp.ajax.handleTargetUrl(targetUrl); + }); + } else { + abp.ajax.handleTargetUrl(targetUrl); + } + }, + + blockUI: function (options) { + if (options.blockUI) { + if (options.blockUI === true) { //block whole page + abp.ui.setBusy(); + } else { //block an element + abp.ui.setBusy(options.blockUI); + } + } + }, + + unblockUI: function (options) { + if (options.blockUI) { + if (options.blockUI === true) { //unblock whole page + abp.ui.clearBusy(); + } else { //unblock an element + abp.ui.clearBusy(options.blockUI); + } + } + }//, + + //ajaxSendHandler: function (event, request, settings) { + // var token = abp.security.antiForgery.getToken(); + // if (!token) { + // return; + // } + + // if (!settings.headers || settings.headers[abp.security.antiForgery.tokenHeaderName] === undefined) { + // request.setRequestHeader(abp.security.antiForgery.tokenHeaderName, token); + // } + //} + }); + + //$(document).ajaxSend(function (event, request, settings) { + // return abp.ajax.ajaxSendHandler(event, request, settings); + //}); + + //abp.event.on('abp.dynamicScriptsInitialized', function () { + // abp.ajax.defaultError.message = abp.localization.abpWeb('DefaultError'); + // abp.ajax.defaultError.details = abp.localization.abpWeb('DefaultErrorDetail'); + // abp.ajax.defaultError401.message = abp.localization.abpWeb('DefaultError401'); + // abp.ajax.defaultError401.details = abp.localization.abpWeb('DefaultErrorDetail401'); + // abp.ajax.defaultError403.message = abp.localization.abpWeb('DefaultError403'); + // abp.ajax.defaultError403.details = abp.localization.abpWeb('DefaultErrorDetail403'); + // abp.ajax.defaultError404.message = abp.localization.abpWeb('DefaultError404'); + // abp.ajax.defaultError404.details = abp.localization.abpWeb('DefaultErrorDetail404'); + //}); + +})(jQuery); \ No newline at end of file diff --git a/npm/jquery/src/abp.resource-loader.js b/npm/jquery/src/abp.resource-loader.js new file mode 100644 index 0000000000..39871dc9e8 --- /dev/null +++ b/npm/jquery/src/abp.resource-loader.js @@ -0,0 +1,89 @@ +/** + * abp.ResourceLoader can be used to load scripts when needed. + * It ensures that every script is only loaded once. + * + * TODO: Add a loadStyle method + */ +var abp = abp || {}; +(function ($) { + + /* UrlStates enum */ + var UrlStates = { + LOADING: 'LOADING', + LOADED: 'LOADED', + FAILED: 'FAILED' + }; + + /* UrlInfo class */ + function UrlInfo() { + this.state = UrlStates.LOADING; + this.loadCallbacks = []; + this.failCallbacks = []; + } + + UrlInfo.prototype.succeed = function () { + this.state = UrlStates.LOADED; + for (var i = 0; i < this.loadCallbacks.length; i++) { + this.loadCallbacks[i](); + } + }; + + UrlInfo.prototype.failed = function () { + this.state = UrlStates.FAILED; + for (var i = 0; i < this.failCallbacks.length; i++) { + this.failCallbacks[i](); + } + }; + + UrlInfo.prototype.handleCallbacks = function (loadCallback, failCallback) { + switch (this.state) { + case UrlStates.LOADED: + loadCallback && loadCallback(); + break; + case UrlStates.FAILED: + failCallback && failCallback(); + break; + case UrlStates.LOADING: + this.addCallbacks(loadCallback, failCallback); + break; + } + }; + + UrlInfo.prototype.addCallbacks = function (loadCallback, failCallback) { + loadCallback && this.loadCallbacks.push(loadCallback); + failCallback && this.failCallbacks.push(failCallback); + }; + + /* ResourceLoader API */ + + abp.ResourceLoader = (function () { + + var _urlInfos = {}; + + var _loadScript = function (url, loadCallback, failCallback) { + + var urlInfo = _urlInfos[url]; + + if (urlInfo) { + urlInfo.handleCallbacks(loadCallback, failCallback); + return; + } + + _urlInfos[url] = urlInfo = new UrlInfo(); + urlInfo.addCallbacks(loadCallback, failCallback); + + $.getScript(url) + .done(function (script, textStatus) { + urlInfo.succeed(); + }) + .fail(function (jqxhr, settings, exception) { + urlInfo.failed(); + }); + }; + + return { + loadScript: _loadScript + } + })(); + +})(jQuery); \ No newline at end of file