status-react/resources/status.js

178 lines
3.9 KiB
JavaScript
Raw Normal View History

var _status_catalog = {
2016-10-11 14:24:52 +00:00
commands: {},
responses: {}
},
status = {};
function Command() {
}
function Response() {
}
Command.prototype.addToCatalog = function () {
_status_catalog.commands[this.name] = this;
};
Command.prototype.param = function (parameter) {
this.params.push(parameter);
return this;
};
Command.prototype.create = function (com) {
this.name = com.name;
this.description = com.description;
this.handler = com.handler;
2016-06-30 16:00:44 +00:00
this["has-handler"] = com.handler != null;
2016-07-07 16:09:16 +00:00
this.validator = com.validator;
this.color = com.color;
this.icon = com.icon;
this.params = com.params || [];
2016-06-14 14:36:39 +00:00
this.preview = com.preview;
2016-07-18 07:38:24 +00:00
this["suggestions-trigger"] = com.suggestionsTrigger || "on-change";
this.fullscreen = com.fullscreen;
this.addToCatalog();
return this;
};
Response.prototype = Object.create(Command.prototype);
Response.prototype.addToCatalog = function () {
_status_catalog.responses[this.name] = this;
};
Response.prototype.onReceiveResponse = function (handler) {
this.onReceive = handler;
};
2016-10-11 14:24:52 +00:00
var context = {}
function addContext(ns, key, value) {
context[ns][key] = value;
}
function call(pathStr, paramsStr) {
var params = JSON.parse(paramsStr),
path = JSON.parse(pathStr),
2016-10-11 14:24:52 +00:00
fn, callResult, message_id;
if (typeof params.context !== "undefined" &&
typeof params.context["message-id"] !== "undefined") {
message_id = params.context["message-id"];
} else {
message_id = null;
}
context[message_id] = {};
status.message_id = message_id;
fn = path.reduce(function (catalog, name) {
if (catalog && catalog[name]) {
return catalog[name];
}
},
_status_catalog
);
2016-07-18 13:22:32 +00:00
if (!fn) {
2016-06-25 13:28:45 +00:00
return null;
}
2016-10-11 14:24:52 +00:00
callResult = fn(params.parameters, params.context);
result = {
returned: callResult,
context: context[message_id]
};
2016-10-11 14:24:52 +00:00
return JSON.stringify(result);
}
2016-06-14 12:56:03 +00:00
function text(options, s) {
return ['text', options, s];
}
function view(options, elements) {
return ['view', options].concat(elements);
}
function image(options) {
return ['image', options];
}
function touchable(options, element) {
return ['touchable', options, element];
}
function scrollView(options, elements) {
return ['scroll-view', options].concat(elements);
}
2016-07-18 07:38:24 +00:00
function webView(url) {
return ['web-view', {
source: {
uri: url
},
javaScriptEnabled: true
}];
}
2016-07-18 13:22:32 +00:00
function validationMessage(titleText, descriptionText) {
var titleStyle = {
2016-09-20 13:29:23 +00:00
style: {
color: "white",
fontSize: 12
}
2016-07-18 13:22:32 +00:00
};
var title = status.components.text(titleStyle, titleText);
var descriptionStyle = {
2016-09-20 13:29:23 +00:00
style: {
color: "white",
fontSize: 12,
opacity: 0.9
}
2016-07-18 13:22:32 +00:00
};
var description = status.components.text(descriptionStyle, descriptionText);
2016-06-30 16:00:44 +00:00
return status.components.view(
2016-07-18 13:22:32 +00:00
{
backgroundColor: "red",
height: 61,
paddingLeft: 16,
paddingTop: 14,
},
[title, description]
);
}
var status = {
2016-07-07 16:09:16 +00:00
command: function (h) {
var command = new Command();
2016-07-07 16:09:16 +00:00
return command.create(h);
},
2016-07-07 16:09:16 +00:00
response: function (h) {
var response = new Response();
2016-07-07 16:09:16 +00:00
return response.create(h);
},
2016-08-03 13:15:04 +00:00
autorun: function (commandName) {
_status_catalog.autorun = commandName;
},
types: {
2016-06-27 15:38:44 +00:00
TEXT: 'text',
NUMBER: 'number',
PHONE: 'phone',
PASSWORD: 'password'
},
events: {
SET_VALUE: 'set-value'
2016-06-14 12:56:03 +00:00
},
components: {
view: view,
text: text,
image: image,
touchable: touchable,
2016-07-18 07:38:24 +00:00
scrollView: scrollView,
2016-07-18 13:22:32 +00:00
webView: webView,
validationMessage: validationMessage
}
};