Files
Nsfocus_test_ai_project/static/js/jsmind.coop.js
T
2022-02-14 21:46:12 +08:00

206 lines
7.2 KiB
JavaScript

/*
* Released under BSD License
* Copyright (c) 2019-2020 Allen_sun_js@hotmail.com
*
* Project Home:
* https://github.com/allensunjian
*/
(function ($w, temp) {
const Jm = $w[temp]
if (!Jm) return
function jsMindCallBackFacade(type, body) {
const coop = $w.coop
if (coop) {
coop.jsMindCallBack_(type, body)
} else {
console.error('coop is undefined')
}
}
Jm.coop = function (_jm) {
this._init(_jm)
}
Jm.coop.prototype = {
// jsMind 对象
_jm: null,
// 后台api 接口地址
coop_api_url: null,
// websocket 地址
coop_ws_url: null,
// 标记当前客户端(同一个IP可能打开多个浏览器,需要用client_id区分)
client_id: null,
// 当前脑图ID
coop_mind_id: null,
// 上一次操作日志ID
coop_mind_log_last_id: 0,
// 默认操作日志ID
coop_mind_log_id: 0,
// 定时备份数据
timer_push_task: null,
// 定时获得变化
timer_fetch_task: null,
// web_socket
web_socket: null,
// init
_init: function (_jm) {
if (!this._jm) {
this._jm = _jm
_jm.add_event_listener(jsMindCallBackFacade)
// 初始化属性
this._init_properties(_jm)
// 初始化websocket
this._init_ws()
}
},
_init_properties: function (_jm) {
// 设置属性
this.coop_api_url = _jm.options.coop_api_url
this.coop_ws_url = _jm.options.coop_ws_url
this.client_id = Jm.util.uuid.newid()
const coop_mind_id = _jm.options.coop_mind_id
if (this.notNull(coop_mind_id)) {
this.coop_mind_id = coop_mind_id
// this.timerFetch()
}
},
// 初始化websocket
_init_ws: function () {
if (!this.web_socket) {
const ws = new WebSocket(this.coop_ws_url + '?_' + this.coop_mind_id)
this.web_socket = ws
//申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头
ws.onopen = function () {
//当WebSocket创建成功时,触发onopen事件
console.log("open");
ws.send("hello"); //将消息发送到服务端
}
ws.onmessage = function (e) {
//当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
console.log(e.data);
}
ws.onclose = function (e) {
//当客户端收到服务端发送的关闭连接请求时,触发onclose事件
console.log("close");
this.web_socket = null
}
ws.onerror = function (e) {
//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
console.log('error');
this.web_socket = null
}
}
},
// 清理定时任务
clear_timer: function () {
console.log('clear_timer')
if (this.timer_push_task) {
$w.clearInterval(this.timer_push_task)
}
if (this.timer_fetch_task) {
$w.clearInterval(this.timer_fetch_task)
}
},
/**
* 是否为空
* @param obj
*/
notNull: function (obj) {
return obj !== null && obj !== undefined
},
isNull: function (obj) {
return !this.notNull(obj)
},
/**
* 构建公共参数
*/
buildCommonParam: function () {
return {'client_id': this.client_id, 'log_uuid': Jm.util.uuid.newid()}
},
/**
* 回调
* # {evt:'add_node',data:[parent_node.id,nodeid,topic,data],node:nodeid}
* # {evt:'insert_node_before',data:[beforeid,nodeid,topic,data],node:nodeid}
* # {evt:'insert_node_after',data:[afterid,nodeid,topic,data],node:nodeid}
* # {evt:'remove_node',data:[nodeid],node:parentid}
* # {evt:'update_node',data:[nodeid,topic],node:nodeid}
* # {evt:'move_node',data:[nodeid,beforeid,parentid,direction],node:nodeid}
* @param type
* @param body
* @private
*/
jsMindCallBack_: function (type, body) {
const evt = body.evt
const data = body.data
const param = this.buildCommonParam()
param.type = type
if (type === 1 || (evt && evt.length > 0)) {
if (type === 1) {
// 新建脑图
param.name = data[0].meta.name
} else if (this.notNull(evt) && evt.length > 0) {
// 脑图编辑
param.evt = evt
param.data = data.join(';')
param.node = body.node
param.coop_mind_id = this.coop_mind_id
}
// ajax
const me = this
/*Jm.util.ajax.post(this.coop_api_url, param, function (resp) {
if (type === 1) {
// 新建
me.coop_mind_id = resp.coop_mind_id
// this.timerFetch()
// this.timerPush()
} else {
// 操作
me.coop_mind_log_id = resp.coop_mind_log_id
}
})*/
}
},
/**
* 定时向db获取
*/
timerFetch: function () {
// 根据最大的日志Id 获取
},
/**
* 每隔n秒钟同步一次数据到db
*/
timerPush: function () {
// 把操作日志最大的data 持久化到数据库
const me = this
if (this.isNull(me.timer_push_task)) {
me.timer_push_task = setInterval(function () {
const param = {
'data': Jm.util.json.json2string(me._jm.get_data('node_array').data),
'client_id': me.client_id,
'coop_mind_id': me.coop_mind_id,
'coop_mind_log_id': me.coop_mind_log_id,
'type': 'snapshot'
}
console.log(me._jm.get_data('node_array').meta.name)
Jm.util.ajax.post(me.coop_api_url, param, function (resp) {
// console.log(resp)
})
}, 5000)
}
}
}
var plugin = new Jm.plugin('coop', function (_jm) {
// _jm 已经实例化好的jm对象
$w.coop = new Jm.coop(_jm)
// coop 对象 与 _jm 对象互相设置关联关系
coop.jm = _jm
_jm.coop = coop
})
Jm.register_plugin(plugin)
})(window, 'jsMind')