File indexing completed on 2024-05-05 15:53:11
0001 /* GCompris - lang.js 0002 * 0003 * Copyright (C) Siddhesh suthar <siddhesh.it@gmail.com> (Qt Quick port) 0004 * 0005 * Authors: 0006 * Pascal Georges (pascal.georges1@free.fr) (GTK+ version) 0007 * Holger Kaelberer <holger.k@elberer.de> (Qt Quick port of imageid) 0008 * Siddhesh suthar <siddhesh.it@gmail.com> (Qt Quick port) 0009 * Bruno Coudoin <bruno.coudoin@gcompris.net> (Integration Lang dataset) 0010 * 0011 * SPDX-License-Identifier: GPL-3.0-or-later 0012 */ 0013 .pragma library 0014 .import QtQuick 2.12 as Quick 0015 .import GCompris 1.0 as GCompris 0016 .import "qrc:/gcompris/src/core/core.js" as Core 0017 .import "qrc:/gcompris/src/activities/lang/lang_api.js" as Lang 0018 0019 var lessonModelIndex = 0; 0020 var currentSubLevel = 0; 0021 var items; 0022 var baseUrl = "qrc:/gcompris/src/activities/lang/resource/"; 0023 var dataset 0024 var lessons 0025 var maxWordInLesson = 12 0026 0027 function init(items_) { 0028 items = items_ 0029 lessonModelIndex = 0 0030 currentSubLevel = 0 0031 } 0032 0033 function start() { 0034 lessonModelIndex = 0; 0035 currentSubLevel = 0; 0036 items.imageReview.stop() 0037 items.menuScreen.stop() 0038 0039 var locale = GCompris.ApplicationInfo.getVoicesLocale(items.locale) 0040 0041 // register the voices for the locale 0042 GCompris.DownloadManager.updateResource(GCompris.GCompris.VOICES, {"locale": locale}) 0043 0044 var data = Lang.loadDataset(items.parser, baseUrl, locale); 0045 dataset = data["dataset"]; 0046 items.background.englishFallback = data["englishFallback"]; 0047 0048 // We have to keep it because we can't access content from the model 0049 lessons = Lang.getAllLessons(dataset) 0050 addPropertiesToLessons(lessons) 0051 0052 items.menuModel.clear() 0053 items.menuModel.append(lessons) 0054 savedPropertiesToLessons(items.dialogActivityConfig.activityData) 0055 sortByFavorites(); 0056 0057 if(!items.background.englishFallback) { 0058 items.menuScreen.start(); 0059 } 0060 } 0061 0062 // Insert our specific properties in the lessons 0063 function addPropertiesToLessons(lessons) { 0064 for (var i in lessons) { 0065 // Ceil the wordCount to a maxWordInLesson count 0066 lessons[i]['wordCount'] = 0067 Math.ceil(Lang.getLessonWords(dataset, lessons[i]).length / maxWordInLesson) 0068 * maxWordInLesson 0069 lessons[i]['image'] = lessons[i].content[0].image 0070 lessons[i]['progress'] = 0 0071 lessons[i]['favorite'] = false 0072 // We need to keep a back reference from the model to the lessons array 0073 lessons[i]['lessonIndex'] = i 0074 } 0075 } 0076 0077 // Return a new json that contains all the properties we have to save 0078 function lessonsToSavedProperties() { 0079 var locale = GCompris.ApplicationInfo.getVoicesLocale(items.locale) 0080 var props = {} 0081 for(var i = 0; i < items.menuModel.count; i++) { 0082 var lesson = items.menuModel.get(i) 0083 props[lesson.name] = { 0084 'favorite': lesson['favorite'], 0085 'progress': lesson['progress'] 0086 } 0087 } 0088 return props 0089 } 0090 0091 // Update the lessons based on a previous saving 0092 function savedPropertiesToLessons(dataToSave) { 0093 var locale = GCompris.ApplicationInfo.getVoicesLocale(items.locale) 0094 var props = dataToSave[locale] 0095 for(var i = 0; i < items.menuModel.count; i++) { 0096 var lesson = items.menuModel.get(i) 0097 if(props && props[lesson.name]) { 0098 lesson['favorite'] = props[lesson.name].favorite 0099 lesson['progress'] = props[lesson.name].progress 0100 } else { 0101 lesson['favorite'] = false 0102 lesson['progress'] = 0 0103 } 0104 } 0105 } 0106 0107 function stop() { 0108 } 0109 0110 function initLevel(lessonModelIndex_) { 0111 lessonModelIndex = lessonModelIndex_ 0112 var lessonIndex = items.menuModel.get(lessonModelIndex).lessonIndex 0113 0114 var flatWordList = Lang.getLessonWords(dataset, lessons[lessonIndex]); 0115 // We have to split the works in chunks of maxWordInLesson 0116 items.wordList = [] 0117 var i = 0 0118 while(flatWordList.length > 0) { 0119 items.wordList[i++] = Core.shuffle(flatWordList.splice(0, maxWordInLesson)); 0120 } 0121 // If needed complete the last set to have maxWordInLesson items in it 0122 // We pick extra items from the head of the list 0123 if(items.wordList[i-1].length != maxWordInLesson) { 0124 var flatWordList = Lang.getLessonWords(dataset, lessons[lessonIndex]); 0125 var lastLength = items.wordList[i-1].length 0126 items.wordList[i-1] = 0127 items.wordList[i-1].concat(flatWordList.splice(0, maxWordInLesson - lastLength)) 0128 } 0129 0130 items.imageReview.category = items.categoriesTranslations[lessons[lessonIndex].name] //lessons[lessonIndex].name 0131 0132 // Calc the sublevel to start with 0133 var subLevel = Math.floor(items.menuModel.get(lessonModelIndex)['progress'] / maxWordInLesson) 0134 if(subLevel >= items.wordList.length) 0135 // Level done, start again at level 0 0136 subLevel = 0 0137 0138 items.menuScreen.stop() 0139 items.imageReview.initLevel(subLevel) 0140 } 0141 0142 function launchMenuScreen() { 0143 items.imageReview.stop() 0144 items.menuScreen.start() 0145 } 0146 0147 function sortByFavorites() { 0148 for(var i = 0; i < items.menuModel.count; i++) { 0149 if(items.menuModel.get(i)['favorite']) 0150 items.menuModel.move(i, 0, 1); 0151 } 0152 } 0153 0154 function markProgress() { 0155 // We count progress as a number of image learnt from the lesson start 0156 items.menuModel.get(lessonModelIndex)['progress'] += maxWordInLesson 0157 } 0158 0159 function playWord(word) { 0160 var locale = GCompris.ApplicationInfo.getVoicesLocale(items.locale) 0161 items.audioVoices.stop() 0162 items.audioVoices.clearQueue() 0163 return items.audioVoices.append( 0164 GCompris.ApplicationInfo.getAudioFilePathForLocale(word, locale)) 0165 } 0166 0167 function clearVoiceQueue() { 0168 items.audioVoices.clearQueue() 0169 } 0170 0171 function focusEventInput() { 0172 if(items && items.menuScreen && items.menuScreen.opacity === 1) { 0173 items.menuScreen.forceActiveFocus(); 0174 } else if(items && items.imageReview && items.imageReview.opacity === 1 && items.imageReview.currentMiniGame === -1) { 0175 items.imageReview.forceActiveFocus(); 0176 } else if(items && items.imageReview && items.imageReview.opacity === 1 && items.imageReview.currentMiniGame >= 0) { 0177 items.imageReview.restoreMinigameFocus(); 0178 } 0179 }