Last active
October 25, 2017 05:33
-
-
Save jjhesk/063d3b93c7a59aa2e22b7f52030badcd to your computer and use it in GitHub Desktop.
code review
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Created by hesk on 25/10/2017. | |
| */ | |
| var selSeat = new Array(8); | |
| var count = 0; | |
| var max_seat_in_row = 15; | |
| var reject_list = [201, 102, 101]; | |
| var accept_list = []; | |
| $(document).ready(function () { | |
| for (var i = 2; i <= max_seat_in_row; i++) { | |
| var accept_list_key = '2'; | |
| for (var x = 1; x <= i; x++) { | |
| accept_list_key += '1'; | |
| } | |
| accept_list_key += '2'; | |
| accept_list.push(accept_list_key); | |
| accept_list.push(accept_list_key); | |
| } | |
| $.each(accept_list, function (index, code) { | |
| if (index % 2 == 0) { | |
| accept_list[index] = replaceStr(accept_list[index], (accept_list[index].length - 2), '0'); | |
| } | |
| else { | |
| accept_list[index] = replaceStr(accept_list[index], 1, '0'); | |
| } | |
| }); | |
| connectWebViewJavascriptBridge(function (bridge) { | |
| bridge.init(function (message, responseCallback) { | |
| console.log('JS got a message', message); | |
| var data = { | |
| 'Javascript Responds': '测试中文!' | |
| }; | |
| console.log('JS responding with', data); | |
| responseCallback(data); | |
| }); | |
| bridge.registerHandler("functionInJs", function (data, responseCallback) { | |
| var responseData = "Javascript is connected and ready!"; | |
| responseCallback(responseData); | |
| }); | |
| }); | |
| }); | |
| function selectSeatWithPrevPost(row, col, prev_col, post_col, row_name, col_name, obj, iswheel) { | |
| if (checkStatusByObj(obj)) { | |
| if (checkStatusByRowCol(row, prev_col) && checkStatusByRowCol(row, post_col)) { | |
| return; | |
| } else { | |
| deselect(obj); | |
| return; | |
| } | |
| } | |
| checkSeat(obj); | |
| if (checkWheelchair(obj)) { | |
| return false; | |
| } | |
| if (count == 0) { | |
| // obj.style.backgroundColor = '#0000FF'; | |
| $(obj).addClass('selected'); | |
| selSeat[count] = new Array(5); | |
| selSeat[count][0] = obj; | |
| selSeat[count][1] = row; | |
| selSeat[count][2] = col; | |
| selSeat[count][3] = row_name; | |
| selSeat[count][4] = col_name; | |
| count++; | |
| selSeat.sort(); | |
| checkBuyable(); | |
| } else if (checkStatusByRowCol(row, prev_col) || checkStatusByRowCol(row, post_col)) { | |
| if (count < 8) { | |
| // obj.style.backgroundColor = '#0000FF'; | |
| $(obj).addClass('selected'); | |
| selSeat[count] = new Array(5); | |
| selSeat[count][0] = obj; | |
| selSeat[count][1] = row; | |
| selSeat[count][2] = col; | |
| selSeat[count][3] = row_name; | |
| selSeat[count][4] = col_name; | |
| count++; | |
| selSeat.sort(); | |
| } else if (count === 8) { | |
| exceedMaxSeat(); | |
| } | |
| } else { | |
| resetSeat(); | |
| // obj.style.backgroundColor = '#0000FF'; | |
| $(obj).addClass('selected'); | |
| selSeat[count] = new Array(5); | |
| selSeat[count][0] = obj; | |
| selSeat[count][1] = row; | |
| selSeat[count][2] = col; | |
| selSeat[count][3] = row_name; | |
| selSeat[count][4] = col_name; | |
| count++; | |
| selSeat.sort(); | |
| checkBuyable(); | |
| } | |
| msg = ''; | |
| for (var i = 0; i < count; i++) { | |
| // msg = msg + i + '- ' + selSeat[i][0].id + '\n'; | |
| msg = msg + i + '- ' + $(selSeat[i][0]).attr('data-seatid') + '\n'; | |
| } | |
| alert(msg); | |
| } | |
| function selectSeat(row, col, row_name, col_name, obj, iswheel) { | |
| if (checkStatusByObj(obj)) { | |
| if (checkStatusByRowCol(row, col - 1) && checkStatusByRowCol(row, col + 1)) { | |
| return; | |
| } else { | |
| var isRemovable = checkRemovable(obj); | |
| if (isRemovable) { | |
| deselect(obj); | |
| } | |
| return; | |
| } | |
| } | |
| checkSeat(obj); | |
| if (checkWheelchair(obj)) { | |
| return false; | |
| } | |
| var error = ''; | |
| if (count < 8) { | |
| $(obj).addClass('selected'); | |
| selSeat[count] = new Array(5); | |
| selSeat[count][0] = obj; | |
| selSeat[count][1] = row; | |
| selSeat[count][2] = col; | |
| selSeat[count][3] = row_name; | |
| selSeat[count][4] = col_name; | |
| count++; | |
| if (!checkBuyableNew(obj)) { | |
| deselect(obj); | |
| error = "|710"; | |
| } | |
| } else { | |
| exceedMaxSeat(); | |
| error = "|700"; | |
| } | |
| console.log(selSeat); | |
| notification_android(generate_android_format() + error); | |
| } | |
| function generate_android_format() { | |
| var msg = ''; | |
| for (var i = 0; i < count; i++) { | |
| msg = msg + $(selSeat[i][0]).attr('data-seatid') + '-' + selSeat[i][3] + selSeat[i][4]; | |
| if (i < count - 1) { | |
| msg += ","; | |
| } | |
| } | |
| return msg; | |
| } | |
| function selectSeat_one(row, col, row_name, col_name, obj, iswheel) { | |
| if (checkStatusByObj(obj)) { | |
| deselect(obj); | |
| return; | |
| } | |
| if (count == 0) { | |
| // obj.style.backgroundColor = '#0000FF'; | |
| $(obj).addClass('selected'); | |
| checkSeat(obj); | |
| selSeat[count] = new Array(5); | |
| selSeat[count][0] = obj; | |
| selSeat[count][1] = row; | |
| selSeat[count][2] = col; | |
| selSeat[count][3] = row_name; | |
| selSeat[count][4] = col_name; | |
| count++; | |
| } else if (count < 8) { | |
| // obj.style.backgroundColor = '#0000FF'; | |
| $(obj).addClass('selected'); | |
| checkSeat(obj); | |
| selSeat[count] = new Array(5); | |
| selSeat[count][0] = obj; | |
| selSeat[count][1] = row; | |
| selSeat[count][2] = col; | |
| selSeat[count][3] = row_name; | |
| selSeat[count][4] = col_name; | |
| count++; | |
| } else if (count === 8) { | |
| exceedMaxSeat(); | |
| } else { | |
| resetSeat(); | |
| // obj.style.backgroundColor = '#0000FF'; | |
| $(obj).addClass('selected'); | |
| checkSeat(obj); | |
| selSeat[count] = new Array(5); | |
| selSeat[count][0] = obj; | |
| selSeat[count][1] = row; | |
| selSeat[count][2] = col; | |
| selSeat[count][3] = row_name; | |
| selSeat[count][4] = col_name; | |
| count++; | |
| } | |
| var msg = ''; | |
| for (var i = 0; i < count; i++) { | |
| // msg = msg + i + '- ' + selSeat[i][0].id + '\n'; | |
| msg = msg + i + '- ' + $(selSeat[i][0]).attr('data-seatid') + '\n'; | |
| } | |
| alert(msg); | |
| } | |
| function deselect(obj) { | |
| temp = new Array(8); | |
| t_count = 0; | |
| for (var i = 0; i < count; i++) { | |
| if (selSeat[i][0] != obj) { | |
| temp[t_count] = selSeat[i]; | |
| t_count++; | |
| } else { | |
| // selSeat[i][0].style.backgroundColor = ''; | |
| $(selSeat[i][0]).removeClass('selected'); | |
| uncheckSeat(selSeat[i][0]); | |
| } | |
| } | |
| count--; | |
| selSeat = temp; | |
| console.log(selSeat); | |
| var msg = ''; | |
| for (var i = 0; i < count; i++) { | |
| msg = msg + $(selSeat[i][0]).attr('data-seatid') + '-' + selSeat[i][3] + selSeat[i][4]; | |
| if (i < count - 1) { | |
| msg += ","; | |
| } | |
| } | |
| notification_android(msg); | |
| } | |
| function resetSeat() { | |
| for (var i = 0; i < count; i++) { | |
| // selSeat[i][0].style.backgroundColor = ''; | |
| $(selSeat[i][0]).removeClass('selected'); | |
| uncheckSeat(selSeat[i][0]); | |
| } | |
| selSeat = new Array(8); | |
| count = 0; | |
| } | |
| function checkStatusByObj(obj) { | |
| isSelected = false; | |
| for (var i = 0; i < count; i++) { | |
| if (selSeat[i][0] == obj) | |
| isSelected = true; | |
| } | |
| return isSelected; | |
| } | |
| function checkStatusByRowCol(row, col) { | |
| isSelected = false; | |
| for (var i = 0; i < count; i++) { | |
| if (selSeat[i][1] == row && selSeat[i][2] == col) | |
| isSelected = true; | |
| } | |
| return isSelected; | |
| } | |
| function confirmSeat(next_pg, lang, show_id, promotion_type) { | |
| if (count == 0) { | |
| return; | |
| } | |
| if (promotion_type.length <= 0) | |
| url = next_pg + '?lang=' + lang + '&show_id=' + show_id + '&seatList=' + selSeat[0][3] + selSeat[0][4]; | |
| else | |
| url = next_pg + '?lang=' + lang + '&show_id=' + show_id + '&promotion_type=' + promotion_type + '&seatList=' + selSeat[0][3] + selSeat[0][4]; //2011-04-29 Microsoft IE Promotion | |
| for (i = 1; i < count; i++) { | |
| url = url + ',' + selSeat[i][3] + selSeat[i][4]; | |
| } | |
| //alert (url); | |
| window.location = url; | |
| } | |
| function checkBuyable() { | |
| var buyable = false; | |
| var newArray = cleanArray(selSeat); | |
| var nextObj = $(newArray[newArray.length - 1]).next(); | |
| var prevObj = $(newArray[0]).prev(); | |
| var firstObj = $(newArray[0]); | |
| var lastObj = $(newArray[newArray.length - 1]); | |
| if (nextObj.is('.correridor, .processing, .sold, .reserved, .wheelchair') || prevObj.is('.correridor, .processing, .sold, .reserved , .wheelchair') || firstObj.is('.edge, .wheelchair') || lastObj.is('.edge, .wheelchair')) { | |
| buyable = true; | |
| } else if ((nextObj.next().is('.available') && !(nextObj.next().is('.wheelchair')) && nextObj.is('.available') && !(nextObj.is('.edge'))) && (prevObj.prev().is('.available') && !(prevObj.prev().is('.wheelchair')) && prevObj.is('.available') && !(prevObj.is('.edge')))) { | |
| buyable = true; | |
| } | |
| if (!(buyable)) { | |
| buyableWarning(); | |
| } | |
| return buyable; | |
| } | |
| function checkBuyableNew(ele) { | |
| var buyable = true; | |
| var nextObj = $(ele).next(); | |
| var prevObj = $(ele).prev(); | |
| var firstObj = $(ele); | |
| var lastObj = $(ele); | |
| var seat_row = []; | |
| var edge_index = []; | |
| var seat_row_substr = []; | |
| var row_number = $(ele).data('row'); | |
| var seat_id = $(ele).data('seatid'); | |
| $("#main span").each(function (index) { | |
| var span_row = $(this).data('row'); | |
| var regex_code = 0; | |
| if (span_row == row_number) { | |
| if (seat_id == $(this).data('seatid')) { | |
| regex_code = 5; | |
| } | |
| else { | |
| if ($(this).is('.correridor, .processing, .sold, .reserved, .wheelchair')) { | |
| regex_code = 2; | |
| } | |
| else if ($(this).hasClass('selected')) { | |
| regex_code = 1; | |
| } | |
| else if ($(this).hasClass('available') && !($(this).hasClass('selected'))) { | |
| regex_code = 0; | |
| } | |
| } | |
| seat_row.push(regex_code); | |
| } | |
| }); | |
| $.each(seat_row, function (index, code) { | |
| if (code == 2) { | |
| edge_index.push(index); | |
| } | |
| }); | |
| var row_string = seat_row.join(''); | |
| for (var i = 0; i < (edge_index.length - 1); i++) { | |
| seat_row_substr.push(row_string.substring(edge_index[i], (edge_index[i + 1] + 1))); | |
| } | |
| $.each(seat_row_substr, function (index, code) { | |
| if (code.indexOf('5') !== -1) { | |
| row_string = code; | |
| } | |
| }); | |
| row_string = row_string.replace("5", "1"); | |
| $.each(reject_list, function (index, code) { | |
| if (row_string.indexOf(code) !== -1) { | |
| buyable = false; | |
| } | |
| }); | |
| if (buyable == false) { | |
| $.each(accept_list, function (index, code) { | |
| if (row_string.indexOf(code) !== -1) { | |
| buyable = true; | |
| } | |
| }); | |
| } | |
| if (!(buyable)) { | |
| buyableWarning(); | |
| } | |
| return buyable; | |
| } | |
| function checkRemovable(ele) { | |
| var isRemovable = true; | |
| var pre = $(ele).prev(); | |
| var nxt = $(ele).next(); | |
| var seat_row = []; | |
| var edge_index = []; | |
| var seat_row_substr = []; | |
| var row_number = $(ele).data('row'); | |
| var seat_id = $(ele).data('seatid'); | |
| $("#main span").each(function (index) { | |
| var span_row = $(this).data('row'); | |
| var regex_code = 0; | |
| if (span_row == row_number) { | |
| if (seat_id == $(this).data('seatid')) { | |
| regex_code = 5; | |
| } | |
| else { | |
| if ($(this).is('.correridor, .processing, .sold, .reserved, .wheelchair')) { | |
| regex_code = 2; | |
| } | |
| else if ($(this).hasClass('selected')) { | |
| regex_code = 1; | |
| } | |
| else if ($(this).hasClass('available') && !($(this).hasClass('selected'))) { | |
| regex_code = 0; | |
| } | |
| } | |
| seat_row.push(regex_code); | |
| } | |
| }); | |
| $.each(seat_row, function (index, code) { | |
| if (code == 2) { | |
| edge_index.push(index); | |
| } | |
| }); | |
| var row_string = seat_row.join(''); | |
| for (var i = 0; i < (edge_index.length - 1); i++) { | |
| seat_row_substr.push(row_string.substring(edge_index[i], (edge_index[i + 1] + 1))); | |
| } | |
| $.each(seat_row_substr, function (index, code) { | |
| if (code.indexOf('5') !== -1) { | |
| row_string = code; | |
| } | |
| }); | |
| row_string = row_string.replace("5", "0"); | |
| $.each(reject_list, function (index, code) { | |
| if (row_string.indexOf(code) !== -1) { | |
| isRemovable = false; | |
| } | |
| }); | |
| if (isRemovable == false) { | |
| $.each(accept_list, function (index, code) { | |
| if (row_string.indexOf(code) !== -1) { | |
| isRemovable = true; | |
| } | |
| }); | |
| } | |
| return isRemovable; | |
| } | |
| function cleanArray(actual) { | |
| var newArray = new Array(); | |
| for (var i = 0; i < actual.length; i++) { | |
| if (actual[i]) { | |
| newArray.push(actual[i]); | |
| } | |
| } | |
| newArray.sort(function (a, b) { | |
| return a[2] - b[2]; | |
| }); | |
| return newArray; | |
| } | |
| function connectWebViewJavascriptBridge(callback) { | |
| if (window.WebViewJavascriptBridge) { | |
| callback(WebViewJavascriptBridge) | |
| } else { | |
| document.addEventListener( | |
| 'WebViewJavascriptBridgeReady' | |
| , function () { | |
| callback(WebViewJavascriptBridge) | |
| }, | |
| false | |
| ); | |
| } | |
| } | |
| function replaceStr(str, pos, value) { | |
| var arr = str.split(''); | |
| arr[pos] = value; | |
| return arr.join(''); | |
| } | |
| function notification_android(msg) { | |
| if (window.WebViewJavascriptBridge) { | |
| window.WebViewJavascriptBridge.callHandler('seat_plan_arrangement', msg, function (responseData) { | |
| }); | |
| //window.WebViewJavascriptBridge.send(msg,function(responseData) { }); | |
| //lets check and make sure when the seat arrange has changed, the call receiver gets the notification. | |
| console.log("debug check android notification:" + msg); | |
| } else { | |
| document.addEventListener( | |
| 'WebViewJavascriptBridgeReady' | |
| , function () { | |
| window.WebViewJavascriptBridge.callHandler('seat_plan_arrangement', msg, function (responseData) { | |
| }); | |
| //window.WebViewJavascriptBridge.send(msg,function(responseData) { }); | |
| }, | |
| false | |
| ); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#file-seatplan-js-L192 supposes to give the notification if the android receive the message or not.