This repository was archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathindex.js
More file actions
393 lines (338 loc) · 10.7 KB
/
index.js
File metadata and controls
393 lines (338 loc) · 10.7 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import React, { Component, PropTypes } from 'react';
import Link from 'react-router/lib/Link';
import { Element } from 'react-scroll';
import Copy from '../Copy';
import debug from '../../helpers/debug';
const styles = require('./style.scss');
/* eslint-disable no-unused-vars */
const CHAR_TYPE_WORD = 1;
const CHAR_TYPE_END = 2;
const CHAR_TYPE_PAUSE = 3;
const CHAR_TYPE_RUB = 4;
const CHAR_TYPE_SAJDAH = 5;
/* eslint-enable no-unused-vars */
export default class Ayah extends Component {
static propTypes = {
isSearched: PropTypes.bool,
ayah: PropTypes.object.isRequired,
bookmarked: PropTypes.bool.isRequired,
bookmarkActions: PropTypes.object,
mediaActions: PropTypes.object.isRequired,
match: PropTypes.array,
isSearch: PropTypes.bool,
isPlaying: PropTypes.bool,
isAuthenticated: PropTypes.bool,
tooltip: PropTypes.string,
currentWord: PropTypes.any, // gets passed in an integer, null by default
audioActions: PropTypes.object.isRequired
};
static defaultProps = {
currentWord: null,
isSearched: false
};
componentDidMount() {
function getOffset(elem) {
var offsetLeft = 0, offsetTop = 0;
do {
if ( !isNaN( elem.offsetLeft ) )
{
offsetLeft += elem.offsetLeft;
offsetTop += elem.offsetTop;
}
} while( elem = elem.offsetParent );
return {left: offsetLeft, top: offsetTop};
}
var targets = document.querySelectorAll( '[rel=tooltip]' ),
target = false,
tooltip = false,
title = false,
tip = false;
for(var i = 0; i < targets.length; i++) {
targets[i].addEventListener("mouseenter", function() {
target = this;
tip = target.getAttribute("title");
tooltip = document.createElement("div");
tooltip.id = "tooltip";
if(!tip || tip == "")
return false;
target.removeAttribute("title");
tooltip.style.opacity = 0;
tooltip.innerHTML = tip;
document.body.appendChild(tooltip);
var init_tooltip = function()
{
console.log(getOffset(target));
// set width of tooltip to half of window width
if(window.innerWidth < tooltip.offsetWidth * 1.5)
tooltip.style.maxWidth = window.innerWidth / 2;
else
tooltip.style.maxWidth = 340;
var pos_left = getOffset(target).left + (target.offsetWidth / 2) - (tooltip.offsetWidth / 2),
pos_top = getOffset(target).top - tooltip.offsetHeight - 10;
console.log("top is", pos_top);
if( pos_left < 0 )
{
pos_left = getOffset(target).left + target.offsetWidth / 2 - 20;
tooltip.classList.add("left");
}
else
tooltip.classList.remove("left");
if( pos_left + tooltip.offsetWidth > window.innerWidth )
{
pos_left = getOffset(target).left - tooltip.offsetWidth + target.offsetWidth / 2 + 20;
tooltip.classList.add("right");
}
else
tooltip.classList.remove("right");
if( pos_top < 0 )
{
var pos_top = getOffset(target).top + target.offsetHeight + 15;
tooltip.classList.add("top");
}
else
tooltip.classList.remove("top");
// adding "px" is very important
tooltip.style.left = pos_left + "px";
tooltip.style.top = pos_top + "px";
tooltip.style.opacity = 1;
};
init_tooltip();
window.addEventListener("resize", init_tooltip);
var remove_tooltip = function() {
tooltip.style.opacity = 0;
document.querySelector("#tooltip") && document.body.removeChild(document.querySelector("#tooltip"));
target.setAttribute("title", tip );
};
target.addEventListener("mouseleave", remove_tooltip );
tooltip.addEventListener("click", remove_tooltip );
});
}
}
shouldComponentUpdate(nextProps) {
const conditions = [
this.props.ayah !== nextProps.ayah,
this.props.bookmarked !== nextProps.bookmarked,
this.props.tooltip !== nextProps.tooltip,
this.props.currentWord !== nextProps.currentWord
];
if (this.props.match) {
conditions.push(this.props.match.length !== nextProps.match.length);
}
return conditions.some(condition => condition);
}
handlePlay(ayah) {
const { isPlaying, audioActions } = this.props;
const { pause, setAyah, play } = audioActions;
if (isPlaying) {
pause();
}
setAyah(ayah);
play();
}
renderTranslations() {
const { ayah, match } = this.props;
const array = match || ayah.content || [];
return array.map((content, index) => {
const arabic = new RegExp(/[\u0600-\u06FF]/);
const character = content.text;
const isArabic = arabic.test(character);
const lang = (content.name || content.resource.name).replace(/\s+/g, '-').toLowerCase();
return (
<div
className={`${styles.translation} ${isArabic && 'arabic'} translation`}
key={index}
>
<h4 className="montserrat">{content.name || content.resource.name}</h4>
<h2 className={`${isArabic ? 'text-right' : 'text-left'} text-translation times-new`}>
<small dangerouslySetInnerHTML={{__html: content.text}} className={`${styles[lang] || 'times-new'}`} />
</h2>
</div>
);
});
}
renderMedia() {
const { ayah, mediaActions } = this.props;
if (!ayah.mediaContent.length) return false;
return (
<div>
{
ayah.mediaContent.map((content, index) => (
<div
className={`${styles.translation} translation`}
key={index}
>
<h2 className="text-translation times-new">
<small>
<a
className="pointer"
onClick={() => mediaActions.setMedia(content)}
data-metrics-event-name="Media Click"
data-metrics-media-content-url={content.url}
data-metrics-media-content-id={content.id}
data-metrics-media-content-ayah-key={ayah.ayahKey}
>
Watch lecture by {content.resource.name}
</a>
</small>
</h2>
</div>
))
}
</div>
);
}
renderText() {
const { ayah, audioActions: { setCurrentWord }, tooltip } = this.props;
if (!ayah.words[0].code) {
return false;
}
// position is important as it will differentiate between words and symbols, see 2:25:13
let position = -1;
const text = ayah.words.map((word, index) => {
let id = null;
const isLast = ayah.words.length === index + 1;
const className = `${word.className} ${word.highlight ? word.highlight : ''}`;
if (word.charTypeId === CHAR_TYPE_WORD) {
position = position + 1;
id = `word-${word.ayahKey.replace(/:/, '-')}-${position}`;
} else {
id = `${word.className}-${word.codeDec}`; // just don't include id
}
if (word.translation || word.transliteration) {
const tooltipContent = word[tooltip];
return (
<b
key={word.code}
id={id}
rel="tooltip"
onClick={(event) => setCurrentWord(event.target.dataset.key)}
data-key={`${word.ayahKey}:${position}`}
className={`${className}`}
title={tooltipContent}
dangerouslySetInnerHTML={{__html: word.code}}
/>
);
}
const label = isLast ? {'title': `Verse ${ayah.ayahNum}`} : {}
return (
<b
id={id}
onClick={(event) => setCurrentWord(event.target.dataset.key)}
data-key={`${word.ayahKey}:${position}`}
rel="tooltip"
className={`${className} ${isLast} pointer`}
key={word.code}
dangerouslySetInnerHTML={{__html: word.code}}
{...label}
/>
);
});
return (
<h1 className={`${styles.font} text-right text-arabic`}>
{text}
<p
dir="rtl"
lang="ar"
className={`text-tashkeel text-p${ayah.pageNum}`}
dangerouslySetInnerHTML={{__html: ayah.textTashkeel}}
/>
</h1>
);
}
renderPlayLink() {
const { isSearched, ayah } = this.props;
if (!isSearched) {
return (
<a
onClick={() => this.handlePlay(ayah.ayahKey)}
className="text-muted"
>
<i className="ss-icon ss-play" /> Play
</a>
);
}
return false;
}
renderCopyLink() {
const { isSearched, ayah: { textTashkeel } } = this.props;
if (!isSearched) {
return (
<Copy text={textTashkeel} />
);
}
return false;
}
renderBookmark() {
const { ayah, bookmarked, isAuthenticated, bookmarkActions } = this.props;
if (!isAuthenticated) return false;
if (bookmarked) {
return (
<a
onClick={() => bookmarkActions.removeBookmark(ayah.ayahKey)}
className="text-muted"
>
<strong><i className="ss-icon ss-bookmark" /> Bookmarked</strong>
</a>
);
}
return (
<a
onClick={() => bookmarkActions.addBookmark(ayah.ayahKey)}
className="text-muted"
>
<i className="ss-icon ss-bookmark" /> Bookmark
</a>
);
}
renderAyahBadge() {
const { isSearched } = this.props;
const content = (
<h4>
<span className={`label label-default ${styles.label}`}>
{this.props.ayah.surahId}:{this.props.ayah.ayahNum}
</span>
</h4>
);
if (isSearched) {
return (
<Link
to={`/${this.props.ayah.surahId}/${this.props.ayah.ayahNum}`}
data-metrics-event-name="Ayah:Searched:Link"
>
{content}
</Link>
);
}
return (
<Link
to={`/${this.props.ayah.surahId}:${this.props.ayah.ayahNum}`}
data-metrics-event-name="Ayah:Link"
>
{content}
</Link>);
}
renderControls() {
return (
<div className={`col-md-1 ${styles.controls}`}>
{this.renderAyahBadge()}
{this.renderPlayLink()}
{this.renderCopyLink()}
{this.renderBookmark()}
</div>
);
}
render() {
const { ayah } = this.props;
debug('component:Ayah', `Render ${this.props.ayah.ayahNum}`);
return (
<Element name={`ayah:${ayah.ayahKey}`} className={`row ${styles.container}`}>
{this.renderControls()}
<div className="col-md-11">
{this.renderText()}
{this.renderTranslations()}
{this.renderMedia()}
</div>
</Element>
);
}
}