[layui-layer] Add types for layui-layer (#42976)

* layui-layer

* Update index.d.ts
This commit is contained in:
943297456 2020-03-13 03:08:50 +08:00 committed by GitHub
parent 043d926d8b
commit 94590b4029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 922 additions and 0 deletions

540
types/layui-layer/index.d.ts vendored Normal file
View File

@ -0,0 +1,540 @@
// Type definitions for layui-layer 1.0
// Project: https://github.com/sentsin/layer
// Definitions by: Zhuo Ning <https://github.com/943297456>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/// <reference types="jquery" />
/**
* @see https://www.layui.com/doc/modules/layer.html
*/
declare const layer: layer.Layer;
declare namespace layer {
type Index = number;
type Selector = string;
interface Types {
/** 信息框 */
dialog: 0;
/** 页面层 */
page: 1;
/** iframe层 */
iframe: 2;
/** 加载层 */
loading: 3;
/** tips层 */
tips: 4;
}
/**
*
*/
interface Options {
/**
*
* @default 0
*/
type?: Types[keyof Types];
/**
*
* @default '信息'
* @example
* '我是标题'
* ['文本', 'font-size:18px;']
* false //不显示标题栏
*/
title?: string | false | [string, string];
/**
*
* @default ''
*/
content?: string | HTMLElement | JQuery | [string, string];
/**
*
* @default ''
*/
skin?: string;
/**
*
* @default 'auto'
* @example
* '500px' //定义宽度,高度自适应
* ['500px', '300px'] //定义宽高
*/
area?: string | [string, string];
/**
*
* @default 'auto'
* @example
* 'auto' //默认坐标,即垂直水平居中
* '100px' //只定义top坐标水平保持居中
* ['100px', '50px'] //同时定义top、left坐标
* 't' //快捷设置顶部坐标
* 'r' //快捷设置右边缘坐标
* 'b' //快捷设置底部坐标
* 'l' //快捷设置左边缘坐标
* 'lt' //快捷设置左上角
* 'lb' //快捷设置左下角
* 'rt' //快捷设置右上角
* 'rb' //快捷设置右下角
*/
offset?: string | [string, string];
/**
*
*
* @description 0-60-2
*/
icon?: number;
/**
*
* @default '确认'
* @description btn默认是一个确认按钮tips层则无效
*/
btn?: string | string[];
/**
*
* @default 'r'
* @example
* 'l' //按钮左对齐
* 'c' //按钮居中对齐
* 'r' //按钮右对齐。默认值,不用设置
*/
btnAlign?: "l" | "c" | "r";
/**
*
* @description layer提供了两种风格的关闭按钮12则closeBtn: 0
* @default 1
*/
closeBtn?: 0 | 1 | 2;
/**
*
* @default 0.3
* @example [0.8, '#393D49'] //透明度、颜色
*/
shade?: number | false | [number, string];
/**
*
* @description shade是存在的shadeClose来控制点击弹层外区域关闭
* @default false
*/
shadeClose?: boolean;
/**
*
* @default 0 //不会自动关闭
*/
time?: number;
/**
*
* @description iframe层模式
* @default ''
*/
id?: string;
/**
*
* @default 0
* @since 3.0
* @example
* -1 //关闭动画
* 0 //平滑放大。默认
* 1 //从上掉落
* 2 //从最底部往上滑入
* 3 //从左滑入
* 4 //从左翻滚
* 5 //渐显
* 6 //抖动
*/
anim?: -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6;
/**
*
* @default true
* @since 3.0.3
*/
isOutAnim?: boolean;
/**
*
* @default false
* @description 该参数值对type:1和type:2
*/
maxmin?: boolean;
/**
*
* @default true
* @description
*/
fixed?: boolean;
/**
*
* @default true
* @description loadingtips层无效
*/
resize?: boolean;
/**
*
* @param layero DOM对象
*/
resizing?(layero: JQuery): void;
/**
*
* @default true
*/
scrollbar?: boolean;
/**
*
* @default 360
* @description `area: 'auto'`maxWidth的设定才有效
*/
maxWidth?: number;
/**
*
* @since 3.1.0
* @description maxHeight的设定才有效
*/
maxHeight?: number;
/**
*
* @default 19891014
* @description
*/
zIndex?: number;
/**
*
* @default '.layui-layer-title'
*/
move?: Selector | false | JQuery;
/**
*
* @default false
*/
moveOut?: boolean;
/**
*
* @param this
* @param layero DOM对象
*/
moveEnd?(this: this, layero: JQuery): void;
/**
* tips方向和颜色
* @default 2
* @description tips层的私有参数
* @example
* 1 //上
* 2 //右
* 3 //下
* 4 //左
* [1, '#c00'] //方向,颜色
*/
tips?: number | [number, string];
/**
* tips
* @default false
* @description tips层
*/
tipsMore?: boolean;
/**
*
* @param layero DOM
* @param index
*/
success?(layero: JQuery, index: Index): void;
/**
*
* @param index
* @param layero DOM对象
* @description yes回调`close(index)`
*/
yes?(index: Index, layero: JQuery): void;
//#region 按钮1的回调是yes而从按钮2开始则回调为btn2: function(){},以此类推。
btn2?(index: Index, layero: JQuery): false | void;
btn3?(index: Index, layero: JQuery): false | void;
btn4?(index: Index, layero: JQuery): false | void;
btn5?(index: Index, layero: JQuery): false | void;
btn6?(index: Index, layero: JQuery): false | void;
btn7?(index: Index, layero: JQuery): false | void;
btn8?(index: Index, layero: JQuery): false | void;
btn9?(index: Index, layero: JQuery): false | void;
//#endregion
/**
*
* @param index
* @param layero DOM对象
* @description `return false`
*/
cancel?(index: Index, layero: JQuery): false | void;
/**
*
* @description end都会执行
*/
end?(): void;
/**
*
* @param layero DOM
*/
full?(layero: JQuery): void;
/**
*
* @param layero DOM
* @description `return false`
*/
min?(layero: JQuery): false | void;
/**
*
* @param layero DOM
*/
restore?(layero: JQuery): void;
}
interface ConfigOptions extends Options {
/**
* js所在的目录
*/
path?: string;
/**
* css皮肤
*/
extend?: string | string[];
}
interface PromptOptions extends Options {
/**
* 012
*/
formType?: 0 | 1 | 2;
/**
*
* @default ''
*/
value?: string;
/**
*
* @default 500
*/
maxlength?: number;
}
interface TabOptions extends Options {
tab: TabItem[];
/**
*
* @param this
* @param tabIndex
*/
change?(this: this, tabIndex: number): void;
}
interface TabItem {
title: string;
content: string;
}
interface PhotosOptions extends Options {
photos: PhotosData | Selector;
/**
*
* @param pic
* @param layero
*/
tab?(pic: PhotosDataItem, layero: JQuery): void;
}
interface PhotosData {
/** 相册标题 */
title: string;
/** 相册id */
id: number;
/**
*
* @default 0
*/
start?: number;
/** 相册包含的图片 */
data: PhotosDataItem[];
}
interface PhotosDataItem {
/** 图片名 */
alt: string;
/** 图片id */
pid?: number;
/** 原图地址 */
src: string;
/** 缩略图地址 */
thumb: string;
}
namespace callback {
type Yes = NonNullable<Options["yes"]>;
type Cancel = NonNullable<Options["cancel"]>;
type End = NonNullable<Options["end"]>;
type Prompt = (value: string, index: Index, elem: JQuery) => void;
}
interface Layer {
/**
*
* @param options
*/
config(options: ConfigOptions): this;
/**
*
* @param callback
*/
ready(callback: () => void): this;
/**
*
* @param options
*/
open(options?: Options): Index;
/**
*
* @param content
* @param options
* @param yes
*/
alert(content?: string, options?: Options, yes?: callback.Yes): Index;
alert(content: string, yes: callback.Yes): Index;
alert(options: Options, yes?: callback.Yes): Index;
alert(yes: callback.Yes): Index;
/**
*
* @param content
* @param options
* @param yes
* @param cancel
*/
confirm(content?: string, options?: Options, yes?: callback.Yes, cancel?: callback.Cancel): Index;
confirm(content: string, yes: callback.Yes, cancel?: callback.Cancel): Index;
confirm(options: Options, yes?: callback.Yes, cancel?: callback.Cancel): Index;
confirm(yes: callback.Yes, cancel?: callback.Cancel): Index;
/**
*
* @param content
* @param options
* @param end
*/
msg(content: string, options?: Options, end?: callback.End): Index;
msg(content: string, end: callback.End): Index;
/**
*
* @param icon
* @param options
*/
load(icon?: 0 | 1 | 2, options?: Options): Index;
/**
* tips层
* @param content
* @param follow
* @param options
*/
tips(content: string, follow: Selector | HTMLElement, options?: Options): Index;
/**
*
* @param index
*/
close(index: Index): void;
/**
*
* @param type
*/
closeAll(type?: keyof Types): void;
/**
*
* @param index
* @param cssStyle
* @description loading层和tips层无效index为层的索引cssStyle允许你传入任意的css属性
*/
style(index: Index, cssStyle: Partial<CSSStyleDeclaration>): void;
/**
*
* @param title
* @param index
*/
title(title: string, index: Index): void;
/**
* iframe页的DOM
* @param selector
* @param index
*/
getChildFrame(selector: string, index: Index): JQuery;
/**
* iframe层的索引
* @param windowName
*/
getFrameIndex(windowName: string): Index;
/**
* iframe层自适应
* @param index
*/
iframeAuto(index: Index): void;
/**
* iframe url
* @param index
* @param url
*/
iframeSrc(index: Index, url: string): void;
/**
*
*/
setTop(layero: JQuery): void;
/**
*
* @param index
*/
full(index: Index): void;
/**
*
* @param index
*/
min(index: Index): void;
/**
*
* @param index
*/
restore(index: Index): void;
/**
*
* @param options
* @param yes
*/
prompt(options?: PromptOptions, yes?: callback.Prompt): Index;
prompt(yes: callback.Prompt): Index;
/**
* tab层
* @param options
*/
tab(options: TabOptions): Index;
/**
*
* @param options
*/
photos(options: PhotosOptions): Index;
/**
*
*/
index: Index;
zIndex: number;
/**
*
*/
v: string;
}
interface Options {
/**
* @deprecated v3.0
* @description
*/
moveType?: number;
/**
* @deprecated v3.0
* @see {@link #fixed}
*/
fix?: boolean;
/**
* @deprecated v3.0
* @see {@link #anim}
*/
shift?: number;
}
}

View File

@ -0,0 +1,357 @@
declare let index: layer.Index;
interface Window {
readonly layer: layer.Layer;
}
/********************************************************************************/
// 如果是页面层
layer.open({
type: 1,
content: '传入任意的文本或html' // 这里content是一个普通的String
});
layer.open({
type: 1,
content: $('#id') // 这里content是一个DOM
});
// Ajax获取
$.post('url', {}, (str) => {
layer.open({
type: 1,
content: str // 注意如果str是object那么需要字符拼接。
});
});
// 如果是iframe层
layer.open({
type: 2,
content: 'http:// sentsin.com' // 这里content是一个URL如果你不想让iframe出现滚动条你还可以content: ['http:// sentsin.com', 'no']
});
// 如果是用layer.open执行tips层
layer.open({
type: 4,
content: ['内容', '#id'] // 数组第二项即吸附元素选择器或者DOM
});
/********************************************************************************/
// 单个使用
layer.open({
skin: 'demo-class'
});
// 全局使用。即所有弹出层都默认采用但是单个配置skin的优先级更高
layer.config({
skin: 'demo-class'
});
/********************************************************************************/
// eg1
layer.alert('酷毙了', { icon: 1 });
// eg2
layer.msg('不开心。。', { icon: 5 });
// eg3
layer.load(1); // 风格1的加载
/********************************************************************************/
// eg1
layer.confirm('纳尼?', {
btn: ['按钮一', '按钮二', '按钮三'] // 可以无限个按钮
, btn3(index, layero) {
// 按钮【按钮三】的回调
}
}, (index, layero) => {
// 按钮【按钮一】的回调
}, (index) => {
// 按钮【按钮二】的回调
});
// eg2
layer.open({
content: 'test'
, btn: ['按钮一', '按钮二', '按钮三']
, yes(index, layero) {
// 按钮【按钮一】的回调
}
, btn2(index, layero) {
// 按钮【按钮二】的回调
return false; // 开启该代码可禁止点击该按钮关闭
}
, btn3(index, layero) {
// 按钮【按钮三】的回调
// return false 开启该代码可禁止点击该按钮关闭
}
, cancel() {
// 右上角关闭回调
// return false 开启该代码可禁止点击该按钮关闭
}
});
/********************************************************************************/
layer.open({
content: '测试回调',
success(layero, index) {
console.log(layero, index);
}
});
/********************************************************************************/
layer.open({
content: '测试回调',
yes(index, layero) {
// do something
layer.close(index); // 如果设定了yes回调需进行手工关闭
}
});
/********************************************************************************/
layer.config({
path: '/res/layer/' // layer.js所在的目录可以是绝对目录也可以是相对目录
});
// 这样的话layer就会去加载一些它所需要的配件比如css等。
// 当然你即便不用seajs或者requirejs也可以通过上述方式设定路径
/********************************************************************************/
layer.config({
path: '/res/layer/' // layer.js所在的目录可以是绝对目录也可以是相对目录
});
/********************************************************************************/
layer.config({
shift: 1, // 默认动画风格
skin: 'layui-layer-molv' // 默认皮肤
});
// 除此之外extend还允许你加载css。这对于layer的第三方皮肤有极大的帮助
layer.config({
extend: [
'skin/layer-ext-myskin/style.css' // layer-ext-myskin即是你拓展的皮肤文件
]
});
// 扩展css的规范您必须在你扩展中的css文件加上这段
/********************************************************************************/
// 页面一打开就执行弹层
layer.ready(() => {
layer.msg('很高兴一开场就见到你');
});
/********************************************************************************/
index = layer.open({
content: 'test'
});
// 拿到的index是一个重要的凭据它是诸如layer.close(index)等方法的必传参数。
/********************************************************************************/
// eg1
layer.alert('只想简单的提示');
// eg2
layer.alert('加了个图标', { icon: 1 }); // 这时如果你也还想执行yes回调可以放在第三个参数中。
// eg3
layer.alert('有了回调', (index) => {
// do something
layer.close(index);
});
/********************************************************************************/
// eg1
layer.confirm('is not?', { icon: 3, title: '提示' }, (index) => {
// do something
layer.close(index);
});
// eg2
layer.confirm('is not?', (index) => {
// do something
layer.close(index);
});
/********************************************************************************/
// eg1
layer.msg('只想弱弱提示');
// eg2
layer.msg('有表情地提示', { icon: 6 });
// eg3
layer.msg('关闭后想做些什么', () => {
// do something
});
// eg
layer.msg('同上', {
icon: 1,
time: 2000 // 2秒关闭如果不配置默认是3秒
}, () => {
// do something
});
/********************************************************************************/
// eg1
index = layer.load();
// eg2
index = layer.load(1); // 换了种风格
// eg3
index = layer.load(2, { time: 10 * 1000 }); // 又换了种风格并且设定最长等待10秒
// 关闭
layer.close(index);
/********************************************************************************/
// eg1
layer.tips('只想提示地精准些', '#id');
// eg 2
$('#id').on('click', function() {
const that = this;
layer.tips('只想提示地精准些', that); // 在元素的事件回调体中follow直接赋予this即可
});
// eg 3
layer.tips('在上面', '#id', {
tips: 1
});
/********************************************************************************/
// 当你想关闭当前页的某个层时
index = layer.open();
index = layer.alert();
index = layer.load();
index = layer.tips("", "#id");
// 正如你看到的每一种弹层调用方式都会返回一个index
layer.close(index); // 此时你只需要把获得的index轻轻地赋予layer.close即可
// 当你在iframe页面关闭自身时
index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
parent.layer.close(index); // 再执行关闭
/********************************************************************************/
layer.closeAll(); // 疯狂模式,关闭所有层
layer.closeAll('dialog'); // 关闭信息框
layer.closeAll('page'); // 关闭所有页面层
layer.closeAll('iframe'); // 关闭所有的iframe层
layer.closeAll('loading'); // 关闭加载层
layer.closeAll('tips'); // 关闭所有的tips层
/********************************************************************************/
// 重新给指定层设定width、top等
layer.style(index, {
width: '1000px',
top: '10px'
});
/********************************************************************************/
layer.open({
type: 2,
content: 'test/iframe.html',
success(layero, index) {
const body = layer.getChildFrame('body', index);
// const iframeWin = window[layero.find('iframe')[0]['name']]; // 得到iframe页的窗口对象执行iframe页的方法iframeWin.method();
console.log(body.html()); // 得到iframe页的body内容
body.find('input').val('Hi我是从父页来的');
}
});
/********************************************************************************/
// 假设这是iframe页
index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
parent.layer.close(index); // 再执行关闭
/********************************************************************************/
layer.iframeSrc(index, 'http:// sentsin.com');
/********************************************************************************/
// 通过这种方式弹出的层,每当它被选择,就会置顶。
layer.open({
type: 2,
shade: false,
area: '500px',
maxmin: true,
content: 'http:// www.layui.com',
zIndex: layer.zIndex, // 重点1
success(layero) {
layer.setTop(layero); // 重点2
}
});
/********************************************************************************/
layer.config({
extend: 'extend/layer.ext.js'
});
/********************************************************************************/
// prompt层新定制的成员如下
let opt = {
formType: 1, // 输入框类型支持0文本默认1密码2多行文本
value: '', // 初始时的值,默认空字符
maxlength: 140, // 可输入文本的最大长度默认500
};
// 例子1
layer.prompt((value, index, elem) => {
alert(value); // 得到value
layer.close(index);
});
// 例子2
layer.prompt({
formType: 2,
value: '初始值',
title: '请输入值'
}, (value, index, elem) => {
alert(value); // 得到value
layer.close(index);
});
/********************************************************************************/
layer.tab({
area: ['600px', '300px'],
tab: [{
title: 'TAB1',
content: '内容1'
}, {
title: 'TAB2',
content: '内容2'
}, {
title: 'TAB3',
content: '内容3'
}]
});
/********************************************************************************/
$.getJSON('/jquery/layer/test/photos.json', (json) => {
layer.photos({
photos: json
, shift: 5 // 0-6的选择指定弹出图片动画类型默认随机
});
});
let photos: layer.PhotosData = {
title: "", // 相册标题
id: 123, // 相册id
start: 0, // 初始显示的图片序号默认0
data: [ // 相册包含的图片,数组格式
{
alt: "图片名",
pid: 666, // 图片id
src: "", // 原图地址
thumb: "" // 缩略图地址
}
]
};
/********************************************************************************/
// HTML示例
`<div id="layer-photos-demo" class="layer-photos-demo">
<img layer-pid="图片id可以不写" layer-src="大图地址" src="缩略图" alt="图片名">
<img layer-pid="图片id可以不写" layer-src="大图地址" src="缩略图" alt="图片名">
</div>`;
`<script>`;
// 调用示例
layer.photos({
photos: '#layer-photos-demo'
, anim: 5 // 0-6的选择指定弹出图片动画类型默认随机请注意3.0之前的版本用shift参数
});
`</script>`;
/********************************************************************************/
layer.photos({
photos: "json/选择器",
tab(pic, layero) {
console.log(pic); // 当前图片的一些信息
}
});
/********************************************************************************/

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"layui-layer-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }