UNPKG

5.31 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24
25/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
26/*global define, $, PathUtils */
27
28/**
29 * PreferencesDialogs
30 *
31 */
32define(function (require, exports, module) {
33 "use strict";
34
35 require("thirdparty/path-utils/path-utils.min");
36
37 var Dialogs = require("widgets/Dialogs"),
38 PreferencesManager = require("preferences/PreferencesManager"),
39 ProjectManager = require("project/ProjectManager"),
40 StringUtils = require("utils/StringUtils"),
41 Strings = require("strings");
42
43 /**
44 * Validate that text string is a valid base url which should map to a server folder
45 * @param {String} url
46 * @return {String} empty string if valid, otherwise error string
47 */
48 function _validateBaseUrl(url) {
49 var result = "";
50 // empty url means "no server mapping; use file directly"
51 if (url === "") {
52 return result;
53 }
54
55 var obj = PathUtils.parseUrl(url);
56 if (!obj) {
57 result = Strings.BASEURL_ERROR_UNKOWN_ERROR;
58 } else if (obj.protocol !== "http:" && obj.protocol !== "https:") {
59 result = StringUtils.format(Strings.BASEURL_ERROR_INVALID_PROTOCOL, obj.protocol);
60 } else if (obj.search !== "") {
61 result = StringUtils.format(Strings.BASEURL_ERROR_SEARCH_DISALLOWED, obj.search);
62 } else if (obj.hash !== "") {
63 result = StringUtils.format(Strings.BASEURL_ERROR_HASH_DISALLOWED, obj.hash);
64 } else {
65 var index = url.search(/[ \^\[\]\{\}<>\\"\?]+/);
66 if (index !== -1) {
67 result = StringUtils.format(Strings.BASEURL_ERROR_INVALID_CHAR, url[index]);
68 }
69 }
70
71 return result;
72 }
73
74 /**
75 * Show a dialog that shows the project preferences
76 * @param {String} baseUrl - initial value
77 * @param {String} errorMessage - error to display
78 * @return {$.Promise} A promise object that will be resolved when user successfully enters
79 * project settings and clicks OK, or rejected if user clicks Cancel.
80 */
81 function showProjectPreferencesDialog(baseUrl, errorMessage) {
82
83 var $dlg,
84 $title,
85 $baseUrlControl,
86 promise;
87
88 promise = Dialogs.showModalDialog(Dialogs.DIALOG_ID_PROJECT_SETTINGS)
89 .done(function (id) {
90 if (id === Dialogs.DIALOG_BTN_OK) {
91 var baseUrlValue = $baseUrlControl.val();
92 var result = _validateBaseUrl(baseUrlValue);
93 if (result === "") {
94 ProjectManager.setBaseUrl(baseUrlValue);
95 } else {
96 // Re-invoke dialog with result (error message)
97 showProjectPreferencesDialog(baseUrlValue, result);
98 }
99 }
100 });
101
102 // Populate project settings
103 $dlg = $(".project-settings-dialog.instance");
104
105 // Title
106 $title = $dlg.find(".dialog-title");
107 var projectName = "",
108 projectRoot = ProjectManager.getProjectRoot(),
109 title;
110 if (projectRoot) {
111 projectName = projectRoot.name;
112 }
113 title = StringUtils.format(Strings.PROJECT_SETTINGS_TITLE, projectName);
114 $title.text(title);
115
116 // Base URL
117 $baseUrlControl = $dlg.find(".base-url");
118 if (baseUrl) {
119 $baseUrlControl.val(baseUrl);
120 }
121
122 // Error message
123 if (errorMessage) {
124 $dlg.find(".settings-list").append("<div class='alert-message' style='margin-bottom: 0'>" + errorMessage + "</div>");
125 }
126
127 // Give focus to first control
128 $baseUrlControl.focus();
129
130 return promise;
131 }
132
133 // For unit testing
134 exports._validateBaseUrl = _validateBaseUrl;
135
136 exports.showProjectPreferencesDialog = showProjectPreferencesDialog;
137});