-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path60-nesne-gruplarini-yonetmek.htm
194 lines (137 loc) · 5.62 KB
/
60-nesne-gruplarini-yonetmek.htm
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
194
<!DOCTYPE html>
<html>
<head>
<title>Nesne Gruplarını Yönetmek</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- LIBRARY FILES -->
<link rel="stylesheet" type="text/css" href="basic/basic.min.css">
<script src="basic/basic.min.js" type="text/javascript" charset="utf-8"></script>
<script>
/*
- Bir çok uygulamada, kendini tekrar eden parçaları, her zaman görürüz.
- Bu parçalar, örnekteki gibi basit kutular veya
içlerinde bir çok başka bilgiyi de barındıran karmaşık kutular olabilir.
- Mesela, bir mesajlaşma uygulamasındaki, kişi listesi sayfasını ele alalım:
Her bir kutuda; kişi resmi, adı, soyadı ve son mesajlara ait başka bilgiler olabilir.
- Bu örnek, böyle parçalar tasarladığımızda,
onları nasıl kontrol edebileceğimizi gösteriyor.
Diğer bir örnek proje: 37-aranabilen-liste.htm
*/
var SPACE_BETWEEN_BOXES = 2
var colors = ["#689BD2", "tomato", "gold", "seagreen"]
// Başlangıç sayfası
var a0
// Son oluşturulan kutunun id'si
var lastBoxId = 0
// Kutu nesneleri
var boxes = []
var start = function() {
// NESNE: ilk sayfa taşıyıcısı
a0 = Box(0, 0, page.width, page.height)
// NESNE: kutuların taşıyıcısı
a0.b1 = Box()
a0.add(that)
that.height = 44
that.round = 3
that.color = "black"
that.setMotion("left 0.3s, width 0.3s")
// NOT: Kutuları toplu olarak, hareket ettirebilmemizi sağlayacak.
// 8 tane kutu oluştur.
for (var i = 0; i < 8; i++) {
addBox()
}
// NESNE: Yeni kutu ekleme butonu
a0.btnAddNewBox = Button()
a0.add(that)
that.text = "Kutu Ekle"
that.color = "lightgray"
that.minimal = 1
that.center("left")
that.bottom = 60
that.onClick(addBox)
page.onResize(pageResized)
}
// Sayfa boyutu değiştiğinde,
var pageResized = function() {
// Başlangıç sayfasını, ekranı kaplayacak şekilde yeniden ayarla.
a0.width = page.width
a0.height = page.height
// Taşıyıcı kutuyu, hareketsiz olarak ortala.
a0.b1.dontMotion()
a0.b1.center()
// Butonu yeniden konumlandır.
a0.btnAddNewBox.center("left")
a0.btnAddNewBox.bottom = 60
}
// Yeni bir kutu ekle
var addBox = function() {
// Yeni nesnenin ID'si
lastBoxId++
var newBoxName = "box" + lastBoxId
// NESNE: Kutu
a0.b1[newBoxName] = Box(0, SPACE_BETWEEN_BOXES, 40, 40)
a0.b1.add(that)
// id, ismini bizim belirlediğimi bir değişkendir.
that.id = lastBoxId
// Üzerine gelince, basılabilir imleci çıksın.
that.element.style.cursor = "pointer"
that.color = colors[random(0, colors.length - 1)]
that.round = 3
that.onClick(removeBox)
// Kutulara hareket özelliği ekle.
that.setMotion("left 0.3s")
// Kutuların genişliğini rasgele belirle.
// that.width = random(40, 100)
// NESNE: Kutu içideki sayı
a0.b1[newBoxName].lblNumber = Label(0, 0, 40, 25)
a0.b1[newBoxName].add(that)
that.text = str(lastBoxId)
that.textAlign = "center"
that.textColor = "rgba(0, 0, 0, 0.6)"
that.element.style.fontFamily = "opensans-bold"
that.center()
// kutu nesnelerini, diziye ekle.
boxes.push(a0.b1[newBoxName])
refreshBoxes()
}
// Basılan kutuyu sil
var removeBox = function(self) {
removeBoxById(self.id)
}
// id'si verilen kutuyu sil
var removeBoxById = function(id) {
var newBoxes = []
for (var i = 0; i < boxes.length; i++) {
// Basılmayan kutuları yeni bir diziye ekle.
if (boxes[i].id != id) {
newBoxes.push(boxes[i])
} else {
// Basılan kutunun nesnesini sil.
boxes[i].remove()
}
}
// Kutuların tutulduğu diziyi, yenisi ile değiştir.
boxes = newBoxes
refreshBoxes()
println(id + " id'li kutu nesnesi silindi.")
}
// Dizideki kutuları, yan yana diz
var refreshBoxes = function() {
var boxLeft = SPACE_BETWEEN_BOXES
for (var i = 0; i < boxes.length; i++) {
// Kutunun sola olan mesafesini belirle.
boxes[i].left = boxLeft
// Sonraki kutunun sola olacak mesafesini hesapla.
boxLeft += boxes[i].width + SPACE_BETWEEN_BOXES
}
// Taşıyıcının genişliğini ayarla ve ortala.
a0.b1.width = boxLeft
a0.b1.center()
}
</script>
</head>
<body>
<!-- HTML content -->
</body>
</html>