prj-common-globalservice-legacy.js 12 KB
// 项目通用的全局service服务,供不同的controller使用,自定义指令不使用

// 文件下载服务
angular.module('ScheduleApp').factory('FileDownload_g', function() {
    return {
        downloadFile: function (data, mimeType, fileName) {
            var success = false;
            var blob = new Blob([data], { type: mimeType });
            try {
                if (navigator.msSaveBlob)
                    navigator.msSaveBlob(blob, fileName);
                else {
                    // Try using other saveBlob implementations, if available
                    var saveBlob = navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob;
                    if (saveBlob === undefined) throw "Not supported";
                    saveBlob(blob, fileName);
                }
                success = true;
            } catch (ex) {
                console.log("saveBlob method failed with the following exception:");
                console.log(ex);
            }

            if (!success) {
                // Get the blob url creator
                var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL;
                if (urlCreator) {
                    // Try to use a download link
                    var link = document.createElement('a');
                    if ('download' in link) {
                        // Try to simulate a click
                        try {
                            // Prepare a blob URL
                            var url = urlCreator.createObjectURL(blob);
                            link.setAttribute('href', url);

                            // Set the download attribute (Supported in Chrome 14+ / Firefox 20+)
                            link.setAttribute("download", fileName);

                            // Simulate clicking the download link
                            var event = document.createEvent('MouseEvents');
                            event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
                            link.dispatchEvent(event);
                            success = true;

                        } catch (ex) {
                            console.log("Download link method with simulated click failed with the following exception:");
                            console.log(ex);
                        }
                    }

                    if (!success) {
                        // Fallback to window.location method
                        try {
                            // Prepare a blob URL
                            // Use application/octet-stream when using window.location to force download
                            var url = urlCreator.createObjectURL(blob);
                            window.location = url;
                            console.log("Download link method with window.location succeeded");
                            success = true;
                        } catch (ex) {
                            console.log("Download link method with window.location failed with the following exception:");
                            console.log(ex);
                        }
                    }
                }
            }

            if (!success) {
                // Fallback to window.open method
                console.log("No methods worked for saving the arraybuffer, using last resort window.open");
                window.open("", '_blank', '');
            }
        }
    };
});


/**
 * saSelect2指令,根据属性值,动态载入数据,然后支持拼音搜索,点击右边的按钮清除选择并重新载入数据。
 * 1、compile阶段使用的属性如下:
 *      required:用于和表单验证连接,指定成required="true"才有效。
 * 2、link阶段使用的属性如下
 *      model:关联的模型对象
 *      name:表单验证时需要的名字
 *      type:关联的那种数据值(xl/cl/ry)-> 对应线路信息/车辆信息/人员信息,后面有的继续加
 *      modelcolname1:关联的模型字段名字1(一般应该是编码字段)
 *      modelcolname2:关联的模型字段名字2(一般应该是名字字段)
 *      datacolname1;内部数据对应的字段名字1(与模型字段1对应)
 *      datacolname2:内部数据对应的字段名字2(与模型字段2对应)
 *      showcolname:下拉框显示的内部数据字段名(注意:不是模型数据字段名),TODO:以后考虑放动态表达式,并在compile阶段使用
 *      placeholder:select placeholder字符串描述
 *
 * $$pyFilter,内部的filter指令,结合简拼音进行拼音过滤。
 * $$SearchInfoService_g,内部使用的数据服务
 */
// saSelect2指令使用的内部信service
angular.module('ScheduleApp').factory('$$SearchInfoService_g', ['$resource', function($resource) {
    return {
        xl: $resource(
            '/line/:type',
            {order: 'name', direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        xlinfo: $resource(
            '/lineInformation/:type',
            {order: 'line.name', direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        zd: $resource(
            '/stationroute/stations',
            {order: 'stationCode', direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        tcc: $resource(
            '/carpark/:type',
            {order: 'parkCode', direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        ry: $resource(
            '/personnel/:type',
            {order: 'personnelName', direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        cl: $resource(
            '/cars/:type',
            {order: "insideCode", direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        ttInfo: $resource(
            '/tic/:type',
            {order: "name", direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        lpInfo: $resource(
            '/gic/ttlpnames',
            {order: "lpName", direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        lpInfo2: $resource(
            '/gic/:type',
            {order: "lpName", direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        cci: $resource(
            '/cci/cars',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }

        ),
        cci2: $resource(
            '/cci/:type',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        cci3: $resource(
            '/cci/cars2',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }

        ),
        eci: $resource(
            '/eci/jsy',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        eci2: $resource(
            '/eci/spy',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        eci3: $resource(
            '/eci/:type',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),


        validate: { // remoteValidation指令用到的resource
            gbv1: { // 路牌序号验证
                template: {'xl.id_eq': -1, 'lpNo_eq': 'ddd'},
                remote: $resource(
                    '/gic/validate1',
                    {},
                    {
                        do: {
                            method: 'GET'
                        }
                    }
                )
            },
            gbv2: { // 路牌名称验证
                template: {'xl.id_eq': -1, 'lpName_eq': 'ddd'},
                remote: $resource(
                    '/gic/validate2',
                    {},
                    {
                        do: {
                            method: 'GET'
                        }
                    }
                )
            },

            cl1: { // 车辆自编号不能重复验证
                template: {'insideCode_eq': '-1'}, // 查询参数模版
                remote: $resource( // $resource封装对象
                    '/cars/validate/equale',
                    {},
                    {
                        do: {
                            method: 'GET'
                        }
                    }
                )
            },
            cde1: { // 车辆设备启用日期验证
                template: {'qyrq': 0, 'xl': 1, 'cl': 1}, // 日期毫秒
                remote: $resource( // $resource封装对象
                    '/cde//validate/qyrq',
                    {},
                    {
                        do: {
                            method: 'GET'
                        }
                    }
                )
            },
            ttc1: { // 时刻表名字验证
                template: {'xl.id_eq': -1, 'name_eq': 'ddd'},
                remote: $resource( // $resource封装对象
                    '/tic/validate/equale',
                    {},
                    {
                        do: {
                            method: 'GET'
                        }
                    }
                )
            },
            sheet: { // 时刻表sheet工作区验证
                template: {'filename': '', 'sheetname': '', 'lineid': -1, 'linename': ''},
                remote: $resource( // $resource封装对象
                    '/tidc/validate/sheet',
                    {},
                    {
                        do: {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            },
                            transformRequest: function(obj) {
                                var str = [];
                                for (var p in obj) {
                                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                                }
                                return str.join("&");
                            }
                        }
                    }
                )
            },
            sheetli: { // 时刻表线路标准验证
                template: {'lineinfoid': -1},
                remote: $resource( // $resource封装对象
                    '/tidc/validate/lineinfo',
                    {},
                    {
                        do: {
                            method: 'GET'
                        }
                    }
                )
            }
        }

        //validate: $resource(
        //    '/cars/validate/:type',
        //    {},
        //    {
        //        insideCode: {
        //            method: 'GET'
        //        }
        //    }
        //)



    }
}]);