-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
193 lines (186 loc) · 7.21 KB
/
script.js
File metadata and controls
193 lines (186 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var index = 0;
$('.box').click(function() {
if ( index++ % 2 === 0 ) {
if ( $(this).children().hasClass('active')){
index --;
} else{
$(this).children('.x').addClass('active');
}
} else {
if ( $(this).children().hasClass('active')){
index --;
} else{
$(this).children('.o').addClass('active');
}
}
var xChild1 = $('.box:nth-child(1)').children('.x').hasClass('active'),
oChild1 = $('.box:nth-child(1)').children('.o').hasClass('active'),
xChild2 = $('.box:nth-child(2)').children('.x').hasClass('active'),
oChild2 = $('.box:nth-child(2)').children('.o').hasClass('active'),
xChild3 = $('.box:nth-child(3)').children('.x').hasClass('active'),
oChild3 = $('.box:nth-child(3)').children('.o').hasClass('active'),
xChild4 = $('.box:nth-child(4)').children('.x').hasClass('active'),
oChild4 = $('.box:nth-child(4)').children('.o').hasClass('active'),
xChild5 = $('.box:nth-child(5)').children('.x').hasClass('active'),
oChild5 = $('.box:nth-child(5)').children('.o').hasClass('active'),
xChild6 = $('.box:nth-child(6)').children('.x').hasClass('active'),
oChild6 = $('.box:nth-child(6)').children('.o').hasClass('active'),
xChild7 = $('.box:nth-child(7)').children('.x').hasClass('active'),
oChild7 = $('.box:nth-child(7)').children('.o').hasClass('active'),
xChild8 = $('.box:nth-child(8)').children('.x').hasClass('active'),
oChild8 = $('.box:nth-child(8)').children('.o').hasClass('active'),
xChild9 = $('.box:nth-child(9)').children('.x').hasClass('active'),
oChild9 = $('.box:nth-child(9)').children('.o').hasClass('active');
if( xChild1 && xChild2 && xChild3 ){
$('.top-horiz').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild1 && oChild2 && oChild3 ){
$('.top-horiz').addClass('active');
$('.o-wins').addClass('active');
} else if( xChild4 && xChild5 && xChild6 ){
$('.mid-horiz').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild4 && oChild5 && oChild6 ){
$('.mid-horiz').addClass('active');
$('.o-wins').addClass('active');
} else if( xChild7 && xChild8 && xChild9 ){
$('.bottom-horiz').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild7 && oChild8 && oChild9 ){
$('.mid-horiz').addClass('active');
$('.o-wins').addClass('active');
} else if( xChild1 && xChild4 && xChild7 ){
$('.left-vert').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild1 && oChild4 && oChild7 ){
$('.left-vert').addClass('active');
$('.o-wins').addClass('active');
} else if( xChild2 && xChild5 && xChild8 ){
$('.mid-vert').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild2 && oChild5 && oChild8 ){
$('.mid-vert').addClass('active');
$('.o-wins').addClass('active');
} else if( xChild3 && xChild6 && xChild9 ){
$('.right-vert').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild3 && oChild6 && oChild9 ){
$('.right-vert').addClass('active');
$('.o-wins').addClass('active');
} else if( xChild1 && xChild5 && xChild9 ){
$('.ltr-diag').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild1 && oChild5 && oChild9 ){
$('.ltr-diag').addClass('active');
$('.o-wins').addClass('active');
} else if( xChild3 && xChild5 && xChild7 ){
$('.rtl-diag').addClass('active');
$('.x-wins').addClass('active');
} else if( oChild3 && oChild5 && oChild7 ){
$('.rtl-diag').addClass('active');
$('.o-wins').addClass('active');
}
});
$('.js-wins, .reset').on('click', function(){
$('.js-wins').removeClass('active');
$('.x').removeClass('active');
$('.o').removeClass('active');
$('.red-line').removeClass('active');
index = 0;
});
/**
* Listener for name changes
*/
$('body').on('keyup', 'input.name', function(){
if($(this).parent().attr('id') === 'player1'){
if($(this).val() === ''){
$('div.x-wins').html('X Wins! <span>Play Again?</span>');
}else{
$('div.x-wins').html($(this).val()+' Wins! <span>Play Again?</span>');
}
}else if ($(this).parent().attr('id') === 'player2') {
if($(this).val() === ''){
$('div.o-wins').html('O Wins! <span>Play Again?</span>');
}else{
$('div.o-wins').html($(this).val()+' Wins! <span>Play Again?</span>');
}
}
});
/**
* Listener for removeing images
*/
$('body').on('click', '.remove-img', function(){
$(this).parent().children('.img_upload').css('background-image', '');
if($(this).parent().parent().attr('id') === 'player1'){
$('div.x-wins').html($(this).val()+' Wins! <span>Play Again?</span>');
$('span.x').each(function(){
$(this).css('background-image', '');
$(this).text('X');
});
}else if ($(this).parent().parent().attr('id') === 'player2') {
$('div.o-wins').html($(this).val()+' Wins! <span>Play Again?</span>');
$('span.o').each(function(){
$(this).css('background-image', '');
$(this).text('O');
});
}
});
/**
* Listener for image uploads. Calls below function on change.
*/
$('body').on('change', '.img_upload', function(){
readURL(this, $(this));
});
/**
* Reads a filestream to an image target. Reisizes image before applying background preview
* @param input
* @param img_target
*/
function readURL(input, img_target) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (readerEvent) {
var image = new Image();
image.onload = function (imageEvent) {
// Resize the image
var canvas = document.createElement('canvas'),
max_size = 768,
width = image.width,
height = image.height;
if (width > height) {
if (width > max_size) {
height *= max_size / width;
width = max_size;
}
} else {
if (height > max_size) {
width *= max_size / height;
height = max_size;
}
}
canvas.width = width;
canvas.height = height;
canvas.getContext('2d').drawImage(image, 0, 0, width, height);
var dataUrl = canvas.toDataURL('image/jpeg');
$.event.trigger({
type: "imageResized",
url: dataUrl
});
img_target.css('background-image', 'url(' + dataUrl + ')');
if(img_target.parent().parent().attr('id') === 'player1'){
$('span.x').each(function(){
$(this).css('background-image', 'url(' + dataUrl + ')');
$(this).text('');
});
}else if (img_target.parent().parent().attr('id') === 'player2') {
$('span.o').each(function(){
$(this).css('background-image', 'url(' + dataUrl + ')');
$(this).text('');
});
}
};
image.src = readerEvent.target.result;
};
reader.readAsDataURL(input.files[0]);
}
};