学习笔记之--详解im-chat源码

写在前面

因最近项目需要制作一个聊天界面,对比后感觉MUI源码内的im-chat.html文件对各种情况的处理比较的全面,因此,将整个页面的逻辑从头到尾理了一遍,希望有需求的小伙伴可以少走弯路,通过这个模板,可以根据我们自己需要的功能进行定制,如果不是很清楚(用词不当)的地方,还请小伙伴们提出来,做相关修改,谢谢!
ps:理这种逻辑性的东西需要耐心,所以希望小伙伴能够静下心来慢慢理,任何代码都是根据需求实现。所以,可以结合实际操作来看相关模块。最后,求一波关注,哈哈!

相关引用

这个聊天框架基于MUI开发,正常引入相关文件后,还需引入如下文件:

  1. 图片预览的mui.imageviewer.css样式文件,
  2. 处理图片浏览的mui.imageViewer.js文件,
  3. 模板渲染的arttmpl.js文件;

代码详解

<body contextmenu="return false;">
        <header class="mui-bar mui-bar-nav">
            <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
            <h1 class="mui-title">chat (聊天窗口)</h1>
        </header>
        <pre id='h'></pre>
        <script id='msg-template' type="text/template">
            <% for(var i in record){ var item=record[i]; %>
                <div class="msg-item <%= (item.sender=='self'?' msg-item-self':'') %>" msg-type='<%=(item.type)%>' msg-content='<%=(item.content)%>'>
                    <% if(item.sender=='self' ) { %>
                        <i class="msg-user mui-icon mui-icon-person"></i>
                    <% } else { %>
                        <img class="msg-user-img" src="../images/logo.png" alt="" />
                    <% } %>
                    <div class="msg-content">
                        <div class="msg-content-inner">
                            <% if(item.type=='text' ) { %>
                                <%=( item.content|| '&nbsp;&nbsp;') %>
                            <% } else if(item.type=='image' ) { %>
                                <img class="msg-content-image" src="<%=(item.content)%>" style="max- 100px;" />
                            <% } else if(item.type=='sound' ) { %>
                                <span class="mui-icon mui-icon-mic" style="font-size: 18px;font-weight: bold;"></span>
                                <span class="play-state">点击播放</span>
                            <% } %>
                        </div>
                        <div class="msg-content-arrow"></div>
                    </div>
                    <div class="mui-item-clear"></div>
                </div>
            <% } %>
        </script>
        <div class="mui-content">
            <div id='msg-list'>
            </div>
        </div>
        <footer>
            <div class="footer-left">
                <i id='msg-image' class="mui-icon mui-icon-camera" style="font-size: 28px;"></i>
            </div>
            <div class="footer-center">
                <textarea id='msg-text' type="text" class='input-text'></textarea>
                <button id='msg-sound' type="button" class='input-sound' style="display: none;">按住说话</button>
            </div>
            <label for="" class="footer-right">
                <i id='msg-type' class="mui-icon mui-icon-mic"></i>
            </label>
        </footer>
        <div id='sound-alert' class="rprogress">
            <div class="rschedule"></div>
            <div class="r-sigh">!</div>
            <div id="audio_tips" class="rsalert">手指上滑,取消发送</div>
        </div>
        <script src="../js/mui.min.js"></script>
        <script src="../js/mui.imageViewer.js"></script>
        <script src="../js/arttmpl.js"></script>
        <script type="text/javascript" charset="utf-8">
            (function($, doc) {
                var MIN_SOUND_TIME = 800;
                $.init({
                    gestureConfig: {
                        tap: true, //默认为true
                        doubletap: true, //默认为false
                        longtap: true, //默认为false
                        swipe: true, //默认为true
                        drag: true, //默认为true
                        hold: true, //默认为false,不监听
                        release: true //默认为false,不监听
                    }
                });
                template.config('escape', false);
                //$.plusReady=function(fn){fn();};
                $.plusReady(function() {
                    plus.webview.currentWebview().setStyle({
                        softinputMode: "adjustResize"
                    });
                    //强制弹出键盘
                    var showKeyboard = function() {
                        if ($.os.ios) {
                            var webView = plus.webview.currentWebview().nativeInstanceObject();
                            webView.plusCallMethod({
                                "setKeyboardDisplayRequiresUserAction": false
                            });
                        } else {
                            var Context = plus.android.importClass("android.content.Context");
                            var InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
                            var main = plus.android.runtimeMainActivity();
                            var imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
                            //var view = ((ViewGroup)main.findViewById(android.R.id.content)).getChildAt(0);
                            imm.showSoftInput(main.getWindow().getDecorView(), InputMethodManager.SHOW_IMPLICIT);
                            //alert("ll");
                        }
                    };
                    var record = [{
                        sender: 'zs',
                        type: 'text',
                        content: 'Hi,我是 MUI 小管家!'
                    }];
                    var ui = {
                        body: doc.querySelector('body'),
                        footer: doc.querySelector('footer'),
                        footerRight: doc.querySelector('.footer-right'),
                        footerLeft: doc.querySelector('.footer-left'),
                        btnMsgType: doc.querySelector('#msg-type'),
                        boxMsgText: doc.querySelector('#msg-text'),
                        boxMsgSound: doc.querySelector('#msg-sound'),
                        btnMsgImage: doc.querySelector('#msg-image'),
                        areaMsgList: doc.querySelector('#msg-list'),
                        boxSoundAlert: doc.querySelector('#sound-alert'),
                        h: doc.querySelector('#h'),
                        content: doc.querySelector('.mui-content')
                    };
                    //将文本框宽度赋值给h
                    ui.h.style.width = ui.boxMsgText.offsetWidth + 'px';
                    //alert(ui.boxMsgText.offsetWidth );
                    //让文本框居中
                    var footerPadding = ui.footer.offsetHeight - ui.boxMsgText.offsetHeight;
                    //语音输入相关操作
                    var msgItemTap = function(msgItem, event) {
                        var msgType = msgItem.getAttribute('msg-type');
                        var msgContent = msgItem.getAttribute('msg-content')
                        if (msgType == 'sound') {
                            player = plus.audio.createPlayer(msgContent);
                            var playState = msgItem.querySelector('.play-state');
                            playState.innerText = '正在播放...';
                            player.play(function() {
                                playState.innerText = '点击播放';
                            }, function(e) {
                                playState.innerText = '点击播放';
                            });
                        }
                    };
                    //图片预览组件
                    var imageViewer = new $.ImageViewer('.msg-content-image', {
                        dbl: false
                    });
                    //绑定消息节点
                    var bindMsgList = function() {
                        //绑定数据:
                        /*tp.bind({
                            template: 'msg-template',
                            element: 'msg-list',
                            model: record
                        });*/
                        //将数据绑定到界面上
                        ui.areaMsgList.innerHTML = template('msg-template', {
                            "record": record
                        });
                        //拿到所有的聊天节点
                        var msgItems = ui.areaMsgList.querySelectorAll('.msg-item');
                        //因为document.querySelectorAll()返回的并不是我们想当然的数组,而是NodeList,对NodeList,它里面没有.forEach方法,我们使用了这样的方法进行循环:
                        //通过call将this绑定到msgItems(以[]下标的方式去遍历msgItems)
                        [].forEach.call(msgItems, function(item, index) {
                            item.addEventListener('tap', function(event) {
                                //处理语音消息播放
                                msgItemTap(item, event);
                            }, false);
                        });
                        //查找所有符合条件的图片
                        imageViewer.findAllImage();
                        //聊天界面的高度修改
                        ui.areaMsgList.scrollTop = ui.areaMsgList.scrollHeight + ui.areaMsgList.offsetHeight;
                    };
                    bindMsgList();
                    //平滑高度过渡
                    window.addEventListener('resize', function() {
                        ui.areaMsgList.scrollTop = ui.areaMsgList.scrollHeight + ui.areaMsgList.offsetHeight;
                    }, false);
                    //发送对象声明
                    var send = function(msg) {
                        //将消息内容体push进record
                        record.push(msg);
                        //绑定消息节点
                        bindMsgList();
                        toRobot(msg.content);
                    };
                    var toRobot = function(info) {
                        var apiUrl = 'http://www.tuling123.com/openapi/api';
                        $.getJSON(apiUrl, {
                            "key": 'acfbca724ea1b5db96d2eef88ce677dc',
                            "info": info,
                            "userid": plus.device.uuid
                        }, function(data) {
                            //alert(JSON.stringify(data));
                            record.push({
                                sender: 'zs',
                                type: 'text',
                                content: data.text
                            });
                            bindMsgList();
                        });
                    };
                    
                    //让输入框获取焦点
                    function msgTextFocus() {
                            ui.boxMsgText.focus();
                            setTimeout(function() {
                                ui.boxMsgText.focus();
                            }, 150);
                        }
                        //解决长按“发送”按钮,导致键盘关闭的问题;
                    ui.footerRight.addEventListener('touchstart', function(event) {
                        if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
                            msgTextFocus();
                            event.preventDefault();
                        }
                    });
                    //解决长按“发送”按钮,导致键盘关闭的问题;
                    ui.footerRight.addEventListener('touchmove', function(event) {
                        if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
                            msgTextFocus();
                            event.preventDefault();
                        }
                    });
                    //                  ui.footerRight.addEventListener('touchcancel', function(event) {
                    //                      if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
                    //                          msgTextFocus();
                    //                          event.preventDefault();
                    //                      }
                    //                  });
                    //                  ui.footerRight.addEventListener('touchend', function(event) {
                    //                      if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
                    //                          msgTextFocus();
                    //                          event.preventDefault();
                    //                      }
                    //                  });
                    //长按离开屏幕时触发
                    ui.footerRight.addEventListener('release', function(event) {
                        //当是文字时
                        if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
                            //showKeyboard();
                            ui.boxMsgText.focus();
                            setTimeout(function() {
                                ui.boxMsgText.focus();
                            }, 150);
                            //                          event.detail.gesture.preventDefault();
                            send({
                                sender: 'self',
                                type: 'text',
                                content: ui.boxMsgText.value.replace(new RegExp('
', 'gm'), '<br/>')
                            });
                            ui.boxMsgText.value = '';
                            $.trigger(ui.boxMsgText, 'input', null);
                        } 
                        //当是语音时
                        else if (ui.btnMsgType.classList.contains('mui-icon-mic')) {
                            ui.btnMsgType.classList.add('mui-icon-compose');
                            ui.btnMsgType.classList.remove('mui-icon-mic');
                            ui.boxMsgText.style.display = 'none';
                            ui.boxMsgSound.style.display = 'block';
                            ui.boxMsgText.blur();
                            document.body.focus();
                        }
                        //当是文本状态时
                        else if (ui.btnMsgType.classList.contains('mui-icon-compose')) {
                            ui.btnMsgType.classList.add('mui-icon-mic');
                            ui.btnMsgType.classList.remove('mui-icon-compose');
                            ui.boxMsgSound.style.display = 'none';
                            ui.boxMsgText.style.display = 'block';
                            //--
                            //showKeyboard();
                            ui.boxMsgText.focus();
                            setTimeout(function() {
                                ui.boxMsgText.focus();
                            }, 150);
                        }
                    }, false);
                    
                    //点击左边按钮时
                    ui.footerLeft.addEventListener('tap', function(event) {
                        var btnArray = [{
                            title: "拍照"
                        }, {
                            title: "从相册选择"
                        }];
                        plus.nativeUI.actionSheet({
                            title: "选择照片",
                            cancel: "取消",
                            buttons: btnArray
                        }, function(e) {
                            var index = e.index;
                            switch (index) {
                                case 0:
                                    break;
                                case 1:
                                    var cmr = plus.camera.getCamera();
                                    cmr.captureImage(function(path) {
                                        send({
                                            sender: 'self',
                                            type: 'image',
                                            content: "file://" + plus.io.convertLocalFileSystemURL(path)
                                        });
                                    }, function(err) {});
                                    break;
                                case 2:
                                    plus.gallery.pick(function(path) {
                                        send({
                                            sender: 'self',
                                            type: 'image',
                                            content: path
                                        });
                                    }, function(err) {}, null);
                                    break;
                            }
                        });
                    }, false); 
                    //控制按住语音时的显示与隐藏
                    var setSoundAlertVisable=function(show){
                        if(show){
                            ui.boxSoundAlert.style.display = 'block';
                            ui.boxSoundAlert.style.opacity = 1;
                        }else{
                            ui.boxSoundAlert.style.opacity = 0;
                            //fadeOut 完成再真正隐藏
                            setTimeout(function(){
                                ui.boxSoundAlert.style.display = 'none';
                            },200);
                        }
                    };
                    var recordCancel = false;
                    var recorder = null;
                    var audio_tips = document.getElementById("audio_tips");
                    var startTimestamp = null;
                    var stopTimestamp = null;
                    var stopTimer = null;
                    //按住说话时候触发
                    ui.boxMsgSound.addEventListener('hold', function(event) {
                        recordCancel = false;
                        //如果有结束时间,清除定时器
                        if(stopTimer)clearTimeout(stopTimer);
                        //修改显示文字
                        audio_tips.innerHTML = "手指上划,取消发送";
                        //移除rprogress-sigh
                        ui.boxSoundAlert.classList.remove('rprogress-sigh');
                        //显示样式
                        setSoundAlertVisable(true);
                        //获取录音对象  5+模块
                        recorder = plus.audio.getRecorder();
                        if (recorder == null) {
                            plus.nativeUI.toast("不能获取录音对象");
                            return;
                        }
                        //记录当前录音时间
                        startTimestamp = (new Date()).getTime();
                        //保存录音http://www.html5plus.org/doc/zh_cn/audio.html#plus.audio.RecordOption
                        recorder.record({
                            filename: "_doc/audio/"
                        }, function(path) {
                            if (recordCancel) return;
                            send({
                                sender: 'self',
                                type: 'sound',
                                content: path
                            });
                        }, function(e) {
                            plus.nativeUI.toast("录音时出现异常: " + e.message);
                        });
                    }, false);
                    
                    //监听drag(拖动中)事件 上滑;下滑事件
                    ui.body.addEventListener('drag', function(event) {
                        //console.log('drag');
                        if (Math.abs(event.detail.deltaY) > 50) {
                            //此时没有录音操作执行 检查recordCancel状态
                            if (!recordCancel) {
                                recordCancel = true;
                                if (!audio_tips.classList.contains("cancel")) {
                                    audio_tips.classList.add("cancel");
                                }
                                audio_tips.innerHTML = "松开手指,取消发送";
                            }
                        } else {
                            if (recordCancel) {
                                recordCancel = false;
                                if (audio_tips.classList.contains("cancel")) {
                                    audio_tips.classList.remove("cancel");
                                }
                                audio_tips.innerHTML = "手指上划,取消发送";
                            }
                        }
                    }, false);
                    //长按离开录音节点时执行
                    ui.boxMsgSound.addEventListener('release', function(event) {
                        //console.log('release');
                        //初始化
                        if (audio_tips.classList.contains("cancel")) {
                            audio_tips.classList.remove("cancel");
                            audio_tips.innerHTML = "手指上划,取消发送";
                        }
                        //判断录音是否小于800毫秒,若小于,则废弃
                        stopTimestamp = (new Date()).getTime();
                        if (stopTimestamp - startTimestamp < MIN_SOUND_TIME) {
                            audio_tips.innerHTML = "录音时间太短";
                            ui.boxSoundAlert.classList.add('rprogress-sigh');
                            recordCancel = true;
                            stopTimer=setTimeout(function(){
                                setSoundAlertVisable(false);
                            },800);
                        }else{
                            setSoundAlertVisable(false);
                        }
                        //停止录音模块
                        recorder.stop();
                    }, false);
                    
                    //阻止浏览器默认的事件冒泡
                    ui.boxMsgSound.addEventListener("touchstart", function(e) {
                        //console.log("start....");
                        e.preventDefault();
                    });
                    //监听用户输入时触发
                    ui.boxMsgText.addEventListener('input', function(event) {
                        //当输入为空时去掉发送文字,当不为空时,显示发送文字
                        ui.btnMsgType.classList[ui.boxMsgText.value == '' ? 'remove' : 'add']('mui-icon-paperplane');
                        //当用户输入值不为空时,设置自定义属性for 赋值msg-text
                        ui.btnMsgType.setAttribute("for", ui.boxMsgText.value == '' ? '' : 'msg-text');
                        //替换与正则表达式相匹配的值(转义)
                        ui.h.innerText = ui.boxMsgText.value.replace(new RegExp('
', 'gm'), '
-') || '-';
                        //动态调整输入框高度
                        ui.footer.style.height = (ui.h.offsetHeight + footerPadding) + 'px';
                        //调整显示信息高度
                        ui.content.style.paddingBottom = ui.footer.style.height;
                    });
                    var focus = false;
                    //监听用户点击发送时触发
                    ui.boxMsgText.addEventListener('tap', function(event) {
                        //得到焦点
                        ui.boxMsgText.focus();
                        setTimeout(function() {
                            ui.boxMsgText.focus();
                        }, 0);
                        focus = true;
                        setTimeout(function () {
                            focus = false;
                        },1000);
                        //阻止iOS2.0中的手势事件:gesture事件 
                        event.detail.gesture.preventDefault();
                    }, false);
                    //点击消息列表,关闭键盘
                    ui.areaMsgList.addEventListener('click',function (event) {
                        if(!focus){
                            ui.boxMsgText.blur();
                        }
                    })  
                });
            }(mui, document));
        </script>
    </body>
原文地址:https://www.cnblogs.com/zhangtinghang/p/8303044.html