1 /**
  2  * Created by I074155 on 01/03/2016.
  3  * Site Service API
  4  *
  5  */
  6 
  7 (function () {
  8     "use strict";
  9     /*global jQuery*/
 10     jQuery.sap.require("sap.ushell.cpv2.services.model.SiteModel");
 11     jQuery.sap.require("sap.ushell.cpv2.services.utils.SiteAssetService");
 12     jQuery.sap.require("sap.ushell.cpv2.services.utils.WidgetLoaderService");
 13     jQuery.sap.declare("sap.ushell.cpv2.services.cloudServices.SiteService");
 14 
 15     if (!sap.ushell.cpv2) {
 16         sap.ushell.cpv2 = {};
 17         sap.ushell.cpv2.services = {};
 18     }
 19     if (!sap.ushell.cpv2.services.cloudServices) {
 20         sap.ushell.cpv2.services.cloudServices = {};
 21     }
 22 
 23     /**
 24      * @class SiteService
 25      * @constructor
 26      */
 27     sap.ushell.cpv2.services.cloudServices.SiteService = function SiteService() {
 28         this.siteModel = new sap.ushell.cpv2.services.model.SiteModel(window.siteConfig.siteV2);
 29         return this;
 30     };
 31 
 32     function onAppChanged() {
 33         this.refreshPageContext();
 34         var currentIntent = this.siteModel.getPageContext().getIntentAction(),
 35             i;
 36 
 37         if (currentIntent !== this.lastIntent) {
 38             for (i = 0; i < this.appChangedListeners.length; i++) {
 39                 this.appChangedListeners[i](this.getCurrentAppTarget());
 40             }
 41         }
 42 
 43         this.lastIntent = currentIntent;
 44     }
 45 
 46     sap.ushell.cpv2.services.cloudServices.SiteService.prototype = {
 47 
 48         widgets: {},
 49         sections: {},
 50         selectedContentUnits: {},
 51         contentUnits: {},
 52 
 53 
 54         /**
 55          * Gets the menu hierarchy
 56          * @param menu
 57          * @param payload
 58          * @returns {*}
 59          */
 60         getMenuHierarchy: function (menu, payload) {
 61             return this.siteModel.getMenuHierarchy(menu, payload);
 62         },
 63 
 64         /**
 65          * Gets the navigation target of the current  app/page
 66          * @returns {*}
 67          */
 68         getCurrentAppTarget: function () {
 69             this.refreshPageContext();
 70             var pageContext = this.siteModel.getPageContext();
 71             return pageContext ? pageContext.getTarget() : undefined;
 72         },
 73 
 74         /**
 75          * Subscribes to navigation event
 76          * @param fn - function to be called
 77          */
 78         subscribeOnAppNavigation: function (fn) {
 79             if (typeof fn !== "function") {
 80                 return;
 81             }
 82 
 83             if (!this.appChangedListeners) {
 84                 this.appChangedListeners = [];
 85                 //lazy register
 86                 sap.ui.getCore().getEventBus().subscribe("sap.ushell.renderers.fiori2.Renderer", "appOpened", onAppChanged, this);
 87             }
 88             this.appChangedListeners.push(fn);
 89         },
 90 
 91         /**
 92          * Unsubscribes from navigation event
 93          * @param fn - function to be unsubsribed
 94          */
 95         unsubscribeOnAppNavigation: function (fn) {
 96             var i;
 97 
 98             if (this.appChangedListeners && typeof fn === "function") {
 99                 for (i = 0; i < this.appChangedListeners.length; i++) {
100                     if (this.appChangedListeners[i] === fn) {
101                         this.appChangedListeners.splice(i, 1);
102                         break;
103                     }
104                 }
105             }
106         },
107 
108         /**
109          * Returns true if the site is running in runtime mode.
110          * @returns {boolean}
111          */
112         isRuntime: function () {
113             return !this.isPreview();
114         },
115 
116         /**
117          * Returns true if the site is running in design time mode.
118          * @returns {boolean}
119          */
120         isDesignTime: function () {
121             return false;
122         },
123 
124         /**
125          * Returns true if the site is running in preview mode (of SAP Web IDE)
126          * @returns {boolean}
127          */
128         isPreview: function () {
129             return window.getURLParameter("siteId") === "sitePreview";
130         },
131 
132         isDraftPreview: function () {
133             //Not public API
134             return window.getURLParameter("mode") === "draftPreview";
135         },
136 
137         isWebContentPreview: function () {
138             //Not public API
139             return window.getURLParameter("mode") === "webContentPreview";
140         },
141 
142         /**
143          * Returns the settings of the current page.
144          * @returns {*}
145          */
146         getPageSettings: function () {
147             var currentPage = this.siteModel.getPageContext();
148             return currentPage.getSettings();
149         },
150 
151         /**
152          * Returns the settings of the site.
153          * @returns {*}
154          */
155         getSiteSettings: function () {
156             var siteEntity = this.siteModel.getSiteEntity(this.siteModel.getSiteShellContext());
157             return siteEntity.getSettings();
158         },
159 
160         /**
161          * Saves the settings of the current page.
162          * To enable editing of the site settings via the Custom Site Properties editor in the Admin Space, use a key:value pair, where the key name is unique for the site, and the value is a string, number or a Boolean. The property value cannot be an array, an object or a null.
163          * @param settings
164          * @returns {*}
165          */
166         savePageSettings: function (settings) {
167         },
168 
169         /**
170          * Saves the settings of the site.
171          * @param settings
172          * @returns {*}
173          */
174         saveSiteSettings: function (settings) {
175         },
176 
177         refreshPageContext: function refreshPageContext() {
178             var navData = sap.ushell.Container.getService("URLParsing").parseShellHash(window.location.hash);
179             if (navData) {
180                 var intentOfPage = navData.semanticObject + '-' + navData.action;
181                 this.siteModel.setPageContext(intentOfPage);
182             }
183         },
184         applyLazyLoading: function () {
185             if (this.widgetLoaderService) {
186                 this.widgetLoaderService.stop();
187                 delete this.widgetLoaderService;
188             }
189             this.widgetLoaderService = new sap.ushell.cpv2.services.utils.WidgetLoaderService();
190         },
191 
192         isSafeMode: function isSafeMode() {
193             return window.getURLParameter("safeMode") === 'true';
194         },
195 
196         isFeatureWidgetLazyLoadingOn: function () {
197             return (window["sap-hana-uis-fiori-model"] &&
198             window["sap-hana-uis-fiori-model"].toggles
199             && window["sap-hana-uis-fiori-model"].toggles.FEATURE_WIDGET_LAZY_LOADING);
200         },
201 
202         getSiteModel: function getSiteModel() {
203             return this.siteModel;
204         },
205 
206         setSiteModel: function (siteJson) {
207             this.siteModel.setData(siteJson);
208         },
209 
210         getRuntimeWidget: function (key) {
211             return this.widgets[key];
212         },
213 
214         setSelectedContentUnits: function (selectedContentUnits) {
215             this.selectedContentUnits = {};
216             var unitId,
217                 index;
218 
219             for (index in selectedContentUnits) {
220                 unitId = selectedContentUnits[index];
221                 this.selectedContentUnits[unitId] = unitId;
222             }
223         },
224 
225         setContentUnitPressCb: function (callback) {
226             this.contentUnitPressCb = callback;
227         },
228 
229         getContentUnitPressCb: function () {
230             return this.contentUnitPressCb;
231         },
232 
233         addContentUnitWidget: function (key, ref) {
234             this.contentUnits[key] = ref;
235         },
236 
237         removeAllContentUnitWidget: function (key) {
238             this.contentUnits = {};
239         },
240 
241         getAllContentUnitWidgets: function () {
242             return this.contentUnits;
243         },
244 
245         isWidgetSelectedContentUnit: function (unitId) {
246             if (this.selectedContentUnits) {
247                 return this.selectedContentUnits.hasOwnProperty(unitId);
248             }
249         },
250 
251         addRuntimeWidget: function (key, ref) {
252             this.widgets[key] = ref;
253         },
254         addRuntimeSection: function (key, ref) {
255             this.sections[key] = ref;
256         },
257 
258         getRuntimeSection: function (key) {
259             return this.sections[key];
260         },
261 
262         getSections: function () {
263             return this.sections;
264         },
265 
266         cleanRuntimeSections: function () {
267             this.sections = {};
268         },
269 
270         cleanRuntimeWidgets: function () {
271             this.widgets = {};
272         },
273 
274         /**
275          * Returns the ID of the current site.
276          * @returns { String}
277          */
278         getSiteID: function () {
279             function getURLParameter(name) {
280                 var value = (new RegExp(name + '=' + '(.+?)(&|$)', 'i').exec(location.search) || [null, null])[1];
281                 return value !== null ? decodeURIComponent(value) : undefined;
282             }
283 
284             if (!this._siteID) {
285                 if (sap.flp.FaasInitUtils.isAdminSpace()) {
286                     this._siteID = sap.flp.FaasInitUtils.getCurrentDesignerSiteId();
287                 } else {
288                     this._siteID = getURLParameter("siteId") || getURLParameter("siteID");
289                 }
290             }
291             return this._siteID || jQuery.sap.getObject('window.siteConfig.site.ID', 4);
292         },
293 
294         getAppOrMenuTitle: function () {
295             var appTitle = "",
296                 siteModel = this.getSiteModel(),
297                 appTarget = this.getCurrentAppTarget();
298 
299             if (appTarget && appTarget.semanticObject && appTarget.action) {
300                 //get the menu page title
301                 appTitle = siteModel.findAppByIntentInTheMenu(appTarget.semanticObject, appTarget.action);
302                 if (!appTitle) {
303                     //get the 'Pages & Apps' page title
304                     var pageIntent = siteModel.getPageByIntent(appTarget.semanticObject + "-" + appTarget.action);
305                     appTitle = pageIntent.getTitle();
306                 }
307             }
308 
309             return appTitle;
310         },
311 
312         isWidgetContainAssets: function (manifestObj) {
313             var manifest = new sap.ushell.cpv2.services.model.Application(manifestObj);
314             return manifest.getEcmResources().length > 0;
315         },
316 
317         widgetAssetsInEcm: function (widgetAlias) {
318             if (!this.siteAssetsService) {
319                 this.siteAssetsService = new sap.ushell.cpv2.services.utils.SiteAssetService(this.getSiteID());
320             }
321             var widget = this.getSiteModel().getWidget(widgetAlias);
322             var widgetResources = widget.getEcmResources();
323             return this.siteAssetsService.validateResourcesListInEcm(widgetResources);
324         },
325 
326         getContentUnit: function (component) {
327             var manifestAsApp = new sap.ushell.cpv2.services.model.Application(component.getMetadata().getManifest()),
328                 contentUnitId = manifestAsApp.getContentUnitId(),
329                 siteModel = this.getSiteModel();
330 
331             return siteModel.getContentUnit(contentUnitId);
332         }
333     };
334 
335     sap.ushell.cpv2.services.cloudServices.SiteService.hasNoAdapter = true;
336 }());