(function () {

    /**
    * 调用APP 外壳交互API 相关
    * @module BM.appointment
    * @submodule appointment
    *
    */

    var appointment = {
        getCommonCallback: function (callback) {
            return function (data) {
                if (callback) {
                    callback(data);
                }
            }
        }
    };

    BM.appointment = appointment;



})();

(function () {

    var appointment = BM.appointment;

    var serviceName = "event";

    function Event() {
        this.add = function (eventName, callback) {
            BM.webBridge.native.execute(serviceName, "add", { eventName: eventName }, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        };

        this.remove = function (eventName, callback) {
            BM.webBridge.native.execute(serviceName, "remove", { eventName: eventName }, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        };
    }


    appointment.event = new Event();


})();


(function () {

    var appointment = BM.appointment;

    function Document() {

        var events = {};

        this.on = function (eventName, fn) {
            var ev = getEvent(eventName);

            if (!$.isFunction(fn)) {
                return;
            }

            var index = $.inArray(fn, ev);
            if (index > -1) {
                return;
            }

            if (ev.length == 0) {
                BM.appointment.event.add(eventName, function (data) {
                    var name = data.eventName;
                    var evs = getEvent(name);

                    $.each(evs, function (_, ev) {
                        ev();
                    });
                });
            }

            ev.push(fn);
        }

        this.off = function (eventName, fn) {
            var ev = getEvent(eventName);

            if (!$.isFunction(fn)) {
                return;
            }

            var index = $.inArray(fn, ev);
            if (index == -1) {
                return;
            }

            ev.splice(index, 1);

            if (ev.length == 0) {
                BM.appointment.event.remove(eventName);
            }
        }

        function getEvent(name) {
            var event = events[name];

            if (event) {
                return event;
            }

            events[name] = [];

            return getEvent(name);
        }
    }

    var document = new Document();

    appointment.document = document;

})();

(function () {
    var appointment = BM.appointment;
    var serviceName = "driver";

    /**
      * 设备信息类操作集合
      * @class driver
      * 
      */


    function Driver() {

        /**
         * 获取设备信息
         * @method get 
         * @param {Function} callback 完成后的回调事件
         * @param {JSON} callback.data callback事件的第一个数据
         * @param {string} callback.data.name 设备名称
         * @param {string} callback.data.platform 平台以下几个值[android、ios、web、wechat]
         * @param {string} callback.data.version 系统版本
         * @param {string} callback.data.version 框架版本
         * 
         * @example
         * 
         *      BM.appointment.driver.get(function(data){
         *          alert("当前的设备是:" + data.name)
         *      })
         * 
         */
        this.get = function (callback) {
            BM.webBridge.native.execute(serviceName, "get", {}, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        };

        var _lasterInfo = null;

        /**
         * Overwritten method see {{#crossLink "appointment.driver/get"}}{{/crossLink}}
         * 
         * 
         * 获取最近一次设备信息,此方法是对 [get] 方法做了缓存包装. 参数参考[get]
         * 
         * @method getLaster 
         */
        this.getLaster = function (callback) {
            if (_lasterInfo != null) {
                appointment.getCommonCallback(callback)(_lasterInfo);
            }
            else {
                this.get(function (data) {
                    _lasterInfo = data;
                    appointment.getCommonCallback(callback)(_lasterInfo);
                });
            }
        }

        /**
         * 获取系统指纹相关信息
         * @method getFingerprintInfo
         * @param {Function} callback 完成后的回调事件
         * @param {JSON} callback.data callback事件的第一个数据
         * @param {Boolean} callback.data.isSupport 系统是否支持指纹
         * @param {Boolean} callback.data.isSet 系统是否设置过指纹
         */
        this.getFingerprintInfo = function (callback) {
            BM.webBridge.native.execute(serviceName, "getFingerprintInfo", {}, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        }
    }


    appointment.driver = new Driver();
})();

(function () {

    var appointment = BM.appointment;


    /**
      * 系统类操作集合
      * @class webview
      * @module BM.appointment
      * 
      */

    var serviceName = "webview";

    var toAbsURL = function (url) {
        var a = document.createElement('a');
        a.href = url;
        return a.href;
    };

    function WebView() {
        if (!BM._popupWindowStack) {
            BM._popupWindowStack = [];
        }

        /**
         * 打开一个新的view,此方法和navigate方法效果一样
         * @method open
         * @param {String} url view中的URL地址为
         * @param {String} [direction] 打开Url的窗口的动画. 此参数暂时设置无效果
         * @param {Boolean} [true] isNeedProgressBar 是否需要显示进度条 
         * @param {Boolean} [true] isNeedTitle 是否需要标题
         */
        this.open = function (url, direction, isNeedProgressBar, isNeedTitle) {
            this.navigate(url, direction, isNeedProgressBar, isNeedTitle);
        };

        /**
         * 打开一个新的view
         * @method navigate
         * @param {String} url view中的URL地址为
         * @param {String} [direction] 打开Url的窗口的动画. 此参数暂时设置无效果
         * @param {Boolean} [true] isNeedProgressBar 是否需要显示进度条 
         * @param {Boolean} [true] isNeedTitle 是否需要标题
         */
        this.navigate = function (url, direction, isNeedProgressBar, isNeedTitle) {
            url = toAbsURL(url);
            if (!url) {
                alert("url 为null");
            }
            BM.webBridge.native.execute(serviceName, "open", { url: url, direction: direction, isNeedProgressBar: isNeedProgressBar, isNeedTitle: isNeedTitle });
        }

        var popups = {};

        this.findPopup = function (id) {
            var popup = popups[id];
            if (popup) {
                return popup;
            }
        };

        /**
         * 弹出一个页,并能够在关闭的时候接受回调函数
         * @method popup
         * @param {JSON} options 选项
         * @param {JSON} options.url 打开的url
         * @param {String} [options.id] 窗口ID
         * @param {Boolean} [options.isNeedProgressBar] 是否需要显示进度条url
         * @param {JSON} [options.data] 传递给打开页面的数据,此数据可以用getRequestData方法获取到
         * @param {Function} [options.onClose] 关闭的回调事件
         */
        this.popup = function (options) {
            var config = $.extend(true, {
                id: "",
                url: "",
                title: "",
                isNeedProgressBar: true,
                onClose: function () {

                },
                data: {},
                isNeedProgressBar: true
            }, options, {});

            config.url = toAbsURL(options.url);

            BM.webBridge.native.execute(serviceName, "popup", { url: config.url, title: config.title, data: JSON.stringify(config.data), isNeedProgressBar: config.isNeedProgressBar }, function (data) {
                if (config.onClose) {
                    config.onClose(data);
                }
            });
        };

        /**
         * 页面后退
         * @method back
         */
        this.back = function () {
            BM.webBridge.native.execute(serviceName, "back");
        }

        /**
         * 关闭一个页, 对于open方法
         * @method close
         */
        this.close = function () {
            BM.webBridge.native.execute(serviceName, "close");
        }

        /**
         * 关闭 Propup 页,对应popup方法
         * @method closePopup
         * @param {object} data 传递给打开Popup的方法
         */
        this.closePopup = function (data) {
            BM.webBridge.native.execute(serviceName, "closePopup", { data: data });
        }

        /**
         * 设置当前窗口的按钮
         * @method setViewButton
         * @param {string} buttonCode 按钮代号    [默认三种: back => 后退 点击为后退.  home => 点击为跳到首页 . none => 去掉图标. 并且点击返回键也不能有任何响应]
         */
        this.setViewButton = function (buttonCode) {
            BM.webBridge.native.execute(serviceName, "setViewButton", { buttonCode: buttonCode });
        };

        /**
         * 跳转到登录主界面
         * @method goHome
         */
        this.goHome = function () {
            BM.webBridge.native.execute(serviceName, "goHome");
        };

        /**
         * 跳转到登录界面
         * @method goLoginView
         */
        this.goLoginView = function () {
            BM.webBridge.native.execute(serviceName, "goLoginView");
        };

        /**
         * 设置当前页面的标题
         * @method setTitle
         * @param {String} title 标题
         */
        this.setTitle = function (title) {
            BM.webBridge.native.execute(serviceName, "setTitle", { title: title });
        }

        /**
         * 禁用刷新选项
         * @method disabledRefresh
         * @param {Boolean} disabled 是否禁用
         */
        this.disabledRefresh = function (disabled) {
            BM.webBridge.native.execute(serviceName, "disabledRefresh", { disabled: disabled });
        }


        /**
         * 添加字体Toolbar
         * @method addFontToolbar
         * @param {String} id 按钮的ID,唯一
         * @param {String} fontFamily 哪种字体
         * @param {String} fontName 字的名称
         * @param {Function} callback 按钮被点击后响应的事件
         */
        this.addFontToolbar = function (id, fontFamily, fontName, callback) {
            BM.webBridge.native.execute(serviceName, "addFontToolbar",
                { id: id, fontFamily: fontFamily, fontName: fontName }, function (data) {
                    if (callback) {
                        callback(data);
                    }
                });
        }

        /**
         * 删除Toolbar
         * @method deleteToolbar
         * @param {String} id 按钮的ID,唯一
         */
        this.deleteToolbar = function (id) {
            BM.webBridge.native.execute(serviceName, "deleteToolbar", { id: id });
        }

        /**
         * 禁用Toolbar
         * @method disabledToolbar
         * @param {String} id 按钮的ID,唯一
         * @param {Boolean} disabled 是否禁用
         */
        this.disabledToolbar = function (id, disabled) {
            BM.webBridge.native.execute(serviceName, "disabledToolbar", { id: id, disabled: disabled });
        }

        /**
         * 添加文本Toolbar
         * @method addTextToolbar
         * @param {String} id 按钮的ID,唯一
         * @param {String} text 按钮的文字
         * @param {Function} callback 按钮被点击后响应的事件
         */
        this.addTextToolbar = function (id, text, callback) {
            BM.webBridge.native.execute(serviceName, "addTextToolbar", { id: id, text: text }, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        }

        /**
         * 更改文本Toolbar的文字
         * @method changeTextToolbar
         * @param {String} id 按钮的ID,唯一
         * @param {String} text 按钮的文字
         */
        this.changeTextToolbar = function (id, text) {
            BM.webBridge.native.execute(serviceName, "changeTextToolbar", { id: id, text: text });
        }

        /**
         * 获取请求的数据
         * @method getRequestData
         * @param {Function} callback 获取到后通知的事件
         * @param {JSON} callback.data 获取到的数据
         */
        this.getRequestData = function (callback) {
            BM.webBridge.native.execute(serviceName, "getRequestData", {}, function (data) {
                if (callback) {
                    callback(JSON.parse(data.data));
                }
            });
        }

        /**
         * 设置是否启用WebView的历史功能,可以后退WebView里面的页面
         * @method setHistroyEnabled
         * @param {Boolean} [false] enabled 是否开启
         */
        this.setHistroyEnabled = function (enabled, callback) {
            BM.webBridge.native.execute(serviceName, "setHistroyEnabled", { enabled: enabled }, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        }

        /**
         * 设置是否禁用WebView的后退功能
         * @method setHistroyBackDisabled
         * @param {Boolean} [false] disabled 是否禁用
         */
        this.setHistroyBackDisabled = function (disabled, callback) {
            BM.webBridge.native.execute(serviceName, "setHistroyBackDisabled", { disabled: disabled }, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        }

        this.addTextViewButton = function (id, text, callback) {
            BM.webBridge.native.execute(serviceName, "addTextViewButton", { id: id, text: text }, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        }

        this.deleteViewButton = function (id) {
            BM.webBridge.native.execute(serviceName, "deleteViewButton", { id: id });
        }

        /** 
         * 设置当前标题栏是否可见
         * @method setTitleBarVisible
         * @param {Boolean} isVisible 是否可见
         */
        this.setTitleBarVisible = function (isVisible) {
            BM.webBridge.native.execute(serviceName, "setTitleBarVisible", { isVisible: isVisible });
        };

        /**
         * 设置当前是否启用沉浸式状态栏
         * @method enabledImmersiveStatusBar
         * @param {Boolean} 是否启用
         */
        this.enabledImmersiveStatusBar = function (enabled) {
            BM.webBridge.native.execute(serviceName, "enabledImmersiveStatusBar", { enabled: enabled });
        };
    }


    function PopupView(options) {
        var that = this;
        this.remoteID = "";
        this.url = options.url;
        this.title = options.title;
        this.options = options;
        this.onDestroy = null;

        this.show = function () {
            BM.webBridge.native.execute(serviceName, "popup", { url: this.url, title: this.title }, function (data) {
                that.remoteID = data.id;
            });
        }

        this.ok = function (data) {
            if (that.options.onOk) {
                that.options.onOk(data);
            }

            this.close();
        }

        this.close = function () {
            BM.webBridge.native.execute(serviceName, "close", { id: this.remoteID }, function (data) {
                if (that.options.onClose) {
                    that.options.onClose();
                }

                that.destroy();
            });
        };

        this.destroy = function () {
            if (this.onDestroy) {
                this.onDestroy();
            }
        };
    }


    appointment.webview = new WebView();
})();


(function () {


    var appointment = BM.appointment;


    var serviceName = "location";

    /**
     * 地理位置
     * @class location
     * @module BM.appointment
     */

    appointment.location = new function () {

        /**
         * 获取定位
         * @method get
         * @param {Function} callback 获取到后通知的事件
         * @param {JSON} callback.data 获取到的数据
         * @param {JSON} callback.data.point 定位的坐标
         * @param {Number} callback.data.point.longitude 经度
         * @param {Number} callback.data.point.longitude 纬度
         * 
         * @return { 
         *    point: { 
         *          longitude: 100.00, 
         *          latitude: 100.00
         *       }
         *   }
         */
        this.get = function (callback) {
            BM.webBridge.native.execute(serviceName, "get", {}, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        }
    };

})();

(function () {
    var appointment = BM.appointment;

    /**
      * 图像查看器
      * @class imageView
      * @module BM.appointment
      */
    var serviceName = "imageView";
    appointment.imageView = new function () {
        /**
         * 显示
         * @method show
         * @param {Array} images 要显示的图片列表
         * @param {String} images.url 图片的下载URL
         * @param {String} images.title 图片的标题
         * @param {int} currentIndex 当前的图片索引
         */
        this.show = function (imageList, currentIndex, callback) {
            BM.webBridge.native.execute(serviceName, "show",
                { imageList: imageList, currentIndex: currentIndex },
                function (data) {
                    if (callback) {
                        callback(data);
                    }
                });
        }
    }
})();



(function () {
    var appointment = BM.appointment;

    /**
      * 照相机
      * @class camera
      * @module BM.appointment
      */
    var serviceName = "camera";

    appointment.camera = new function () {
        /**
         * 获取一张图片
         * @method getPictrue
         * @param {Function} callback 获取成功后的回调函数
         * @param {JSON} callback.data 获取的数据
         * @param {string} callback.data.imgURI 图片的磁盘物理地址
         * @param {string} callback.data.fileSize 图片的文件字节大小
         */
        this.getPicture = function (callback) {
            BM.webBridge.native.execute(serviceName, "getPicture", {}, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        };

        /**
          * 上传一张图片
          * @method uploadPicture
          * @param {String} imgURI 图片的磁盘物理地址
          * @param {String} uploadURL 上传服务器地址
          * @param {Function} callback 获取成功后的回调函数
          * @param {JSON} callback.data 获取的数据
          * @param {Number} callback.data.state 上传的状态 0 准备, 1 上传中, 2 成功, 3 失败, 4 超时 
          * @param {double} callback.data.progressPercentage 上传进度 1为100%
          * @param {string} callback.data.code 上传的code
          * @param {string} callback.data.message 上传的完成后返回的文本
          * @param {string} callback.data.result 上传的完成后服务端返回的数据
          */
        this.uploadPicture = function (imgURI, uploadURL, callback) {
            BM.webBridge.native.execute(serviceName, "uploadPicture", { imgURI: imgURI, uploadURL: uploadURL },
            function (data) {
                if (callback) {
                    callback(data);
                }
            });
        };
        /**
         * 扫码
         * @method scanQR
         * @param {Function} callback 扫码成功后通知的事件
         * @param {String} callback.data 获取到的文本结果
         */
        this.scanQR = function (callback) {
            BM.webBridge.native.execute(serviceName, "scanQR", {}, function (data) {
                if (callback) {
                    callback(data);
                }
            });
        }
    };
})();


(function () {
    var appointment = BM.appointment;

    /**
      * 社交服务
      * @class sns
      * @module BM.appointment
      */
    var serviceName = "sns";


    appointment.sns = new function () {

        /**
         * 分享
         * @method share
         * @param {JSON} options 参数配置
         * @param {string} options.title 标题 为null是获取当期网页的标题
         * @param {string} options.description 简介
         * @param {string} [options.imgUrl] 图片URL
         * @param {Boolean} [options.isScreenshot=false] 是否屏幕截图
         * @param {string} [url=当前URL] 分享的Url如果是为空,则默认为当前的url地址
         */
        this.share = function (options) {
            var config = $.extend(true, {},
                {
                    title: "",
                    subtitle: "",
                    description: "",
                    imgUrl: "",
                    isScreenShot: false,
                    url: ""
                }, options);

            BM.webBridge.native.execute(serviceName, "share", config,
                function (data) {
                    if (callback) {
                        callback(data);
                    }
                }
            );
        };
    }
})();


(function () {
    var appointment = BM.appointment;

    /**
     * 数据服务
     * @class data
     * @module BM.appointment
     */
    var serviceName = "data";

    function Data() {
        /**
          * 设置数据
          * @method set
          * @param {string} key 存储的数据名
          * @param {string} value 存储的值
          * @param {Function} callback 回调函数
          */
        this.set = function (key, value, callback) {
            BM.webBridge.native.execute(serviceName, "set",
                {
                    domain: getCurrentDomain(),
                    key: key,
                    value: value
                },
                BM.appointment.getCommonCallback(callback)
            );
        };

        /**
          * 获取数据
          * @method get
          * @param {string} key 存储的数据名
          * @param {Function} callback 回调函数
          * @param {string} callback.value 获取的值,如果 key 不存在。返回空
          */
        this.get = function (key, callback) {
            this.containKey(key, function (result) {
                if (!result) {
                    if (callback) {
                        callback(null);
                    }
                    return;
                }

                BM.webBridge.native.execute(serviceName, "get",
                    {
                        domain: getCurrentDomain(),
                        key: key
                    },
                    BM.appointment.getCommonCallback(callback)
                );
            });
        };
        /**
          * 移除数据
          * @method remove
          * @param {string} key 存储的数据名
          * @param {Function} callback 回调函数
          */
        this.remove = function (key, callback) {
            BM.webBridge.native.execute(serviceName, "remove",
                {
                    domain: getCurrentDomain(),
                    key: key
                },
                BM.appointment.getCommonCallback(callback)
            );
        };
        /**
          * 清空所有数据
          * @method clear
          * @param {Function} callback 回调函数
          */
        this.clear = function (callback) {
            BM.webBridge.native.execute(serviceName, "clear",
                {
                    domain: getCurrentDomain()
                },
                BM.appointment.getCommonCallback(callback)
            );
        };

        this.containKey = function (key, callback) {
            BM.webBridge.native.execute(serviceName, "containKey",
                {
                    domain: getCurrentDomain(),
                    key: key
                },
                BM.appointment.getCommonCallback(callback)
            );
        };
    }

    appointment.data = new Data();

    function getCurrentDomain() {
        return window.location.host;
    };
})();

(function () {
    var appointment = BM.appointment;

    var serviceName = "plugin";

    /**
     * 插件服务
     * @class plugin
     * @module BM.appointment
     */
    function Plugin() {
        this.show = function (code, functionCode, params, callback) {
            BM.webBridge.native.execute(serviceName, "show",
                {
                    code: code,
                    functionCode: functionCode,
                    params: JSON.stringify(params || {})
                }, BM.appointment.getCommonCallback(callback));
        };

        /**
         * 调用执行插件
         * @method exec
         * @param {String} code 插件服务的标识
         * @param {String} functionCode 插件的功能标识
         * @param {String} params 插件功能需要提供的参数
         * @param {Function} callback 插件执行完成后,返回调用的回调函数
         * @param {Object} callback.data 执行完成后,返回的数据
         */
        this.exec = function (code, functionCode, params, callback) {
            this.show(code, functionCode, params, callback);
        };
    }

    appointment.plugin = new Plugin();

})();

(function () {
    // Init 
    $(function () {
        BM.ready(function () {
            if (window.BM_setAutoConvertLinkToPageEnabled && window.BM_setAutoConvertLinkToPageEnabled == true) {
                $("a").click(function () {
                    var mode = $(this).attr("data-mode");
                    if (mode !== "none") {
                        var href = $(this).attr("href");

                        if (!href || href == "#") {
                            return false;
                        }

                        BM.appointment.webview.open(href);

                        return false;
                    }
                });
            }
        });
    });
})();

    
Top