Advertisement
Guest User

Application.js

a guest
May 21st, 2019
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 8.23 KB | None | 0 0
  1. //# sourceURL=application.js
  2.  
  3. //
  4. //  application.js
  5. //  Filimo
  6. //
  7. //  Created by Saeed Taheri on 2/26/18.
  8. //  Copyright © 2018 Filimo. All rights reserved.
  9. //
  10.  
  11. /*
  12.  * This file provides an example skeletal stub for the server-side implementation
  13.  * of a TVML application.
  14.  *
  15.  * A javascript file such as this should be provided at the tvBootURL that is
  16.  * configured in the AppDelegate of the TVML application. Note that  the various
  17.  * javascript functions here are referenced by name in the AppDelegate. This skeletal
  18.  * implementation shows the basic entry points that you will want to handle
  19.  * application lifecycle events.
  20.  */
  21.  
  22. const attributeToController = {};
  23. const attributeKeys = [];
  24. var baseURL;
  25. var menubarLoaded = false;
  26. var pendingPlayURL = null;
  27.  
  28. /**
  29.  * @description The onLaunch callback is invoked after the application JavaScript
  30.  * has been parsed into a JavaScript context. The handler is passed an object
  31.  * that contains options passed in for launch. These options are defined in the
  32.  * swift or objective-c client code. Options can be used to communicate to
  33.  * your JavaScript code that data and as well as state information, like if the
  34.  * the app is being launched in the background.
  35.  *
  36.  * The location attribute is automatically added to the object and represents
  37.  * the URL that was used to retrieve the application JavaScript.
  38.  */
  39. App.onLaunch = function(options) {
  40.     baseURL = options.baseURL;
  41.    
  42.     // Specify all the URLs for helper JavaScript files
  43.     const helperScriptURLs = [
  44.         "Utilities/Jalali",
  45.         "Utilities/DocumentLoader",
  46.         "Utilities/DocumentController",
  47.         "Utilities/DataLoader",
  48.         "MenuBarController",
  49.         "HomeDocumentController",
  50.         "LoginController",
  51.         "CategoriesDocumentController",
  52.         "SearchDocumentController",
  53.         "MyMoviesDocumentController",
  54.         "ProductsListDocumentController",
  55.         "ProductDocumentController",
  56.         "SeasonsDocumentController",
  57.         "Index"
  58.     ].map(
  59.         moduleName => `${baseURL}${moduleName}.js`
  60.     );
  61.    
  62.     // Show a loading spinner while additional JavaScript files are being evaluated
  63.     let loadingDocument = createLoadingDocument("فیلیمو");
  64.     if (typeof navigationDocument !== "undefined") {
  65.         navigationDocument.pushDocument(loadingDocument);
  66.     }
  67.  
  68.     evaluateScripts(helperScriptURLs, function(scriptsAreLoaded) {
  69.         if (scriptsAreLoaded) {
  70.         } else {
  71.             const alertDocument = createEvalErrorAlertDocument();
  72.             navigationDocument.replaceDocument(alertDocument, loadingDocument);
  73.             throw new EvalError("Application.js: unable to evaluate scripts.");
  74.         }
  75.     });
  76. }
  77.  
  78. App.onOpenURL = function(url) {
  79.     pendingPlayURL = url
  80.     if (menubarLoaded) {
  81.         playMovieFromURL(pendingPlayURL)
  82.     }
  83. }
  84.  
  85. App.onWillResignActive = function() {
  86.  
  87. }
  88.  
  89. App.onDidEnterBackground = function() {
  90.  
  91. }
  92.  
  93. App.onWillEnterForeground = function() {
  94.    
  95. }
  96.  
  97. App.onDidBecomeActive = function() {
  98.    
  99. }
  100.  
  101. App.onWillTerminate = function() {
  102.    
  103. }
  104.  
  105. function playMovieFromURL(url) {
  106.     if (url == null || url === "") {
  107.         return
  108.     }
  109.     const [protocol, path] = url.split("://");
  110.     const [movieUID, type] = path.split("/")
  111.    
  112.     let documentLoader = new DocumentLoader(baseURL)
  113.     let documentURL = documentLoader.prepareURL("/XMLs/Product.xml")
  114.     let shouldPlayMovie = type === 'play'
  115.     new ProductDocumentController({ documentLoader, documentURL, movieUID, shouldPlayMovie })
  116.     pendingPlayURL = null
  117. }
  118.  
  119. /**
  120.  * Convenience function to create a TVML loading document with a specified title.
  121.  */
  122. function createLoadingDocument(title) {
  123.     title = title || "در حال دریافت اطلاعات …";
  124.  
  125.     const template = `<?xml version="1.0" encoding="UTF-8" ?>
  126.         <document>
  127.             <loadingTemplate>
  128.                 <activityIndicator>
  129.                     <title>${title}</title>
  130.                 </activityIndicator>
  131.             </loadingTemplate>
  132.         </document>
  133.     `;
  134.     return new DOMParser().parseFromString(template, "application/xml");
  135. }
  136.  
  137. /**
  138.  * This convenience function returns an alert template, which can be used to present errors to the user.
  139.  */
  140. var createAlertDocument = function(title, description) {
  141.  
  142.     var alertString = `<?xml version="1.0" encoding="UTF-8" ?>
  143.         <document>
  144.           <alertTemplate>
  145.             <title>${title}</title>
  146.             <description>${description}</description>
  147.           </alertTemplate>
  148.         </document>`
  149.  
  150.     var parser = new DOMParser();
  151.  
  152.     var alertDoc = parser.parseFromString(alertString, "application/xml");
  153.  
  154.     return alertDoc
  155. }
  156.  
  157. /**
  158.  * Convenience function to create a TVML alert document with a title and description.
  159.  */
  160. function createDescriptiveAlertDocument(title, description) {
  161.     const template = `<?xml version="1.0" encoding="UTF-8" ?>
  162.         <document>
  163.             <descriptiveAlertTemplate>
  164.                 <title>${title}</title>
  165.                 <description></description>
  166.             </descriptiveAlertTemplate>
  167.         </document>
  168.     `;
  169.     let doc = (new DOMParser()).parseFromString(template, "application/xml");
  170.     doc.getElementsByTagName("description").item(0).textContent = description;
  171.  
  172.     return doc
  173. }
  174.  
  175. /**
  176.  * Convenience function to create a TVML alert for asking user with two options as answers.
  177.  */
  178. function presentAlertQuestion(title, description, defaultTitle, cancelTitle, defaultHandler) {
  179.     var alertString = `<?xml version="1.0" encoding="UTF-8" ?>
  180.         <document>
  181.           <alertTemplate>
  182.             <title>${title}</title>
  183.             <description>${description}</description>
  184.             <button id="alertDefaultButton">
  185.                 <text>${defaultTitle}</text>
  186.             </button>
  187.             <button id="alertCancelButton">
  188.                 <text>${cancelTitle}</text>
  189.             </button>
  190.           </alertTemplate>
  191.         </document>`
  192.  
  193.     var parser = new DOMParser();
  194.  
  195.     var alertDoc = parser.parseFromString(alertString, "application/xml");
  196.  
  197.     alertDoc.getElementById("alertDefaultButton").addEventListener("select", function(element, event) {
  198.         defaultHandler()
  199.         navigationDocument.dismissModal()
  200.     })
  201.     alertDoc.getElementById("alertCancelButton").addEventListener("select", function(element, event) {
  202.         navigationDocument.dismissModal()
  203.     })
  204.  
  205.     navigationDocument.presentModal(alertDoc)
  206. }
  207.  
  208. /**
  209.  * Convenience function to create a TVML alert for failed evaluateScripts.
  210.  */
  211. function createEvalErrorAlertDocument() {
  212.     const title = "Evaluate Scripts Error";
  213.     const description = [
  214.         "There was an error attempting to evaluate the external JavaScript files.",
  215.         "Please check your network connection and try again later."
  216.     ].join("\n\n");
  217.     return createAlertDocument(title, description);
  218. }
  219.  
  220. /**
  221.  * Convenience function to create a TVML alert for a failed XMLHttpRequest.
  222.  */
  223. function createLoadErrorAlertDocument(url, xhr) {
  224.     const title = (xhr.status) ? `Fetch Error ${xhr.status}` : "Fetch Error";
  225.     const description = `Could not load document:\n${url}\n(${xhr.statusText})`;
  226.     return createAlertDocument(title, description);
  227. }
  228.  
  229. function registerAttributeName(type, func) {
  230.     attributeToController[type] = func;
  231.     attributeKeys.push(type);
  232. }
  233.  
  234. function resolveControllerFromElement(elem) {
  235.     for (let key of attributeKeys) {
  236.         if (elem.hasAttribute(key)) {
  237.             return {
  238.             type: attributeToController[key],
  239.             documentURL: elem.getAttribute(key)
  240.             };
  241.         }
  242.     }
  243. }
  244.  
  245. function toPersianDigits(str) {
  246.     if (str == null) { return null }
  247.     return str.replace(/0/g, "۰")
  248.                 .replace(/1/g, "۱")
  249.                 .replace(/2/g, "۲")
  250.                 .replace(/3/g, "۳")
  251.                 .replace(/4/g, "۴")
  252.                 .replace(/5/g, "۵")
  253.                 .replace(/6/g, "۶")
  254.                 .replace(/7/g, "۷")
  255.                 .replace(/8/g, "۸")
  256.                 .replace(/9/g, "۹")
  257. }
  258.  
  259. function isLoggedIn() {
  260.     if (localStorage.getItem("token") != null
  261.     && localStorage.getItem("username") != null) {
  262.         return true
  263.     }
  264.     return false
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement