浅尝MUI-详解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-width: 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('\n', '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('\n', 'gm'), '\n-') || '-';
        //动态调整输入框高度
        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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容