|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<!-- SUPER simple Office Mix Labs.js API demo to help you get started |
| 5 | + Created by Philip Guo (www.pgbovine.net), 2014-06-27 --> |
| 6 | + |
| 7 | +<head> |
| 8 | + |
| 9 | +<!-- Requirements for Labs.js --> |
| 10 | +<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script> |
| 11 | +<script src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js" type="text/javascript"></script> |
| 12 | +<script src="https://az592748.vo.msecnd.net/sdk/LabsJS-1.0.4/labs-1.0.4.js"></script> |
| 13 | + |
| 14 | +<script> |
| 15 | +// modeled after Kurt's simplelab.ts |
| 16 | +var _labEditor = null; // for Edit mode |
| 17 | +var _labViewer = null; // for View mode |
| 18 | + |
| 19 | +// we can't use console.log, so use #outputDiv as a log |
| 20 | +function myLog(msg) { |
| 21 | + $("#outputDiv").append('<br/>' + msg); |
| 22 | +} |
| 23 | + |
| 24 | +function enterEditMode(firstTime) { |
| 25 | + myLog('enterEditMode()'); |
| 26 | + |
| 27 | + // this was required only in View mode, so kill it |
| 28 | + if (_labViewer) { |
| 29 | + _labViewer.done(function() {_labViewer = null;}); |
| 30 | + } |
| 31 | + |
| 32 | + Labs.editLab(function(err, labEditor) { |
| 33 | + if (labEditor) { |
| 34 | + _labEditor = labEditor; // global |
| 35 | + |
| 36 | + // first-time initialization from the configuration blob: |
| 37 | + if (firstTime) { |
| 38 | + _labEditor.getConfiguration(function(err, configuration) { |
| 39 | + if (configuration) { |
| 40 | + var loadedData = configuration.components[0].data; |
| 41 | + myLog('loadedData: ' + loadedData); |
| 42 | + $("#textInput").val(loadedData); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +function enterViewMode(firstTime) { |
| 51 | + myLog('enterViewMode()'); |
| 52 | + |
| 53 | + // this was required only in Edit mode, so kill it |
| 54 | + if (_labEditor) { |
| 55 | + _labEditor.done(function() {_labEditor = null;}); |
| 56 | + } |
| 57 | + |
| 58 | + Labs.takeLab(function(err, labInstance) { |
| 59 | + if (labInstance) { |
| 60 | + _labViewer = labInstance; |
| 61 | + |
| 62 | + // first-time initialization from the configuration blob: |
| 63 | + if (firstTime) { |
| 64 | + var loadedData = _labViewer.components[0].component.data; |
| 65 | + myLog('loadedData: ' + loadedData); |
| 66 | + $("#textInput").val(loadedData); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +// Helper method to return the lab configuration from the provided data |
| 73 | +function getConfigurationFromData(dat) { |
| 74 | + var appVersion = { major: 1, minor: 0 }; |
| 75 | + var activityComponent = { |
| 76 | + type: Labs.Components.ActivityComponentType, |
| 77 | + name: "", // XXX: can this be blank? |
| 78 | + values: {}, |
| 79 | + data: dat, |
| 80 | + secure: false |
| 81 | + }; |
| 82 | + var configuration = { |
| 83 | + appVersion: appVersion, |
| 84 | + components: [activityComponent], |
| 85 | + name: "", // XXX: can this be blank? |
| 86 | + timeline: null, |
| 87 | + analytics: null |
| 88 | + }; |
| 89 | + |
| 90 | + return configuration; |
| 91 | +} |
| 92 | + |
| 93 | +$(document).ready(function() { |
| 94 | + // To run in https://labsjs.blob.core.windows.net/sdk/LabsJS-1.0.4/labshost.html |
| 95 | + // append "?PostMessageLabHost" to the query string. |
| 96 | + Labs.DefaultHostBuilder = function() { |
| 97 | + if (window.location.href.indexOf("PostMessageLabHost") !== -1) { |
| 98 | + return new Labs.PostMessageLabHost("test", parent, "*"); |
| 99 | + } else { |
| 100 | + return new Labs.OfficeJSLabHost(); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + // this initialization function kicks everything off |
| 105 | + Labs.connect(function (err, connectionResponse) { |
| 106 | + var initialMode = Labs.Core.LabMode[connectionResponse.mode]; |
| 107 | + myLog('Initial mode: ' + initialMode); |
| 108 | + |
| 109 | + if (initialMode == 'Edit') { |
| 110 | + enterEditMode(true); |
| 111 | + } |
| 112 | + else if (initialMode == 'View') { |
| 113 | + enterViewMode(true); |
| 114 | + } |
| 115 | + |
| 116 | + // initialize these callbacks only after Labs.connect is successful |
| 117 | + Labs.on(Labs.Core.EventTypes.ModeChanged, function(data) { |
| 118 | + myLog('ModeChanged: ' + data.mode); |
| 119 | + if (data.mode == 'Edit') { |
| 120 | + enterEditMode(false); |
| 121 | + } |
| 122 | + else if (data.mode == 'View') { |
| 123 | + enterViewMode(false); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + Labs.on(Labs.Core.EventTypes.Activate, function() { |
| 128 | + myLog('Activate'); |
| 129 | + }); |
| 130 | + |
| 131 | + Labs.on(Labs.Core.EventTypes.Deactivate, function() { |
| 132 | + myLog('Deactivate'); |
| 133 | + }); |
| 134 | + |
| 135 | + // update the configuration data whenever the user types into #textInput |
| 136 | + $("#textInput").on('input', function() { |
| 137 | + var dat = $(this).val(); |
| 138 | + if (_labEditor) { |
| 139 | + _labEditor.setConfiguration(getConfigurationFromData(dat), |
| 140 | + function() {} /* empty error handler */); |
| 141 | + } |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
| 145 | +</script> |
| 146 | +</head> |
| 147 | +<body> |
| 148 | + |
| 149 | +<div> |
| 150 | +Data: <input type="text" id="textInput"></input> |
| 151 | +</div> |
| 152 | + |
| 153 | +<div id="outputDiv"></div> |
| 154 | + |
| 155 | +</body> |
| 156 | +</html> |
0 commit comments