lar/ljs

Laravel 应用 JS 核心库

维护者

详细信息

github.com/bfg-s/ljs

主页

源代码

问题

安装次数: 17,735

依赖: 4

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 1

开放问题: 0

语言:JavaScript

类型:lar-library


README

执行模式

模式 "exec"

/**
 * Method for one or multiple call.
 * 
 * @param data {string|Array|Object} executed data
 * @param params {array|any} executed params for one call
 * @param storage_data {Object} Executor storage (this.storage)
 * @returns {null}
 */
ljs.exec(data, params = null, storage_data = {});

/**
 * Execute helper.
 * 
 * @param command {string|Array|Object} executed data
 * @param params {any} executed params for one call
 * @returns {*}
 */
exec(command, ...params);

/**
 * String Prototypes
 */
"toast:error".exec('Error message');
"toast:error".exec('Error message', 'Error');

//Send param to executor
"message".to('toast:success');

/**
 * Array Prototypes
 */
//Map executors for params
["toast::success","console.log"].exec('New message');
["toast::success","console.log"].exec('message text', 'New message');

//To map. Each element of the array as a new parameter for the next call.
["You have 5 new followers","New message from user"].tom('toast:success');
[["5 new", "Followers"],["message text","Message"]].tom('toast:success');

//Parameters to pass to the function.
["message", "title"].to('toast:success'); 
//same 
[["message", "title"]].tom('toast:success');

示例

单次执行
ljs.exec('toast::success', 'User successful auth!');
ljs.exec('toast::success', ['Success auth!', 'User']);
// or
exec('toast::success', 'User success auth!');
exec('toast::success', 'Success auth!', 'User');
多次执行
ljs.exec({
    'toast::success': 'User successful auth!',
    'toast::warning': ['Needle enter a profile data!', 'Profile']
});
// or
exec({...})

模式 "call" 单行字符串

/**
 * Call mode
 * 
 * @param command {string} executed data
 * @param storage
 * @returns {undefined|*}
 */
ljs.call(command, storage = {});

/**
 * Call helper
 * 
 * @param command {string} executed data
 * @param storage
 * @returns {*}
 */
call(command, storage = {});

/**
 * String Prototypes
 */
"toast:success(Success auth)".call();

/**
 * Array Prototypes
 */
["toast:success(Success auth)", "toast:warning(Enter a phone number, Profile)"].call()

单行调用语法

toast:error(Some error text, Error title, {"progressBar": true})
// or
location.pathname

括号内是参数的正文。参数之间用逗号分隔,注意仅在需要添加下一个参数时才在参数中使用逗号符号。

示例

ljs.call("toast:success(Success auth)");
// or 
call("toast:success(Success auth)");

模式 "parse" 单行字符串

/**
 * String parse
 *
 * @param str
 * @param storage
 * @returns {*}
 */
ljs.parse(str, storage = {});

/**
 * String Prototypes
 */
"My location is >>location.href".parse();

/**
 * Array Prototypes
 */
["toast:success(Success auth)", "toast:warning(Enter a phone number, Profile)"].call()

要指示调用字符串格式化函数,必须在调用之前指定输入字符 >>

执行器扩展

1 - 创建您的 js 执行器文件 'resources/js/executors/Root.js'

'use strict';

class Root extends Executor {

    /**
     * For call without method.
     * (don't required)
     * 
     * @param text
     * @public
     */
    __invoke (text = "My executor") {
        
        //this.storage - Executor storage

        text.exec('toast:success');
        
        return text;
    }
    
    /**
     * Magic method "How PHP".
     * (don't required)
     * 
     * @param $name
     * @param $arguments
     * @public
     */
    __call ($name, $arguments) {
        //
    }

    /**
     * Class name for call.
     * If don't set, name becomes equal to class name.
     * (don't required)
     *
     * @returns {null|string}
     * @private
     */
    static __name () {

        return "root";
    }

    /**
     * Array of aliases from object.
     * (don't required)
     *
     * @returns {{}}
     */
    static __aliases () {

        return [];
    }

    /**
     * Individual method name getter.
     * (don't required)
     *
     * @returns {string}
     * @private
     */
    static __individual_method () {

        return "__invoke";
    }
}

module.exports = Message;

2 - 在 LJS 中注入扩展

ljs.regExec(require('./executors/Root'));

3 - 调用新的扩展

call('root');
call('root', 'I Love Shaurma');
"I created >>root".parse();

管道

  1. && - 将全局传递的参数发送到下一次调用。
  2. & - 将 undefined 发送到下一次调用。
  3. # - 将 null 发送到下一次调用。
  4. * - 将前一次执行的结果发送到下一次调用。
  5. > - 将全局参数与上次结果一起发送。
  6. [0-9]> - 从行首发送结果编号。
  7. [0-9]>> - 从行尾发送结果编号。

示例

"location.origin & location.pathname > toast".exec();
// or
"document.title > confirm".exec();

默认 LJS 执行器

警报系统

扩展文档 https://github.com/CodeSeven/toastr

errors [msg, msg,...]
message [msg]

显示成功消息

toast:success [message]
toast:success [message, title = null, options = {}]
toast:success [{'text': 'message text', 'title': 'message title', 'options': {}}]
//or
toast::success //public method

显示错误消息

toast:error [message]
toast:error [message, title = null, options = {}]
toast:error [{'text': 'message text', 'title': 'message title', 'options': {}}]
//or
toast::error //public method

显示信息消息

toast:info [message]
toast:info [message, title = null, options = {}]
toast:info [{'text': 'message text', 'title': 'message title', 'options': {}}]
//or
toast::info //public method

显示警告消息

toast:warning [message]
toast:warning [message, title = null, options = {}]
toast:warning [{'text': 'message text', 'title': 'message title', 'options': {}}]
//or
toast::warning //public method

显示 toast 消息

toast [message]
toast [message, title = null, options = {}, type = "info"]
toast [{'text': 'message text', 'title': 'message title', 'options': {}}]
示例
ljs.exec('errors', ['error1', 'error2']);
ljs.exec('message', 'message text');
ljs.exec('toast:success', 'success static message');
ljs.exec('toast::success', 'success message');
ljs.exec('toast::success', ['message', 'title', {'progressBar': true}]);
ljs.exec('toast::success', {'text': 'message', 'title': 'title', 'options': {'progressBar': true}});

文档

公共方法

LJS 事件分发

doc::dispatch_event [event]

设置文档标题

doc::title [title]

设置文档位置

doc::location [location]
doc::set_location [location]

重新加载文档

doc::reload [null]

JQuery 装饰器

等同于 $('$element').show()

jq::show [$element]

等同于 $('$element').hide()

jq::hide [$element]

等同于 $('$element').html($data)

jq::html [$element, $data = '']

等同于 $('$element').replaceWith($data)

jq::replace [$element, $data = '']

等同于 $('$element').append($data)

jq::append [$element, $data = '']

等同于 $('$element').submit()

jq::submit [$element]

设置属性

jq::attribute [$element, $attribute_name, $attribute_value]
jq::attribute [$element, {'attribute_name': 'attribute_value',...}]

//Or on with element
jq::attribute [$attribute_name, $attribute_value]
jq::attribute [{'attribute_name': 'attribute_value',...}]

全局 eval

jq::eval [data]
jq::globalEval [data]

Swal 警报

成功警报

swal::success [text]
swal::success [title, text]
alert::success [text]
alert::success [title, text]

警告警报

swal::warning [text]
swal::warning [title, text]
alert::warning [text]
alert::warning [title, text]

错误警报

swal::error [text]
swal::error [title, text]
alert::error [text]
alert::error [title, text]

信息警报

swal [text]
swal [title, text]
alert [text]
alert [title, text]
swal::info [text]
swal::info [title, text]
alert::info [text]
alert::info [title, text]

问题警报

swal::question [text]
swal::question [title, text]
alert::question [text]
alert::question [title, text]

定时器

创建超时定时器

timer::out {1000: ExecutedData, 2000: ExecutedData,...}
1000 - ms
2000 - ms

创建定时器间隔

timer::interval [name, ExecutedData, ms = 1000]

清除定时器间隔

timer::clear [name]
示例
//Create interval in 2 second and clear him by timeout 6 sec
ljs.exec({
    'timer::interval': [
        'my_timer', {'toast::success': 'Timer tik!'}, 2000
    ], 
    'timer::out': {
        6000: {'timer::clear': 'my_timer'}
    }
});

全局变量

设置变量

var::set [path, value, save = false]

合并全局变量

var::merge [{path: value,...}, save = false]

设置并保存变量(在本地存储中保存)

var::save [path, value]

仅忘记变量(从本地存储中删除)

var::forgot [path, value]

取消设置变量

var::unset [path]

获取变量

var::get [path = null]

获取变量名称(键)

var::keys [null]