/* $This file is distributed under the terms of the license in /doc/license.txt$ */ var pageManagementUtils = { dataGetterLabelToURI:null,//initialized by custom data dataGetterURIToLabel:null, //initialized from custom data processDataGetterUtils:processDataGetterUtils,//an external class that should exist before this one dataGetterMap:null, menuAction:null, // on initial page setup onLoad:function(){ if (this.disableFormInUnsupportedBrowsers()) { return; } this.mixIn(); this.initReverseURIToLabel(); this.initDataGetterProcessors(); this.initObjects(); this.bindEventListeners(); this.initDisplay(); //if edit, then generate existing content if(this.isEdit()) { this.initExistingContent(); } }, isEdit:function() { if(pageManagementUtils.menuAction != null && pageManagementUtils.menuAction == "Edit") { return true; } return false; }, isAdd:function() { if(pageManagementUtils.menuAction != null && pageManagementUtils.menuAction == "Add") { return true; } return false; }, isAddMenuItem:function() { if(pageManagementUtils.addMenuItem != null && pageManagementUtils.addMenuItem == "true") { return true; } return false; }, initExistingContent:function() { this.generateExistingContentSections(); //display more content button - will need to review how to hit save etc. //Don't need to display this b/c already in appended section //pageManagementUtils.moreContentButton.show(); //Need to have additional save button }, initReverseURIToLabel:function() { if(this.dataGetterLabelToURI != null) { this.dataGetterURIToLabel = {}; for(var label in this.dataGetterLabelToURI) { if(label != undefined) { var uri = this.dataGetterLabelToURI[label]; this.dataGetterURIToLabel[uri] = label; } } } else { //Error condition. } }, initDataGetterProcessors:function() { //data getter processor map should come in from custom data //Go through each and initialize with their class if(pageManagementUtils.processDataGetterUtils != null) { var dataGetterProcessorMap = pageManagementUtils.dataGetterProcessorMap = pageManagementUtils.processDataGetterUtils.dataGetterProcessorMap; $.each(dataGetterProcessorMap, function(key, dataGetterProcessorObject) { //passes class name from data getter label to uri to processor dataGetterProcessorObject.initProcessor(pageManagementUtils.dataGetterLabelToURI[key]); }) } }, disableFormInUnsupportedBrowsers: function() { var disableWrapper = $('#ie67DisableWrapper'); // Check for unsupported browsers only if the element exists on the page if (disableWrapper.length) { if (vitro.browserUtils.isIELessThan8()) { disableWrapper.show(); $('.noIE67').hide(); return true; } } return false; }, mixIn: function() { //Data getter process list input should be retrieved from the custom data // Mix in the custom form utility methods $.extend(this, vitro.customFormUtils); // Get the custom form data from the page $.extend(this, customFormData); $.extend(this, i18nStrings); }, initObjects:function(){ this.counter = 0; this.contentTypeSelect = $("select#typeSelect"); //list of options this.contentTypeSelectOptions = $('select#typeSelect option'); this.classGroupSection = $("section#browseClassGroup"); this.sparqlQuerySection = $("section#sparqlQuery"); this.fixedHTMLSection = $("section#fixedHtml"); this.solrIndividualsSection = $("section#solrIndividuals"); //From original menu management edit this.defaultTemplateRadio = $('input.default-template'); this.customTemplateRadio = $('input.custom-template'); this.selfContainedTemplateRadio = $('input.selfContained-template'); this.customTemplate = $('#custom-template'); //In this version, these don't exist but we can consider this later // this.changeContentType = $('#changeContentType'); // this.selectContentType = $('#selectContentType'); // this.existingContentType = $('#existingContentType'); this.selectClassGroupDropdown = $('select#selectClassGroup'); this.classesForClassGroup = $('section#classesInSelectedGroup'); this.selectedGroupForPage = $('#selectedContentTypeValue'); this.allClassesSelectedCheckbox = $('#allSelected'); this.displayInternalMessage = $('#internal-class label em'); this.pageContentSubmissionInputs = $("#pageContentSubmissionInputs"); this.headerBar = $("section#headerBar"); this.doneButton = $("input#doneWithContent"); this.cancelLink = $("a#cancelContentLink"); this.isMenuCheckbox = $("input#menuCheckbox"); this.menuLinkText = $("input#menuLinkText"); this.menuSection = $("section#menu"); this.pageNameInput = $("input#pageName"); this.pageSaveButton = $("input#pageSave"); this.leftSideDiv = $("div#leftSide"); this.rightSideDiv = $("div#rightSide"); //contentDivs container where content added/existing placed this.savedContentDivs = $("section#contentDivs"); //for solr individuals data getter this.solrAllClassesDropdown = $("select#vclassUri"); }, initDisplay: function(){ //right side components this.contentTypeSelectOptions.eq(0).attr('selected', 'selected'); $('select#selectClassGroup option').eq(0).attr('selected', 'selected'); //Why would you want to hide this? This hides everything // $("section#pageDetails").hide(); this.headerBar.hide(); this.classGroupSection.hide(); this.sparqlQuerySection.hide(); this.fixedHTMLSection.hide(); this.solrIndividualsSection.hide(); this.classesForClassGroup.addClass('hidden'); //left side components //These depend on whether or not this is an existing item or not if(this.isAdd()) { this.defaultTemplateRadio.attr('checked',true); //disable save button this.disablePageSave(); if(!this.isAddMenuItem()) { this.isMenuCheckbox.attr('checked',false); this.menuSection.hide(); } } //populates the dropdown of classes for the solr individuals template //dropdown now populated in template/from form specific data instead of ajax request //this.populateClassForSolrDropdown(); }, //this method can be utilized if using an ajax request to get the vclasses /* //for solr individuals - remember this populates the template class dropdown populateClassForSolrDropdown:function() { //Run ajax query var url = "dataservice?getAllVClasses=1"; //Make ajax call to retrieve vclasses $.getJSON(url, function(results) { //Moved the function to processClassGroupDataGetterContent //Should probably remove this entire method and copy there pageManagementUtils.displayAllClassesForSolrDropdown(results); }); }, displayAllClassesForSolrDropdown:function(results) { if ( results.classes.length == 0 ) { } else { var appendHtml = ""; $.each(results.classes, function(i, item) { var thisClass = results.classes[i]; var thisClassName = thisClass.name; //Create options for the dropdown appendHtml += ""; }); //if there are options to add if(appendHtml != "") { pageManagementUtils.solrAllClassesDropdown.html(appendHtml); } } },*/ bindEventListeners:function(){ this.defaultTemplateRadio.click( function() { pageManagementUtils.customTemplate.addClass('hidden'); //Also clear custom template value so as not to submit it pageManagementUtils.clearInputs(pageManagementUtils.customTemplate); pageManagementUtils.rightSideDiv.show(); //Check to see if there is already content on page, in which case save should be enabled var pageContentSections = $("section[class='pageContent']"); if(pageContentSections.length == 0) { pageManagementUtils.disablePageSave(); } }); this.customTemplateRadio.click( function() { pageManagementUtils.handleSelectCustomTemplate(); }); this.selfContainedTemplateRadio.click( function() { pageManagementUtils.customTemplate.removeClass('hidden'); pageManagementUtils.rightSideDiv.hide(); pageManagementUtils.enablePageSave(); }); this.isMenuCheckbox.click( function() { if ( pageManagementUtils.menuSection.is(':hidden') ) { pageManagementUtils.menuSection.show(); } else { pageManagementUtils.menuSection.hide(); } }); //Collapses the current content and creates a new section of content //Resets the content to be cloned to default settings this.doneButton.click( function() { pageManagementUtils.handleClickDone(); }); this.cancelLink.click( function() { pageManagementUtils.clearSourceTemplateValues(); pageManagementUtils.headerBar.hide(); pageManagementUtils.classGroupSection.hide(); pageManagementUtils.fixedHTMLSection.hide(); pageManagementUtils.sparqlQuerySection.hide(); pageManagementUtils.contentTypeSelectOptions.eq(0).attr('selected', 'selected'); pageManagementUtils.contentTypeSelect.focus(); pageManagementUtils.adjustSaveButtonHeight(); pageManagementUtils.checkSelfContainedRadio(); }); //replacing with menu management edit version which is extended with some of the logic below //This is technically content specific and should be moved into the individual processor classes somehow this.selectClassGroupDropdown.change(function() { pageManagementUtils.chooseClassGroup(); }); this.contentTypeSelect.change( function() { pageManagementUtils.handleContentTypeSelect(); }); //Submission: validate as well as create appropriate hidden json inputs $("form").submit(function (event) { pageManagementUtils.handleFormSubmission(event); }); }, handleSelectCustomTemplate: function() { pageManagementUtils.customTemplate.removeClass('hidden'); pageManagementUtils.rightSideDiv.show(); //Check to see if there is already content on page, in which case save should be enabled var pageContentSections = $("section[class='pageContent']"); if(pageContentSections.length == 0) { pageManagementUtils.disablePageSave(); } }, handleClickDone:function() { var selectedType = pageManagementUtils.contentTypeSelect.val(); var selectedTypeText = $("#typeSelect option:selected").text(); //Hide all sections pageManagementUtils.classGroupSection.hide(); pageManagementUtils.fixedHTMLSection.hide(); pageManagementUtils.sparqlQuerySection.hide(); pageManagementUtils.solrIndividualsSection.hide(); //Reset main content type drop-down pageManagementUtils.contentTypeSelectOptions.eq(0).attr('selected', 'selected'); if ( pageManagementUtils.leftSideDiv.css("height") != undefined ) { pageManagementUtils.leftSideDiv.css("height",""); if ( pageManagementUtils.leftSideDiv.height() < pageManagementUtils.rightSideDiv.height() ) { pageManagementUtils.leftSideDiv.css("height",pageManagementUtils.rightSideDiv.height() + "px"); } } pageManagementUtils.headerBar.hide(); pageManagementUtils.headerBar.text(""); pageManagementUtils.cloneContentArea(selectedType, selectedTypeText); //Reset class group section AFTER cloning not before pageManagementUtils.resetClassGroupSection(); //Clear all inputs values pageManagementUtils.clearSourceTemplateValues(); //If adding browse classgroup, need to remove the classgroup option from the dropdown if(selectedType == "browseClassGroup") { pageManagementUtils.handleAddBrowseClassGroupPageContent(); } //Enable save button now that some content has been selected pageManagementUtils.enablePageSave(); pageManagementUtils.contentTypeSelect.focus(); }, //Form submission handleFormSubmission:function(event) { var validationError = pageManagementUtils.validateMenuItemForm(); //Add any errors from page content sections if necessary // Only validate the content sections if the self contained template section is NOT selected tlw72 if ( !pageManagementUtils.isSelfContainedTemplateChecked() ) { validationError += pageManagementUtils.validatePageContentSections(); } if (validationError == "") { //Check if menu label needs to be page title pageManagementUtils.checkMenuTitleSubmission(); //Create the appropriate json objects if necessary pageManagementUtils.createPageContentForSubmission(); //pageManagementUtils.mapCustomTemplateName(); pageManagementUtils.setUsesSelfContainedTemplateInput(); return true; } else{ $('#error-alert').removeClass('hidden'); $('#error-alert p').html(validationError); event.preventDefault(); return false; } }, checkMenuTitleSubmission:function() { var isMenu = pageManagementUtils.isMenuCheckbox.is(":checked"); var linkText = pageManagementUtils.menuLinkText.val(); if(isMenu && linkText == "") { //substitute with page title instead var pageName = pageManagementUtils.pageNameInput.val(); pageManagementUtils.menuLinkText.val(pageName); } if(!isMenu && linkText.length > 0) { // if the isMenuCheckbox is unchecked, we need to clear the // menuLinkText field; otherwise, the page remains a menu pageManagementUtils.menuLinkText.val(""); } }, //Select content type - this is content type specific //TODO: Find better way to refactor this and/or see if any of this display functionality can be moved into content type processing handleContentTypeSelect:function() { _this = pageManagementUtils; pageManagementUtils.clearSourceTemplateValues(); if ( _this.contentTypeSelect.val() == "browseClassGroup" ) { pageManagementUtils.classGroupSection.show(); pageManagementUtils.fixedHTMLSection.hide(); pageManagementUtils.sparqlQuerySection.hide(); pageManagementUtils.solrIndividualsSection.hide(); pageManagementUtils.headerBar.text(pageManagementUtils.browseClassGroup + " - "); pageManagementUtils.headerBar.show(); $('div#selfContainedDiv').hide(); } if ( _this.contentTypeSelect.val() == "fixedHtml" || _this.contentTypeSelect.val() == "sparqlQuery" || _this.contentTypeSelect.val() == "solrIndividuals") { pageManagementUtils.classGroupSection.hide(); //if fixed html show that, otherwise show sparql results if ( _this.contentTypeSelect.val() == "fixedHtml" ) { pageManagementUtils.headerBar.text(pageManagementUtils.fixedHtml + " - "); pageManagementUtils.fixedHTMLSection.show(); pageManagementUtils.sparqlQuerySection.hide(); pageManagementUtils.solrIndividualsSection.hide(); } else if (_this.contentTypeSelect.val() == "sparqlQuery"){ pageManagementUtils.headerBar.text(pageManagementUtils.sparqlResults + " - "); pageManagementUtils.sparqlQuerySection.show(); pageManagementUtils.fixedHTMLSection.hide(); pageManagementUtils.solrIndividualsSection.hide(); } else { //solr individuals pageManagementUtils.headerBar.text(pageManagementUtils.solrIndividuals + " - "); pageManagementUtils.sparqlQuerySection.hide(); pageManagementUtils.fixedHTMLSection.hide(); pageManagementUtils.solrIndividualsSection.show(); } pageManagementUtils.headerBar.show(); pageManagementUtils.classesForClassGroup.addClass('hidden'); $('div#selfContainedDiv').hide(); } if ( _this.contentTypeSelect.val() == "" ) { pageManagementUtils.classGroupSection.hide(); pageManagementUtils.fixedHTMLSection.hide(); pageManagementUtils.sparqlQuerySection.hide(); pageManagementUtils.solrIndividualsSection.hide(); pageManagementUtils.classesForClassGroup.addClass('hidden'); pageManagementUtils.headerBar.hide(); pageManagementUtils.headerBar.text(""); pageManagementUtils.checkSelfContainedRadio(); } //Collapse any divs for existing content if it exists pageManagementUtils.collapseAllExistingContent(); //adjust save button height pageManagementUtils.adjustSaveButtonHeight(); //Disable save button until the user has clicked done or cancel from the addition pageManagementUtils.disablePageSave(); //If the default template is selected, there is already content on the page, and the user is selecting new content //display alert message that they must select a custom template and select pageManagementUtils.checkTemplateForMultipleContent(_this.contentTypeSelect.val()); }, disablePageSave:function() { pageManagementUtils.pageSaveButton.attr("disabled", "disabled"); pageManagementUtils.pageSaveButton.addClass("disabledSubmit"); }, enablePageSave:function() { pageManagementUtils.pageSaveButton.removeAttr("disabled"); pageManagementUtils.pageSaveButton.removeClass("disabledSubmit"); }, collapseAllExistingContent:function() { var spanArrows = pageManagementUtils.savedContentDivs.find("span.pageContentExpand div.arrow"); spanArrows.removeClass("collapseArrow"); spanArrows.addClass("expandArrow"); pageManagementUtils.savedContentDivs.find("div.pageContentContainer div.pageContentWrapper").slideUp(222); }, //Clear values in content areas that are cloned to create the page content type specific sections //i.e. reset sparql query/class group areas //TODO: Check if reset is more what we need here? clearSourceTemplateValues:function() { //inputs, textareas pageManagementUtils.clearInputs(pageManagementUtils.fixedHTMLSection); pageManagementUtils.clearInputs(pageManagementUtils.sparqlQuerySection); pageManagementUtils.clearInputs(pageManagementUtils.classGroupSection); pageManagementUtils.clearInputs(pageManagementUtils.solrIndividualsSection); }, clearInputs:function($el) { // jquery selector :input selects all input text area select and button elements $el.find("input").each( function() { if ( $(this).attr('id') != "doneWithContent" ) { $(this).val(""); } }); $el.find("textarea").val(""); //resetting class group section as well so selection is reset if type changes $el.find("select option:eq(0)").attr("selected", "selected"); }, checkTemplateForMultipleContent:function(contentTypeSelected) { if(contentTypeSelected != "") { var pageContentSections = $("section[class='pageContent']"); var selectedTemplateValue = $('input:radio[name=selectedTemplate]:checked').val(); //A new section hasn't been added yet so check to see if there is at least one content type already on page if(selectedTemplateValue == "default" && pageContentSections.length >= 1) { //alert the user that they should be picking custom template instead alert(pageManagementUtils.multipleContentWithDefaultTemplateError); //pick custom template $('input:radio[name=selectedTemplate][value="custom"]').attr("checked", true); pageManagementUtils.handleSelectCustomTemplate(); } } }, //Clone content area //When adding a new content type, this function will copy the values from the new content form and generate //the content for the new section containing the content cloneContentArea: function(contentType, contentTypeLabel) { var ctr = pageManagementUtils.counter; var counter = pageManagementUtils.counter; var varOrClass; //Clone the object, renaming ids and copying text area values as well $newContentObj = pageManagementUtils.createCloneObject(contentType, counter); // Get rid of the cancel link; it'll be replaced by a delete link $newContentObj.find('span#cancelContent' + counter).html(''); if ( contentType == "sparqlQuery" || contentType == "fixedHtml" || contentType == "solrIndividuals") { varOrClass = $newContentObj.find('input[name="saveToVar"]').val(); } else if ( contentType == "browseClassGroup" ) { $newContentObj.find('section#classesInSelectedGroup' + counter).removeClass('hidden'); varOrClass = $newContentObj.find('select#selectClassGroup' + counter + ' option:selected').text(); } //For cases where varOrClass might be empty, pass an empty string if(varOrClass == null || varOrClass==undefined) { varOrClass = ""; } //Attach event handlers if they exist pageManagementUtils.bindClonedContentEventHandlers($newContentObj); pageManagementUtils.createClonedContentContainer($newContentObj, counter, contentTypeLabel, varOrClass); //previously increased by 10, just increasing by 1 here pageManagementUtils.counter++; }, //For binding content type specific event handlers should they exist bindClonedContentEventHandlers:function($newContentObj) { var dataGetterProcessorObj = pageManagementUtils.getDataGetterProcessorObject($newContentObj); if($.isFunction(dataGetterProcessorObj.bindEventHandlers)) { dataGetterProcessorObj.bindEventHandlers($newContentObj); } //Bind done event as the done button is within the cloned content pageManagementUtils.bindClonedContentDoneEvent($newContentObj); }, createClonedContentContainer:function($newContentObj, counter, contentTypeLabel, varOrClass) { //Create the container for the new content $newDivContainer = $("
", { id: "divContainer" + counter, "class": "pageContentContainer", html: "" + contentTypeLabel + " - " + varOrClass + "