helper.js
3.73 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
isClear = true;
Helper = {
_append: function(type, attrs) {
return $('<' + type + '/>', attrs).appendTo('body');
},
_attrs: function(data, options) {
var attrs = options || {};
if (data.prefix === '#') {
attrs.id = data.id;
} else {
attrs['class'] = data.id;
}
return attrs;
},
_select: function() {
return '' +
'<option value="Cancel this rating!">cancel hint default</option>' +
'<option value="cancel-hint-custom">cancel hint custom</option>' +
'<option value="">cancel number default</option>' +
'<option value="0">cancel number custom</option>' +
'<option value="bad">bad hint imutable</option>' +
'<option value="1">bad number imutable</option>' +
'<option value="targetText">targetText is setted without targetKeep</option>' +
'<option value="gorgeous">targetFormat</option>';
},
_save: function(id) {
var data = { prefix: id.charAt(0), id: id.slice(1) };
this.ids = this.ids || [];
this.ids.push(id);
return data;
},
clear: function() {
for (var i = 0; i < this.ids.length; i++) {
$(this.ids[i]).remove();
}
},
create: function(id, type, options) {
type = type || 'div';
var data = this._save(id),
attrs = this._attrs(data, options);
return this._append(type, attrs);
},
click: function(el, integer, decimal) {
this.mouseTrigger('mousemove', el, integer, decimal);
this.mouseTrigger('click', el, integer, decimal);
},
mouseData: function(el, integer, decimal) {
var
stars = el.children('img:not(.raty-cancel)'),
star = stars.eq(integer),
width = star[0].width || parseFloat(star.css('font-size'));
fraction = width / 10,
left = star.offset().left,
pageX = left + fraction * decimal + 0.1;
if (console && console.log) {
console.debug(integer + '.' + decimal, ':', 'left:', left, 'width:', width, 'fraction (width/10):', fraction, 'pageX:', pageX, 'fractions (decimal * fraction)', decimal * fraction);
}
return { el: star, pageX: pageX }
},
mouseTrigger: function(action, el, integer, decimal) {
var
data = this.mouseData(el, integer, decimal),
evt = $.Event(action, { pageX: data.pageX });
data.el.trigger(evt);
},
mousemove: function(el, integer, decimal) {
this.mouseTrigger('mousemove', el, integer, decimal);
},
target: function(id, type, options) {
type = type || 'div';
var data = this._save(id),
attrs = this._attrs(data, options);
if (type === 'select') {
attrs.html = this._select();
}
return this._append(type, attrs);
}
};
function context(description, spec) {
describe(description, spec);
}
function build() {
$('body').append('<div id="element"></div>');
}
function buildDivTarget() {
$('body').append('<div id="hint"></div>');
}
function buildComboboxTarget() {
$('body').append(
'<select id="hint">' +
'<option value="Cancel this rating!">cancel hint default</option>' +
'<option value="cancel-hint-custom">cancel hint custom</option>' +
'<option value="">cancel number default</option>' +
'<option value="0">cancel number custom</option>' +
'<option value="bad">bad hint imutable</option>' +
'<option value="1">bad number imutable</option>' +
'<option value="targetText">targetText is setted without targetKeep</option>' +
'<option value="score: bad">targetFormat</option>' +
'</select>'
);
}
function buildTextareaTarget() {
$('body').append('<textarea id="hint"></textarea>');
}
function buildTextTarget() {
$('body').append('<input id="hint" type="text" />');
}
function clear() {
if (isClear) {
$('#element').remove();
$('#hint').remove();
}
}