(function (namespace) { // RSS Behavior Common Code. Used for the process of showing or not showing items in RSS feeds. 'use strict'; var config = { selector: { rssBehavior: '[name=RssBehavior]', showInRssFeed: "[name='ShowInRssFeed']", rssConfigurationEnabled: '[name=RssConfigurationEnabled]', $rssBehaviorMultipleCategory: function (categoryId) { return $("[name='RssBehavior." + categoryId + "']"); } }, localization: { includeInRssFeedQuestion: 'Would you like to include this in the RSS feed? Click OK to include it. Click Cancel to exclude it.' }, attributes: { addItemBehavior: 'data-add-item-behavior', modifyItemBehavior: 'data-modify-item-behavior' } }; /* Description: Asks the user about RSS behavior and sets a standard field for it if needed ([name='ShowInRssFeed']). Also supports multi category contexts. Requirements for use: For any use case you must define a hidden input field [name='ShowInRssFeed']". For item modifies within the context of a single category you must define a hidden input indicating the rss behavior for the category [Name=RssBehavior}] For multicategory selection you must define a hidden input indicating the RSS behavior for a given category [Name=RssBehavior.{categoryId}] Example Markup: Example Usage (all of these will set [name='ShowInRssFeed'] as appropriate; should be called before sending server data; server should save data in field name "ShowInRssFeed" to the item): SetFieldForRssBehavior({ categoryId: 5, add:true }); SetFieldForRssBehavior({ modify: true }); SetFieldForRssBehavior({ add: true }); SetFieldForRssBehavior({ add: true, showInRssFeedSelector: 'Agenda.ShowInRssField' }); // Overrides the default selector for ShowInRssField */ namespace.SetFieldForRssBehavior = function (options) { options = options || {}; validateOptions(options); // prevent the developer using this function from doing something silly. cloneInElementsFromParentWindowIfNeeded(); if (!verifyRssConfiguration()) { return; } var $rssBehavior = $(config.selector.rssBehavior); config.selector.showInRssFeed = options.showInRssFeedSelector || config.selector.showInRssFeed; // override selector for show in rss field if needed. if (options.categoryId) { $rssBehavior = config.selector.$rssBehaviorMultipleCategory(options.categoryId); if (!$rssBehavior.length) { console.warn("A category was passed, but the required fields were not setup. (Expected " + config.selector.$rssBehaviorMultipleCategory(options.categoryId).selector + "); Falling back to general setting ("+config.selector.rssBehavior+")"); $rssBehavior = $(config.selector.rssBehavior); if (!$rssBehavior.length) { throw "Required fields were not setup. Rss beahavior is unabled to be determined. Expected " + $rssBehavior.selector; } } } UpdateShowInRssFeed($rssBehavior, options.add, options.modify); }; function verifyRssConfiguration() { var $showInRssFeed = $(config.selector.showInRssFeed); var $rssConfigurationEnabled = $(config.selector.rssConfigurationEnabled); if (!$rssConfigurationEnabled.length) { throw "Rss configuration (the module parameter setting) was not enabled or disabled. Please define " + config.selector.rssConfigurationEnabled; } if ($rssConfigurationEnabled.val().toLowerCase() === 'false') { $showInRssFeed.val('true'); return false; } return true; } // This function is added "robustness". Basically some partials are reused inside LiveEdit IFrames on the front end; but also used on the backend. We also have common wrappers. // I ran into a problem where I had to have these fields in the common wrapper because the wrapper itself needed it; but cannot have it in the child partials without a bunch of filtering as then // sometimes multiple elements of the same name occur breaking equality comparisions (e.g. duplicate fields go to server as "true,true" as opposed to "true" for a single one). // This function will clone in needed elements from the parent for the duration of an iframe session if needed. function cloneInElementsFromParentWindowIfNeeded() { if (window !== top) { // if this is an iframe var $jQueryParent = window.parent.$; var $showInRssFeed = $(config.selector.showInRssFeed); var $showInRssFeedParent = $jQueryParent(config.selector.showInRssFeed); var $rssConfigurationEnabled = $(config.selector.rssConfigurationEnabled); var $rssConfigurationEnabledParent = $jQueryParent(config.selector.rssConfigurationEnabled); var $rssBehaviors = $('[' + config.attributes.addItemBehavior + ']'); var $rssBehaviorsParent = $jQueryParent('[' + config.attributes.addItemBehavior + ']'); if (!$showInRssFeed.length && $showInRssFeedParent.length) { // if show in rss feed exists in parent window but not in our window; let's clone it. $(document.forms).prepend($showInRssFeedParent.clone()); } if (!$rssConfigurationEnabled.length && $rssConfigurationEnabledParent.length) { // if rss configuration in rss feed exists in parent window but not in our window; let's clone it. $(document.forms).prepend($rssConfigurationEnabledParent.clone()); } if (!$rssBehaviors.length && $rssBehaviorsParent.length) { // if rss behaviors exist in parent window but not in our window; let's clone them. $(document.forms).prepend($rssBehaviorsParent.clone()); } } } function askUserAboutRss() { var $showInRssFeed = $(config.selector.showInRssFeed); if (!$showInRssFeed.length) { throw "ShowInRssFeed field is not defined. Expected " + $showInRssFeed.selector; } if (confirm(config.localization.includeInRssFeedQuestion)) { $showInRssFeed.val('true'); } else { $showInRssFeed.val('false'); } } // Primarily a helper for multiple category situations. namespace.UpdateShowInRssFeed = function ($rssBehavior, add, modify) { var $showInRssFeed = $(config.selector.showInRssFeed); if (add) { if (!$rssBehavior.is("[" + config.attributes.modifyItemBehavior + "]")) { throw "Modify behavior was not set. Please set [or check spelling of] attribute " + config.attributes.addItemBehavior + ' on ' + $rssBehavior.selector; } if ($rssBehavior.attr(config.attributes.addItemBehavior) === "0" || $rssBehavior.attr(config.attributes.addItemBehavior) === 'AlwaysAsk') { askUserAboutRss(); } else { $showInRssFeed.val($rssBehavior.attr(config.attributes.addItemBehavior) === '1' || $rssBehavior.attr(config.attributes.addItemBehavior) === 'IncludeInRssFeed'); } } else if (modify) { if (!$rssBehavior.is("[" + config.attributes.modifyItemBehavior + "]")) { throw "Modify behavior was not set. Please set [or check spelling of] attribute " + config.attributes.modifyItemBehavior + ' on ' + $rssBehavior.selector; } if ($rssBehavior.attr(config.attributes.modifyItemBehavior) === '0' || $rssBehavior.attr(config.attributes.modifyItemBehavior) === 'AlwaysAsk') { askUserAboutRss(); } else { $showInRssFeed.val($rssBehavior.attr(config.attributes.modifyItemBehavior) === '1' || $rssBehavior.attr(config.attributes.modifyItemBehavior) === 'IncludeInRssFeed'); } } else { throw "Add or Modify was not specified. The caller of this function should specify one."; } }; function validateOptions(options) { if (!options.modify && !options.add) { throw "The caller of this function must declare if this operation is adding or modifying."; } if (options.modify && options.add) { throw "The caller of this function cannot simulataneously be adding and modifying an item for rss behavior."; } } // namespace.checkForRssPrompt = function (selector) { var $rssBehavior = $(selector); if (!$rssBehavior.length) { throw "ShowInRssFeed field is not defined. Expected " + $rssBehavior.selector; } if ($rssBehavior.val() == '0') { showInRssFeed = confirm(config.localization.includeInRssFeedQuestion); } else { showInRssFeed = $rssBehavior.val() == '1'; } return showInRssFeed; }; // namespace.attachRssPromptHandlersForSave = function () { $(document).ready(function () { $('.bigButton.nextAction.modify').unbind('click').click(function (e) { var rssBehaviorModifyItems = checkForRssPrompt("#RssBehaviorModifyItems") ? '1' : '2'; $('#RssBehaviorModifyItems').val(rssBehaviorModifyItems); }); }); }; // namespace.handleRssSaveAndSend = function (isPublish) { showInRssFeed = checkForRssPrompt(isPublish ? '#RssBehaviorAddItems' : '#RssBehaviorModifyItems'); $('#ShowInRssFeed').val(showInRssFeed ? "1" : "0"); }; namespace.showInRssFeed = true; }(window));