/* * 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 coop.jsMindCallBack_(type, body) } Jm.coop = function (_jm) { if (!this._jm) { this._jm = _jm _jm.add_event_listener(jsMindCallBackFacade) // 设置属性 this.coop_api_url = this.notNull(_jm.options.coop_api_url) === null ? _jm.options.coop_api_url : '/jsmind_op' 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() } // this.timerPush() } } Jm.coop.prototype = { // jsMind 对象 _jm: null, // 后台api 接口地址 coop_api_url: null, // 标记当前客户端(同一个IP可能打开多个浏览器,需要用client_id区分) client_id: null, // 当前脑图ID coop_mind_id: null, // 默认操作日志ID coop_mind_log_id: 0, // 定时备份数据 timer_push_task: null, // 定时获得变化 timer_fetch_task: null, // 清理定时任务 clear_timer: function () { 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_id': 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) { $w.coop = new Jm.coop(_jm) coop.jm = _jm _jm.coop = coop }) Jm.register_plugin(plugin) })(window, 'jsMind')