aboutsummaryrefslogtreecommitdiff
path: root/js/io/ui/new-file-dialog/new-file-workflow-controller.js
blob: c2be687a18f3b55b9b0bde5c65b458057c893f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var Montage = require("montage/core/core").Montage,
    Popup = require("montage/ui/popup/popup.reel").Popup,
    newFileOptionsNavigatorModule = require("js/io/ui/new-file-dialog/new-file-options-navigator.reel"),
    newFileWorkflowModelModule = require("js/io/ui/new-file-dialog/new-file-workflow-model").NewFileWorkflowModel;
    saveAsModule = require("js/io/ui/save-as-dialog.reel");

//singleton
var NewFileWorkflowController =  exports.NewFileWorkflowController = Montage.create(require("montage/ui/component").Component, {
    /**
     * Register a listener for showPicker event
     */
    deserializedFromTemplate:{
        writable:false,
        enumerable:true,
        value:function(){
            var that = this;

            this.eventManager.addEventListener("saveAs", function(evt){
                var data = evt._event.data || {};//data will contain the current file name, directory location and callback
                that.showSaveAsDialog(data);
            }, false);
        }
    },

    model:{
        enumerable:true,
        value: null
    },

    showNewFileDialog:{
        writable:false,
        enumerable:true,
        value:function(data){
            this.model = newFileWorkflowModelModule;

            //read file descriptor to populate model
            this.model.projectTypeData = this.loadDescriptor("js/io/templates/descriptor.json");

            //get default project type
            this.model.defaultProjectType = "js/io/templates/files/html.txt";
            this.model.callback = data.callback || null;
            this.model.callbackScope = data.callbackScope || null;

            //populate the last opened folder first, if none then populate default root
            var sessionStorage = window.sessionStorage;
            var lastSelectedProjectType = sessionStorage.getItem("lastSelectedProjectType");

            if(!!lastSelectedProjectType){
                this.model.defaultProjectType = lastSelectedProjectType;
            }

            var newFileOptionsNav = newFileOptionsNavigatorModule.NewFileOptionsNavigator.create();
            newFileOptionsNav.newFileModel = this.model;

            var popup = Popup.create();
            popup.content = newFileOptionsNav;
            popup.modal = true;
            popup.type = "newFileDialog";
            popup.show();

            newFileOptionsNav.popup = popup;//handle to be used for hiding the popup

        }
    },

    showSaveAsDialog:{
        writable:false,
        enumerable:true,
        value:function(data){
            var fileName = data.fileName || "filename.txt";
            var folderUri = data.folderUri || "/Documents";

            //render modal dialog
            var saveAsDialogContainer = document.createElement("div");
            saveAsDialogContainer.id = "saveAsDialog";

            //elements needs to be on DOM to be drawn
            document.getElementById('modalContainer').appendChild(saveAsDialogContainer);

            var saveAsDialog = saveAsModule.SaveAsDialog.create();
            saveAsDialog.fileName = fileName;
            saveAsDialog.folderUri = folderUri;
            saveAsDialog.callback = data.callback;
            saveAsDialog.callbackScope = data.callbackScope;
            saveAsDialog.element = saveAsDialogContainer;

            //remove after rendering and add in modal dialog
            document.getElementById('modalContainer').removeChild(saveAsDialogContainer);

            var popup = Popup.create();
            popup.content = saveAsDialog;
            popup.modal = true;
            popup.type = "saveAsDialog";
            popup.show();

            saveAsDialog.popup = popup;//handle to be used for hiding the popup
        }
    },

    loadDescriptor:{
        value: function(descriptorPath){
            var content = null, descriptorObj=null;
            var xhr = new XMLHttpRequest();
            xhr.open("GET", descriptorPath, false);
            xhr.send();
            if (xhr.readyState === 4) {
                if(xhr.status == 200) {
                    content = xhr.responseText;
                }
            }
            if(!!content && (content.length > 0)){
                try{
                    descriptorObj = JSON.parse(content);
                }catch(e){
                    console.log(e,stack);
                }
            }
            return descriptorObj;
        }
    }
});