save works

This commit is contained in:
hyb1996
2018-10-12 16:25:24 +08:00
parent 9b1daa6567
commit 7fe204c44f
11 changed files with 306 additions and 372 deletions

View File

@@ -4,10 +4,7 @@ module.exports = function (runtime, global) {
require("array-observe.min")();
var J = util.java;
var ui = {};
ui.__view_cache__ = {};
ui.__defineGetter__("emitter", ()=> activity ? activity.getEventEmitter() : null);
@@ -34,7 +31,6 @@ module.exports = function (runtime, global) {
ui.setContentView = function (view) {
ui.view = view;
ui.__view_cache__ = {};
ui.run(function () {
activity.setContentView(view);
});
@@ -43,15 +39,11 @@ module.exports = function (runtime, global) {
ui.findById = function (id) {
if (!ui.view)
return null;
var v = ui.findByStringId(ui.view, id);
if (v) {
v = decorate(v);
}
return v;
return ui.findByStringId(ui.view, id);
}
ui.isUiThread = function () {
importClass(android.os.Looper);
let Looper = android.os.Looper;
return Looper.myLooper() == Looper.getMainLooper();
}
@@ -192,111 +184,8 @@ module.exports = function (runtime, global) {
}).call(ctx);
}
});
}
function decorate(view) {
return view;
var javaObject = view;
var view = global.events.__asEmitter__(Object.create(view));
view.__javaObject__ = javaObject;
if (view.getClass().getName() == "com.stardust.autojs.core.ui.widget.JsListView"
|| view.getClass().getName() == "com.stardust.autojs.core.ui.widget.JsGridView") {
view = decorateList(view);
}
var gestureDetector = new android.view.GestureDetector(context, {
onDown: function (e) {
e = wrapMotionEvent(e);
emit("touch_down", e, view);
return e.consumed;
},
onShowPress: function (e) {
e = wrapMotionEvent(e);
emit("show_press", e, view);
},
onSingleTapUp: function (e) {
e = wrapMotionEvent(e);
emit("single_tap", e, view);
return e.consumed;
},
onScroll: function (e1, e2, distanceX, distanceY) {
e1 = wrapMotionEvent(e1);
e2 = wrapMotionEvent(e2);
emit("scroll", e1, e2, distanceX, distanceY, view);
return e1.consumed || e2.consumed;
},
onLongPress: function (e) {
e = wrapMotionEvent(e);
emit("long_press", e, view);
},
onFling: function (e1, e2, velocityX, velocityY) {
e1 = wrapMotionEvent(e1);
e2 = wrapMotionEvent(e2);
emit("fling", e1, e2, velocityX, velocityY, view);
return e1.consumed || e2.consumed;
}
});
view.setOnTouchListener(function (v, event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
event = wrapMotionEvent(event);
event.consumed = false;
emit("touch", event, view);
return event.consumed;
});
if(!J.instanceOf(view, "android.widget.AdapterView")){
view.setOnLongClickListener(function (v) {
var event = {};
event.consumed = false;
emit("long_click", event, view);
return event.consumed;
});
view.setOnClickListener(function (v) {
emit("click", view);
});
}
view.setOnKeyListener(function (v, keyCode, event) {
event = wrapMotionEvent(event);
emit("key", keyCode, event, v);
return event.consumed;
});
if (typeof (view.setOnCheckedChangeListener) == 'function') {
view.setOnCheckedChangeListener(function (v, isChecked) {
emit("check", isChecked == true ? true : false, view);
});
}
view._id = function (id) {
return ui.findByStringId(view, id);
}
view.click = function (listener) {
if (listener) {
view.setOnClickListener(new android.view.View.OnClickListener(wrapUiAction(listener)));
} else {
view.performClick();
}
}
view.longClick = function (listener) {
if (listener) {
view.setOnLongClickListener(wrapUiAction(listener, false));
} else {
view.performLongClick();
}
}
function emit() {
var args = arguments;
global.__exitIfError__(function () {
//不支持使用apply的原因是rhino会把参数中的primitive变成object
functionApply(view, view.emit, args);
//view.emit.apply(view, args);
});
}
return view;
}
function initListView(list) {
list.setDataSourceAdapter({
getItemCount: function (data) {
@@ -327,31 +216,6 @@ module.exports = function (runtime, global) {
});
}
function decorateList(list) {
list.setOnItemTouchListener({
onItemClick: function(listView, itemView, item, pos){
emit("item_click", item, pos, itemView, listView);
},
onItemLongClick: function(listView, itemView, item, pos){
var event = {};
event.consumed = false;
emit("item_long_click", event, item, pos, itemView, listView);
return event.consumed;
}
});
function emit() {
var args = arguments;
global.__exitIfError__(function () {
//不支持使用apply的原因是rhino会把参数中的primitive变成object
functionApply(list, list.emit, args);
//view.emit.apply(view, args);
});
}
return list;
}
ui.__decorate__ = decorate;
function wrapUiAction(action, defReturnValue) {
if (typeof (activity) != 'undefined') {
return function () { return action(); };
@@ -361,30 +225,6 @@ module.exports = function (runtime, global) {
}
}
function wrapMotionEvent(e) {
e = Object.create(e);
e.consumed = false;
return e;
}
function functionApply(obj, func, args) {
if (args.length == 0)
return func.call(obj);
if (args.length == 1)
return func.call(obj, args[0]);
if (args.length == 2)
return func.call(obj, args[0], args[1]);
if (args.length == 3)
return func.call(obj, args[0], args[1], args[2]);
if (args.length == 4)
return func.call(obj, args[0], args[1], args[2], args[3]);
if (args.length == 5)
return func.call(obj, args[0], args[1], args[2], args[3], args[4]);
if (args.length == 6)
return func.call(obj, args[0], args[1], args[2], args[3], args[4], args[5]);
throw new Error("too many arguments: " + args.length);
}
var proxy = runtime.ui;
proxy.__proxy__ = {
set: function (name, value) {
@@ -392,14 +232,9 @@ module.exports = function (runtime, global) {
},
get: function (name) {
if (!ui[name] && ui.view) {
var cache = ui.__view_cache__[name];
if (cache) {
return cache;
}
cache = ui.findById(name);
if (cache) {
ui.__view_cache__[name] = cache;
return cache;
let v = ui.findById(name);
if (v) {
return v;
}
}
return ui[name];

View File

@@ -0,0 +1,301 @@
/**
* Copyright 2014-2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Since we intend to use the Function constructor.
/* jshint evil: true */
module = (typeof module === 'undefined') ? {} : module;
(function () {
var builtInModules = ["lodash.js"];
var System = java.lang.System;
var Scanner = java.util.Scanner;
var File = java.io.File;
NativeRequire = (typeof NativeRequire === 'undefined') ? {} : NativeRequire;
if (typeof require === 'function' && !NativeRequire.require) {
NativeRequire.require = require;
}
function Module (id, parent, core) {
this.id = id;
this.core = core;
this.parent = parent;
this.children = [];
this.filename = id;
this.loaded = false;
Object.defineProperty(this, 'exports', {
get: function () {
return this._exports;
}.bind(this),
set: function (val) {
Require.cache[this.filename] = val;
this._exports = val;
}.bind(this)
});
this.exports = {};
if (parent && parent.children) parent.children.push(this);
this.require = function (id) {
return Require(id, this);
}.bind(this);
}
Module._load = function _load (file, parent, core, main) {
var module = new Module(file, parent, core);
var body = readFile(module.filename, module.core);
var dir = new File(module.filename).getParent();
var func = new Function('exports', 'module', 'require', '__filename', '__dirname', body);
func.apply(module,
[module.exports, module, module.require, module.filename, dir]);
module.loaded = true;
module.main = main;
return module.exports;
};
Module.runMain = function runMain (main) {
var file = Require.resolve(main);
Module._load(file, undefined, false, true);
};
function Require (id, parent) {
var normalizePath = normalizeName(id);
if(builtInModules.indexOf(normalizePath) >= 0 && !files.exists(normalizePath)){
return NativeRequire.require(normalizePath);
}
if(id === "events"){
return events;
}
if(id.startsWith("http://") || id.startsWith("https://")){
return NativeRequire.require(id);
}
var core;
var native_;
var file = Require.resolve(id, parent);
if (!file) {
if (typeof NativeRequire.require === 'function') {
if (Require.debug) {
System.out.println(['Cannot resolve', id, 'defaulting to native'].join(' '));
}
native_ = NativeRequire.require(id);
if (native_) return native_;
}
System.err.println('Cannot find module ' + id);
throw new ModuleError('Cannot find module ' + id, 'MODULE_NOT_FOUND');
}
if (file.core) {
file = file.path;
core = true;
}
try {
if (Require.cache[file]) {
return Require.cache[file];
} else if (file.endsWith('.js')) {
return Module._load(file, parent, core);
} else if (file.endsWith('.json')) {
return loadJSON(file);
}
} catch (ex) {
if (ex instanceof java.lang.Exception) {
throw new ModuleError('Cannot load module ' + id, 'LOAD_ERROR', ex);
} else {
System.out.println('Cannot load module ' + id + ' LOAD_ERROR');
throw ex;
}
}
}
Require.resolve = function (id, parent) {
var roots = findRoots(parent);
for (var i = 0; i < roots.length; ++i) {
var root = roots[i];
var result = resolveCoreModule(id, root) ||
resolveAsFile(id, root, '.js') ||
resolveAsFile(id, root, '.json') ||
resolveAsDirectory(id, root) ||
resolveAsNodeModule(id, root);
if (result) {
return result;
}
}
return false;
};
Require.root = files.cwd();//System.getProperty('user.dir');
Require.NODE_PATH = undefined;
function findRoots (parent) {
var r = [];
r.push(findRoot(parent));
return r.concat(Require.paths());
}
function parsePaths (paths) {
if (!paths) {
return [];
}
if (paths === '') {
return [];
}
var osName = java.lang.System.getProperty('os.name').toLowerCase();
var separator;
if (osName.indexOf('win') >= 0) {
separator = ';';
} else {
separator = ':';
}
return paths.split(separator);
}
Require.paths = function () {
var r = [];
r.push(java.lang.System.getProperty('user.home') + '/.node_modules');
r.push(java.lang.System.getProperty('user.home') + '/.node_libraries');
if (Require.NODE_PATH) {
r = r.concat(parsePaths(Require.NODE_PATH));
} else {
var NODE_PATH = java.lang.System.getenv().NODE_PATH;
if (NODE_PATH) {
r = r.concat(parsePaths(NODE_PATH));
}
}
// r.push( $PREFIX + "/node/library" )
return r;
};
function findRoot (parent) {
if (!parent || !parent.id) { return Require.root; }
var pathParts = parent.id.split(/[\/|\\,]+/g);
pathParts.pop();
return pathParts.join('/');
}
Require.debug = true;
Require.cache = {};
Require.extensions = {};
require = Require;
Module.require = require;
module.exports = Module;
function loadJSON (file) {
var json = JSON.parse(readFile(file));
Require.cache[file] = json;
return json;
}
function resolveAsNodeModule (id, root) {
var base = [root, 'node_modules'].join('/');
return resolveAsFile(id, base) ||
resolveAsDirectory(id, base) ||
(root ? resolveAsNodeModule(id, new File(root).getParent()) : false);
}
function resolveAsDirectory (id, root) {
var base = [root, id].join('/');
var file = new File([base, 'package.json'].join('/'));
if (file.exists()) {
try {
var body = readFile(file.getCanonicalPath());
var package_ = JSON.parse(body);
if (package_.main) {
return (resolveAsFile(package_.main, base) ||
resolveAsDirectory(package_.main, base));
}
// if no package.main exists, look for index.js
return resolveAsFile('index.js', base);
} catch (ex) {
throw new ModuleError('Cannot load JSON file', 'PARSE_ERROR', ex);
}
}
return resolveAsFile('index.js', base);
}
function resolveAsFile (id, root, ext) {
var file;
if (id.length > 0 && id[0] === '/') {
file = new File(normalizeName(id, ext));
if (!file.exists()) {
return resolveAsDirectory(id);
}
} else {
file = new File([root, normalizeName(id, ext)].join('/'));
}
if (file.exists()) {
return file.getCanonicalPath();
}
}
function resolveCoreModule (id, root) {
var name = normalizeName(id);
var classloader = java.lang.Thread.currentThread().getContextClassLoader();
if (classloader.getResource(name)) {
return { path: name, core: true };
}
}
function normalizeName (fileName, ext) {
if(fileName.endsWith('.json')){
return fileName;
}
var extension = ext || '.js';
if (fileName.endsWith(extension)) {
return fileName;
}
return fileName + extension;
}
function readFile (filename, core) {
var input;
try {
if (core) {
var classloader = java.lang.Thread.currentThread().getContextClassLoader();
input = classloader.getResourceAsStream(filename);
} else {
input = new File(filename);
}
// TODO: I think this is not very efficient
return new Scanner(input).useDelimiter('\\A').next();
} catch (e) {
throw new ModuleError('Cannot read file [' + input + ']: ', 'IO_ERROR', e);
}
}
function ModuleError (message, code, cause) {
this.code = code || 'UNDEFINED';
this.message = message || 'Error loading module';
this.cause = cause;
}
// Helper function until ECMAScript 6 is complete
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function (suffix) {
if (!suffix) return false;
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
ModuleError.prototype = new Error();
ModuleError.prototype.constructor = ModuleError;
}());