1// ----------------------------------------------------------------------- 2// <copyright file="cxa.common.messaging.js" company="Sitecore Corporation"> 3// Copyright (c) Sitecore Corporation 1999-2018 4// </copyright> 5// <summary>Provides a common tool</summary> 6// ----------------------------------------------------------------------- 7// Copyright 2017-2018 Sitecore Corporation A/S 8// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 9// except in compliance with the License. You may obtain a copy of the License at 10// http://www.apache.org/licenses/LICENSE-2.0 11// 12// Unless required by applicable law or agreed to in writing, software distributed under the 13// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 14// either express or implied. See the License for the specific language governing permissions 15// and limitations under the License. 16// ------------------------------------------------------------------------------------------- 17 18(function (root, factory) { 19 'use strict'; 20 var instance; 21 22 if (typeof define === 'function' && define.amd) { 23 // use AMD define funtion to support AMD modules if in use 24 define('CXA/Common/MessageContext', ['exports'], factory); 25 } else if (typeof exports === 'object') { 26 // to support CommonJS 27 factory(exports); 28 } 29 30 function createContext() { 31 var context = { 32 Handlers: {}, // repsoitory to store message handlers 33 LastHandlerId: -1, // last used handler Id, initialized to -1 34 StandardChannels: { Error: "standard.error", Warning: "standard.warning", Info: "standard.info", Command: "standard.command", Info: "standard.success" }, 35 StandardCommands: { ClearAll: "command.clearAll", ClearErrors: "command.clearErrors", ClearWarnings: "command.clearWarnings", ClearInfo: "command.clearInfo" } 36 }; 37 return context; 38 } 39 40 function getContext() { 41 if (!instance) { 42 instance = createContext(); 43 } 44 return instance; 45 } 46 // browser global variable 47 var MessageContext = getContext(); 48 root.MessageContext = MessageContext; 49 factory(MessageContext); 50}(this, function (MessageContext) { 51 'use strict'; 52 53 function channelExists(channel) { 54 return MessageContext.Handlers.hasOwnProperty(channel); 55 } 56 57 function registerChannel(channel) { 58 if (!MessageContext.Handlers.hasOwnProperty(channel)) { 59 MessageContext.Handlers[channel] = {}; 60 } 61 } 62 63 function hasHandlers(channel) { 64 if (!channelExists) { return false; } 65 var handlers = MessageContext.Handlers[channel]; 66 var handlerId; 67 for (handlerId in handlers) { 68 if (handlers.hasOwnProperty(handlerId)) { 69 return true; 70 } 71 } 72 return false; 73 } 74 75 function notifyHandler(source, handler, message, description) { 76 handler(source, message, null, description); 77 } 78 79 function dispatch(source, channel, message, description) { 80 var handlers = MessageContext.Handlers[channel]; 81 if (channelExists(channel)) { 82 for (var handlerId in handlers) { 83 if (handlers.hasOwnProperty(handlerId)) { 84 notifyHandler(source, handlers[handlerId], message, description); 85 } 86 } 87 } 88 } 89 90 // -------------------- Service Public Functions --------------------------------------------------------------------- 91 92 MessageContext.PublishMessage = function (source, channel, message) { 93 if (!hasHandlers(channel)) { return false; } 94 var dispatchFunction = dispatch(source, channel, message); 95 // run asyncronously 96 setTimeout(dispatchFunction, 0); 97 return true; 98 }; 99 100 MessageContext.PublishCommand = function (source, command) { 101 if (!hasHandlers(MessageContext.StandardChannels.Command)) { return false; } 102 var dispatchFunction = dispatch(source, MessageContext.StandardChannels.Command, command); 103 // run asyncronously 104 setTimeout(dispatchFunction, 0); 105 return true; 106 }; 107 108 MessageContext.PublishError = function (source, message, data, description) { 109 if (!hasHandlers(MessageContext.StandardChannels.Error)) { return false; }
110 var dispatchFunction = dispatch(source, MessageContext.StandardChannels.Error, message, description); 111 // run asyncronously 112 setTimeout(dispatchFunction, 0); 113 return true; 114 }; 115 116 MessageContext.PublishWarning = function (source, message, data, description) { 117 if (!hasHandlers(MessageContext.StandardChannels.Warning)) { return false; } 118 var dispatchFunction = dispatch(source, MessageContext.StandardChannels.Warning, message, description); 119 // run asyncronously 120 setTimeout(dispatchFunction, 0); 121 return true; 122 }; 123 124 MessageContext.PublishInfo = function (source, message, data, description) { 125 if (!hasHandlers(MessageContext.StandardChannels.Info)) { return false; } 126 var dispatchFunction = dispatch(source, MessageContext.StandardChannels.Info, message, description); 127 // run asyncronously 128 setTimeout(dispatchFunction, 0); 129 return true; 130 }; 131 132 MessageContext.PublishSuccess = function (source, message, data, description) { 133 if (!hasHandlers(MessageContext.StandardChannels.Success)) { return false; } 134 var dispatchFunction = dispatch(source, MessageContext.StandardChannels.Success, message, description); 135 // run asyncronously 136 setTimeout(dispatchFunction, 0); 137 return true; 138 }; 139 140 MessageContext.SubscribeMessageHandler = function (channel, handler) { 141 if (typeof handler !== 'function') { 142 return false; 143 } 144 145 registerChannel(channel); 146 var newHandlerId = 'h_' + String(++MessageContext.LastHandlerId); 147 MessageContext.Handlers[channel][newHandlerId] = handler; 148 149 return newHandlerId; 150 }; 151 152 MessageContext.SubscribeErrorHandler = function (handler) { 153 if (typeof handler !== 'function') { 154 return false; 155 } 156 var channel = MessageContext.StandardChannels.Error; 157 registerChannel(channel); 158 var newHandlerId = 'h_' + String(++MessageContext.LastHandlerId); 159 MessageContext.Handlers[channel][newHandlerId] = handler; 160 161 return newHandlerId; 162 }; 163 164 MessageContext.SubscribeInfoHandler = function (handler) { 165 if (typeof handler !== 'function') { 166 return false; 167 } 168 var channel = MessageContext.StandardChannels.Info; 169 registerChannel(channel); 170 var newHandlerId = 'h_' + String(++MessageContext.LastHandlerId); 171 MessageContext.Handlers[channel][newHandlerId] = handler; 172 173 return newHandlerId; 174 }; 175 176 MessageContext.SubscribeWarningHandler = function (handler) { 177 if (typeof handler !== 'function') { 178 return false; 179 } 180 var channel = MessageContext.StandardChannels.Warning; 181 registerChannel(channel); 182 var newHandlerId = 'h_' + String(++MessageContext.LastHandlerId); 183 MessageContext.Handlers[channel][newHandlerId] = handler; 184 185 return newHandlerId; 186 }; 187 188 MessageContext.SubscribeCommandHandler = function (handler) { 189 if (typeof handler !== 'function') { 190 return false; 191 } 192 var channel = MessageContext.StandardChannels.Command; 193 registerChannel(channel); 194 var newHandlerId = 'h_' + String(++MessageContext.LastHandlerId); 195 MessageContext.Handlers[channel][newHandlerId] = handler; 196 197 return newHandlerId; 198 }; 199 200 MessageContext.UnSubscribeHandler = function (handlerId) { 201 for (var channel in MessageContext.Handlers) { 202 if (MessageContext.Handlers.hasOwnProperty(channel)) { 203 var channelHandlers = MessageContext.Handlers[channel]; 204 if (channelHandlers[handlerId]) { 205 delete channelHandlers[handlerId]; 206 return handlerId; 207 } 208 } 209 } 210 211 return null; 212 }; 213 214 // sends a clearAll command to all components that have subscribed to MessageContext.StandardChannels.Command channel 215 MessageContext.ClearAllMessages = function () { 216 MessageContext.PublishCommand(this, MessageContext.StandardCommands.ClearAll); 217 }; 218}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.