|
| 1 | +<html> |
| 2 | +<head> |
| 3 | + <script type="text/javascript" src="../../zepto.min.js"></script> |
| 4 | +<script type="text/javascript"> |
| 5 | + $(document).ready(function(){ |
| 6 | + $("body").append('<p>Main</p>'); |
| 7 | + }); |
| 8 | +var data; |
| 9 | + function showMess(){ |
| 10 | + $("body").append('<p>Main</p>'); |
| 11 | + } |
| 12 | +</script> |
| 13 | +</head> |
| 14 | +<body> |
| 15 | +<script> |
| 16 | + var gui = require('nw.gui'); |
| 17 | + var program = require('../../node_modules/commander'); |
| 18 | + var net = require('net'); |
| 19 | + //command line arguments. |
| 20 | + program |
| 21 | + .option('--auto', 'run app automatic') |
| 22 | + .option('-p, --port <port>', "set port used by socket") |
| 23 | + .parse([ 'node-webkit', 'nw-test' ].concat(gui.App.argv)); |
| 24 | + |
| 25 | + var prot = 13013; |
| 26 | + if (program.port) port = program.port; |
| 27 | + |
| 28 | + |
| 29 | + var windows = []; |
| 30 | + var winattr = {x: 50, y: 50, width: 300, height: 300, |
| 31 | + frame: false, toolbar: false}; |
| 32 | + |
| 33 | + function next(win, arr) { |
| 34 | + winattr.x += 400; |
| 35 | + if (winattr.x > 1600) { winattr.x = 50; } |
| 36 | + arr.push(win); |
| 37 | + } |
| 38 | + |
| 39 | + function newWindow(){ |
| 40 | + var win = gui.Window.open('test.html', winattr); |
| 41 | + next(win, windows); |
| 42 | + return win; |
| 43 | + } |
| 44 | + |
| 45 | + function newBrowserWindow() { |
| 46 | + var win = window.open('test.html', null, 'screenX='+winattr.x+ |
| 47 | + ',screenY='+winattr.y+',width=400,height=300'); |
| 48 | + win = gui.Window.get(win); |
| 49 | + next(win, windows); |
| 50 | + return win; |
| 51 | + } |
| 52 | + |
| 53 | + function fourTimes(f){ |
| 54 | + for (var i = 0; i < 4; i++) f(); |
| 55 | + } |
| 56 | + |
| 57 | + function testWindows(){ |
| 58 | + for (var i = 0; i < windows.length; i++){ |
| 59 | + if (windows[i].test) windows[i].test(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + gui.Window.get().on('close', function() { |
| 65 | + for (var i = 0; i < windows.length; i++) windows[i].close(true); |
| 66 | + this.close(true); |
| 67 | + }); |
| 68 | + |
| 69 | + |
| 70 | + if (program.auto){ |
| 71 | + var client = net.connect({port: port}); |
| 72 | + client.setEncoding('utf8'); |
| 73 | + |
| 74 | + client.on('data', function(data) { |
| 75 | + if (data == 'newWindow') { |
| 76 | + var win = newWindow(); |
| 77 | + |
| 78 | + win.on('loaded', function() { |
| 79 | + if (win.test) { |
| 80 | + win.test(); |
| 81 | + |
| 82 | + if (win.isTestSuccess()) { |
| 83 | + client.write('ok'); |
| 84 | + } else { |
| 85 | + client.write('call does not success'); |
| 86 | + } |
| 87 | + |
| 88 | + } else { |
| 89 | + client.write('call does not exist'); |
| 90 | + }// if (win.test) |
| 91 | + win.close(); |
| 92 | + }); |
| 93 | + |
| 94 | + }//if (data == 'newWindow') |
| 95 | + |
| 96 | + if (data == 'newBrowserWindow') { |
| 97 | + var win; |
| 98 | + try { |
| 99 | + win = newBrowserWindow(); |
| 100 | + } |
| 101 | + catch (e){ |
| 102 | + client.write('there is error in creating window'); |
| 103 | + return; |
| 104 | + } |
| 105 | + win.on('loaded', function(){ |
| 106 | + if (win.test) { |
| 107 | + win.test(); |
| 108 | + |
| 109 | + if (win.isTestSuccess()) { |
| 110 | + client.write('ok'); |
| 111 | + } else { |
| 112 | + client.write('call does not success'); |
| 113 | + } |
| 114 | + |
| 115 | + } else { |
| 116 | + client.write('call does not exist'); |
| 117 | + }// if (win.test) |
| 118 | + win.close(); |
| 119 | + }); |
| 120 | + |
| 121 | + |
| 122 | + }//if (data == 'newBrowserWindow') |
| 123 | + |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | +</script> |
| 129 | +<button onclick="fourTimes(newWindow)">New gui windows</button> |
| 130 | +<button onclick="fourTimes(newBrowserWindow)">New browser windows</button><br> |
| 131 | +<button onclick="testWindows()">Test windows</button> |
| 132 | +<button onclick="gui.App.quit()">quit</button> |
| 133 | +</body> |
| 134 | +</html> |
0 commit comments