mirror of
https://github.com/The-Low-Code-Foundation/OpenNoodl.git
synced 2026-01-13 07:42:55 +01:00
Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com> Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com> Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com> Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com> Co-Authored-By: Johan <4934465+joolsus@users.noreply.github.com> Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com> Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
This commit is contained in:
73
packages/noodl-viewer-react/src/api/cloudfunctions.js
Normal file
73
packages/noodl-viewer-react/src/api/cloudfunctions.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const NoodlRuntime = require('@noodl/runtime');
|
||||
|
||||
function _makeRequest(path, options) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === 4) {
|
||||
var json;
|
||||
try {
|
||||
json = JSON.parse(xhr.response);
|
||||
} catch (e) {}
|
||||
|
||||
if (xhr.status === 200 || xhr.status === 201) {
|
||||
options.success(json);
|
||||
} else options.error(json);
|
||||
}
|
||||
};
|
||||
|
||||
xhr.open(options.method || 'GET', options.endpoint + path, true);
|
||||
|
||||
xhr.setRequestHeader('X-Parse-Application-Id', options.appId);
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
const cloudServices = NoodlRuntime.instance.getMetaData('cloudservices');
|
||||
if (cloudServices && cloudServices.deployVersion) {
|
||||
xhr.setRequestHeader('x-noodl-cloud-version', cloudServices.deployVersion);
|
||||
}
|
||||
|
||||
// Check for current users
|
||||
var _cu = localStorage['Parse/' + options.appId + '/currentUser'];
|
||||
if (_cu !== undefined) {
|
||||
try {
|
||||
const currentUser = JSON.parse(_cu);
|
||||
xhr.setRequestHeader('X-Parse-Session-Token', currentUser.sessionToken);
|
||||
} catch (e) {
|
||||
// Failed to extract session token
|
||||
}
|
||||
}
|
||||
|
||||
xhr.send(JSON.stringify(options.content));
|
||||
}
|
||||
|
||||
const cloudfunctions = {
|
||||
async run(functionName, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cloudServices = NoodlRuntime.instance.getMetaData('cloudservices');
|
||||
if (cloudServices === undefined) {
|
||||
reject('No cloud services defined in this project.');
|
||||
return;
|
||||
}
|
||||
|
||||
const appId = cloudServices.appId;
|
||||
const endpoint = NoodlRuntime.instance.editorConnection.isRunningLocally()
|
||||
? `http://${window.location.hostname}:8577`
|
||||
: cloudServices.endpoint;
|
||||
|
||||
_makeRequest('/functions/' + encodeURIComponent(functionName), {
|
||||
appId,
|
||||
endpoint,
|
||||
content: params,
|
||||
method: 'POST',
|
||||
success: (res) => {
|
||||
resolve(res ? res.result : undefined);
|
||||
},
|
||||
error: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = cloudfunctions;
|
||||
23
packages/noodl-viewer-react/src/api/files.js
Normal file
23
packages/noodl-viewer-react/src/api/files.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const CloudStore = require("@noodl/runtime/src/api/cloudstore");
|
||||
const CloudFile = require("@noodl/runtime/src/api/cloudfile");
|
||||
|
||||
const files = {
|
||||
async upload(file, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
CloudStore.instance.uploadFile({
|
||||
file,
|
||||
onUploadProgress: (p) => {
|
||||
options && options.onProgress && options.onProgress(p);
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(new CloudFile(response));
|
||||
},
|
||||
error: (e) => {
|
||||
reject(e);
|
||||
},
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = files;
|
||||
48
packages/noodl-viewer-react/src/api/navigation.js
Normal file
48
packages/noodl-viewer-react/src/api/navigation.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const { RouterHandler } = require('../nodes/navigation/router-handler');
|
||||
const NoodlRuntime = require('@noodl/runtime');
|
||||
|
||||
const navigation = {
|
||||
async showPopup(componentPath, params) {
|
||||
return new Promise((resolve) => {
|
||||
navigation._noodlRuntime.context.showPopup(componentPath, params, {
|
||||
onClosePopup: (action, results) => {
|
||||
resolve({
|
||||
action: action.replace('closeAction-', ''),
|
||||
parameters: results
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
navigate(routerName, targetPageName, params) {
|
||||
RouterHandler.instance.navigate(routerName, {
|
||||
target: targetPageName,
|
||||
params: params
|
||||
});
|
||||
},
|
||||
|
||||
navigateToPath(path, options) {
|
||||
let hashPath, urlPath;
|
||||
var navigationPathType = NoodlRuntime.instance.getProjectSettings()['navigationPathType'];
|
||||
if (navigationPathType === undefined || navigationPathType === 'hash') hashPath = path;
|
||||
else urlPath = path;
|
||||
|
||||
var query = [];
|
||||
if (options && options.query !== undefined) {
|
||||
for (let key in options.query) {
|
||||
query.push(key + '=' + options.query[key]);
|
||||
}
|
||||
}
|
||||
|
||||
var compiledUrl =
|
||||
(urlPath !== undefined ? urlPath : '') +
|
||||
(query.length >= 1 ? '?' + query.join('&') : '') +
|
||||
(hashPath !== undefined ? '#' + hashPath : '');
|
||||
|
||||
window.history.pushState({}, '', compiledUrl);
|
||||
dispatchEvent(new PopStateEvent('popstate', {}));
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = navigation;
|
||||
73
packages/noodl-viewer-react/src/api/seo.ts
Normal file
73
packages/noodl-viewer-react/src/api/seo.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
const IS_BROWSER = typeof document !== 'undefined';
|
||||
|
||||
export class SeoApi {
|
||||
private _title = '';
|
||||
private _meta: Record<string, string> = {};
|
||||
|
||||
/** Returns the current document title. */
|
||||
get title(): string {
|
||||
return IS_BROWSER ? document.title : this._title;
|
||||
}
|
||||
|
||||
/** Set the document title. */
|
||||
setTitle(value: string) {
|
||||
this._title = value;
|
||||
|
||||
if (IS_BROWSER) {
|
||||
document.title = value;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns all the current meta-tags */
|
||||
get meta(): Readonly<SeoApi['_meta']> {
|
||||
return this._meta;
|
||||
}
|
||||
|
||||
/** Clear all the meta-tags. */
|
||||
clearMeta(): void {
|
||||
if (IS_BROWSER) {
|
||||
// Remove all the meta-tags, technically this is useless when running client-side.
|
||||
Object.keys(this._meta).forEach((key) => {
|
||||
const metaTag = document.querySelector(`meta[name="${key}"]`);
|
||||
if (metaTag) {
|
||||
metaTag.remove();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this._meta = {};
|
||||
}
|
||||
|
||||
/** Returns a specific meta-tag by name. */
|
||||
getMeta(key: string) {
|
||||
// NOTE: We are not querying if the meta-tag exist, maybe something we would like to do?
|
||||
return this._meta[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a meta-tag.
|
||||
*
|
||||
* @param key The 'name' and/or the 'property' key used for the meta-tags.
|
||||
* @param value The meta-tag content; if undefined, the meta-tag is removed.
|
||||
*/
|
||||
setMeta(key: string, value: string | undefined): void {
|
||||
this._meta[key] = value;
|
||||
|
||||
if (IS_BROWSER) {
|
||||
const metaTag = document.querySelector(`meta[name="${key}"]`);
|
||||
if (metaTag) {
|
||||
if (!value) {
|
||||
metaTag.remove();
|
||||
} else {
|
||||
metaTag.setAttribute('content', value);
|
||||
}
|
||||
} else if (value) {
|
||||
const newMetaTag = document.createElement('meta');
|
||||
newMetaTag.setAttribute('name', key);
|
||||
newMetaTag.setAttribute('property', key);
|
||||
newMetaTag.setAttribute('content', value);
|
||||
document.head.appendChild(newMetaTag);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
178
packages/noodl-viewer-react/src/api/users.js
Normal file
178
packages/noodl-viewer-react/src/api/users.js
Normal file
@@ -0,0 +1,178 @@
|
||||
const UserService = require('../nodes/std-library/user/userservice')
|
||||
|
||||
const users = {
|
||||
async logIn(options) {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.logIn({
|
||||
username:options.username,
|
||||
password:options.password,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
async signUp(options) {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.signUp({
|
||||
username:options.username,
|
||||
password:options.password,
|
||||
email:options.email,
|
||||
properties:options.properties,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
async become(sessionToken) {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.fetchCurrentUser({
|
||||
sessionToken,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
// Deprecated use cloud functions instead
|
||||
/* async requestPasswordReset(options) {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.requestPasswordReset({
|
||||
email:options.email,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
async resetPassword(options) {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.resetPassword({
|
||||
token:options.token,
|
||||
username:options.username,
|
||||
newPassword:options.newPassword,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
async sendEmailVerification(options) {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.sendEmailVerification({
|
||||
email:options.email,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
async verifyEmail(options) {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.verifyEmail({
|
||||
username:options.username,
|
||||
token:options.token,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},*/
|
||||
|
||||
on(event,cb) {
|
||||
UserService.instance.on(event,cb)
|
||||
},
|
||||
|
||||
off(event,cb) {
|
||||
UserService.instance.off(event,cb)
|
||||
},
|
||||
}
|
||||
|
||||
const _currentUser = {
|
||||
async logOut() {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.logOut({
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
async save() {
|
||||
return new Promise((resolve,reject) => {
|
||||
const props = Object.assign({},_currentUser.Properties.data)
|
||||
|
||||
UserService.instance.setUserProperties({
|
||||
properties:props,
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
async fetch() {
|
||||
return new Promise((resolve,reject) => {
|
||||
UserService.instance.fetchCurrentUser({
|
||||
success:() => {
|
||||
resolve()
|
||||
},
|
||||
error:(e) => {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(users, 'Current', {
|
||||
get: function() {
|
||||
const _user = UserService.instance.current;
|
||||
if(_user === undefined) return;
|
||||
else {
|
||||
_currentUser.email = _user.email;
|
||||
_currentUser.username = _user.username;
|
||||
_currentUser.id = _user.id;
|
||||
_currentUser.emailVerified = _user.emailVerified;
|
||||
_currentUser.Properties = _user
|
||||
return _currentUser
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = users
|
||||
Reference in New Issue
Block a user