|
| 1 | +/***************************************************************************** |
| 2 | + * Open MCT, Copyright (c) 2014-2017, United States Government |
| 3 | + * as represented by the Administrator of the National Aeronautics and Space |
| 4 | + * Administration. All rights reserved. |
| 5 | + * |
| 6 | + * Open MCT is licensed under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + * |
| 17 | + * Open MCT includes source code licensed under additional open source |
| 18 | + * licenses. See the Open Source Licenses file (LICENSES.md) included with |
| 19 | + * this source code distribution or the Licensing information page available |
| 20 | + * at runtime from the About dialog for additional information. |
| 21 | + *****************************************************************************/ |
| 22 | +define( |
| 23 | + [], |
| 24 | + function () { |
| 25 | + |
| 26 | + var SNAPSHOT_TEMPLATE = '<mct-representation key="\'draggedEntry\'"'+ |
| 27 | + 'class="t-rep-frame holder"'+ |
| 28 | + 'mct-object="selObj">'+ |
| 29 | + '</mct-representation>'; |
| 30 | + |
| 31 | + var NEW_TASK_FORM = { |
| 32 | + name: "Create a Notebook Entry", |
| 33 | + hint: "Please select one Notebook", |
| 34 | + sections: [{ |
| 35 | + rows: [{ |
| 36 | + name: 'Entry', |
| 37 | + key: 'entry', |
| 38 | + control: 'textarea', |
| 39 | + required: true, |
| 40 | + "cssClass": "l-textarea-sm" |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'Embed Type', |
| 44 | + key: 'withSnapshot', |
| 45 | + control: 'snapshot-select', |
| 46 | + "options": [ |
| 47 | + { |
| 48 | + "name": "Link and Snapshot", |
| 49 | + "value": true |
| 50 | + }, |
| 51 | + { |
| 52 | + "name": "Link only", |
| 53 | + "value": false |
| 54 | + } |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + name: 'Embed', |
| 59 | + key: 'embedObject', |
| 60 | + control: 'embed-control' |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'Save in Notebook', |
| 64 | + key: 'saveNotebook', |
| 65 | + control: 'locator', |
| 66 | + validate: validateLocation |
| 67 | + }] |
| 68 | + }] |
| 69 | + }; |
| 70 | + |
| 71 | + function NewEntryContextual($compile,$rootScope,dialogService,notificationService,linkService,context) { |
| 72 | + context = context || {}; |
| 73 | + this.domainObject = context.selectedObject || context.domainObject; |
| 74 | + this.dialogService = dialogService; |
| 75 | + this.notificationService = notificationService; |
| 76 | + this.linkService = linkService; |
| 77 | + this.$rootScope = $rootScope; |
| 78 | + this.$compile = $compile; |
| 79 | + } |
| 80 | + |
| 81 | + function validateLocation(newParentObj) { |
| 82 | + return newParentObj.model.type == 'notebook'; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + NewEntryContextual.prototype.perform = function () { |
| 87 | + |
| 88 | + var self = this; |
| 89 | + var domainObj = this.domainObject; |
| 90 | + var notification = this.notificationService; |
| 91 | + var linkService = this.linkService; |
| 92 | + var dialogService = this.dialogService; |
| 93 | + var rootScope = this.$rootScope; |
| 94 | + rootScope.newEntryText = ''; |
| 95 | + // Create the overlay element and add it to the document's body |
| 96 | + this.$rootScope.selObj = domainObj; |
| 97 | + this.$rootScope.selValue = ""; |
| 98 | + var newScope = rootScope.$new(); |
| 99 | + newScope.selObj = domainObj; |
| 100 | + newScope.selValue = ""; |
| 101 | + var element = this.$compile(SNAPSHOT_TEMPLATE)(newScope); |
| 102 | + //newScope.$destroy(); |
| 103 | + |
| 104 | + this.$rootScope.$watch("snapshot", setSnapshot); |
| 105 | + |
| 106 | + function setSnapshot(value){ |
| 107 | + if(value === "annotationCancelled"){ |
| 108 | + rootScope.snapshot = rootScope.lastValue; |
| 109 | + rootScope.lastValue = ''; |
| 110 | + }else if(value && value !== rootScope.lastValue){ |
| 111 | + var overlayModel = { |
| 112 | + title: NEW_TASK_FORM.name, |
| 113 | + message: NEW_TASK_FORM.message, |
| 114 | + structure: NEW_TASK_FORM, |
| 115 | + value: {'entry': rootScope.newEntryText || ""} |
| 116 | + }; |
| 117 | + |
| 118 | + // Provide result from the model |
| 119 | + function resultGetter() { |
| 120 | + return overlayModel.value; |
| 121 | + } |
| 122 | + |
| 123 | + rootScope.currentDialog = overlayModel; |
| 124 | + |
| 125 | + dialogService.getDialogResponse( |
| 126 | + "overlay-dialog", |
| 127 | + overlayModel, |
| 128 | + resultGetter |
| 129 | + ).then(addNewEntry); |
| 130 | + |
| 131 | + rootScope.lastValue = value; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + function addNewEntry(options){ |
| 136 | + options.selectedModel = options.embedObject.getModel(); |
| 137 | + options.cssClass= options.embedObject.getCapability('type').typeDef.cssClass; |
| 138 | + if(self.$rootScope.snapshot){ |
| 139 | + options.snapshot= self.$rootScope.snapshot; |
| 140 | + self.$rootScope.snapshot = undefined; |
| 141 | + }else{ |
| 142 | + options.snapshot = undefined; |
| 143 | + } |
| 144 | + |
| 145 | + if(!options.withSnapshot){ |
| 146 | + options.snapshot = ''; |
| 147 | + } |
| 148 | + |
| 149 | + createSnap(options); |
| 150 | + } |
| 151 | + |
| 152 | + function createSnap(options){ |
| 153 | + options.saveNotebook.useCapability('mutation', function(model) { |
| 154 | + var entries = model.entries; |
| 155 | + var lastEntry= entries[entries.length-1]; |
| 156 | + if(lastEntry==undefined || lastEntry.text || lastEntry.embeds){ |
| 157 | + model.entries.push({ |
| 158 | + 'createdOn':Date.now(), |
| 159 | + 'text': options.entry, |
| 160 | + 'embeds':[{'type':options.embedObject.getId(), |
| 161 | + 'id':''+Date.now(), |
| 162 | + 'cssClass':options.cssClass, |
| 163 | + 'name':options.selectedModel.name, |
| 164 | + 'snapshot':options.snapshot |
| 165 | + }] |
| 166 | + }); |
| 167 | + }else{ |
| 168 | + model.entries[entries.length-1] = { |
| 169 | + 'createdOn':Date.now(), |
| 170 | + 'text': options.entry, |
| 171 | + 'embeds':[{'type':options.embedObject.getId(), |
| 172 | + 'id':''+Date.now(), |
| 173 | + 'cssClass':options.cssClass, |
| 174 | + 'name':options.selectedModel.name, |
| 175 | + 'snapshot':options.snapshot |
| 176 | + }] |
| 177 | + }; |
| 178 | + } |
| 179 | + }); |
| 180 | + |
| 181 | + notification.info({ |
| 182 | + title: "Notebook Entry created" |
| 183 | + }); |
| 184 | + } |
| 185 | + }; |
| 186 | + |
| 187 | + NewEntryContextual.appliesTo = function (context) { |
| 188 | + var domainObject = context.domainObject; |
| 189 | + return domainObject && domainObject.hasCapability("notebook") && |
| 190 | + domainObject.getCapability("notebook").isNotebook(); |
| 191 | + }; |
| 192 | + |
| 193 | + return NewEntryContextual; |
| 194 | + } |
| 195 | +); |
0 commit comments