Bloom.Book.BookData.SetUpDisplayOfTopicInBook C# (CSharp) Method

SetUpDisplayOfTopicInBook() private method

Topics are uni-directional value, reactâ„¢-style. The UI tells the book to change the topic key, and then eventually the page/book is re-evaluated and the appropriate topic is displayed on the page. To differentiate from fields with @data-book, which are two-way, the topic on the page instead has a @data-derived attribute (in the data-div, it is still a data-book... perhaps that too could change to something like data-book-source, but it's not clear to me yet, so.. not yet). When the topic is changed, the javascript sends c# a message with the new English Key for the topic is set in the data-div, and then the page is re-computed. That leads to this method, which grabs the english topic (which serves as the 'key') from the datadiv. It then finds the placeholder for the topic and fills it with the best translation it can find.
private SetUpDisplayOfTopicInBook ( DataSet data ) : void
data DataSet
return void
        private void SetUpDisplayOfTopicInBook(DataSet data)
        {
            var topicPageElement = this._dom.SelectSingleNode("//div[@data-derived='topic']");
            if (topicPageElement == null)
            {
                //old-style. here we don't have the data-derived, so we need to avoid picking from the datadiv
                topicPageElement = this._dom.SelectSingleNode("//div[not(id='bloomDataDiv')]//div[@data-book='topic']");
                if (topicPageElement == null)
                {
                    //most unit tests do not have complete books, so this not surprising. It just means we don't have anything to do
                    return;
                }
            }
            //clear it out what's there now
            topicPageElement.RemoveAttribute("lang");
            topicPageElement.InnerText = "";

            NamedMutliLingualValue topicData;

            var parentOfTopicDisplayElement = ((XmlElement)(topicPageElement.ParentNode));
            //this just lets us have css rules that vary if there is a topic (allows other text to be centered instead left-aligned)
            //we'll change it later if we find there is a topic
            parentOfTopicDisplayElement.SetAttribute("data-have-topic", "false");

            //if we have no topic element in the data-div
            //leave the field in the page with an empty text.
            if (!data.TextVariables.TryGetValue("topic", out topicData))
            {
                return;
            }

            //we use English as the "key" for topics.
            var englishTopic = topicData.TextAlternatives.GetExactAlternative("en");

            //if we have no topic, just clear it out from the page
            if (string.IsNullOrEmpty(englishTopic) || englishTopic == "NoTopic")
                return;

            parentOfTopicDisplayElement.SetAttribute("data-have-topic", "true");

            var stringId = "Topics." + englishTopic;

            //get the topic in the most prominent language for which we have a translation
            var langOfTopicToShowOnCover = _collectionSettings.Language1Iso639Code;
            if (LocalizationManager.GetIsStringAvailableForLangId(stringId, _collectionSettings.Language1Iso639Code))
            {
                langOfTopicToShowOnCover = _collectionSettings.Language1Iso639Code;
            }
            else if (LocalizationManager.GetIsStringAvailableForLangId(stringId, _collectionSettings.Language2Iso639Code))
            {
                langOfTopicToShowOnCover = _collectionSettings.Language2Iso639Code;
            }
            else if (LocalizationManager.GetIsStringAvailableForLangId(stringId, _collectionSettings.Language3Iso639Code))
            {
                langOfTopicToShowOnCover = _collectionSettings.Language3Iso639Code;
            }
            else
            {
                langOfTopicToShowOnCover = "en";
            }

            var bestTranslation = LocalizationManager.GetDynamicStringOrEnglish("Bloom", stringId, englishTopic,
                "this is a book topic", langOfTopicToShowOnCover);

            //NB: in a unit test environment, GetDynamicStringOrEnglish is going to give us the id back, which is annoying.
            if (bestTranslation == stringId)
                bestTranslation = englishTopic;

            topicPageElement.SetAttribute("lang", langOfTopicToShowOnCover);
            topicPageElement.InnerText = bestTranslation;
        }