瀏覽代碼

Gestion des imports dans les profils

upgrade-electron
wpetit 4 年之前
父節點
當前提交
996ccd90f1

+ 11
- 18
default-profile.json 查看文件

@@ -1,6 +1,9 @@
1 1
 {
2 2
   "items": [
3 3
     {
4
+      "import": "./partial-profile.json"
5
+    },
6
+    {
4 7
       "label": "Level 1",
5 8
       "icon": "chromium-browser",
6 9
       "background": "./img/background2.jpg",
@@ -12,15 +15,13 @@
12 15
             {
13 16
               "label": "Chromium Browser 1",
14 17
               "icon": "chromium-browser",
15
-              "exec": "/usr/bin/chromium-browser",
16
-              "_key": "item_1444480285022_3"
18
+              "exec": "/usr/bin/chromium-browser"
17 19
             },
18 20
             {
19 21
               "label": "Pitaya Edit",
20 22
               "exec": "PITAYA_MODE=edit PITAYA_AS_DESKTOP=0 npm start"
21 23
             }
22
-          ],
23
-          "_key": "item_1444480285022_2"
24
+          ]
24 25
         },
25 26
         {
26 27
           "label": "Level 2-3",
@@ -33,32 +34,24 @@
33 34
                 {
34 35
                   "label": "Chromium Browser 2",
35 36
                   "icon": "chromium-browser",
36
-                  "exec": "/usr/bin/chromium-browser",
37
-                  "_key": "item_1444480285022_6"
37
+                  "exec": "/usr/bin/chromium-browser"
38 38
                 },
39 39
                 {
40 40
                   "label": "Atom",
41 41
                   "icon": "atom",
42 42
                   "exec": "/usr/share/atom/atom %U",
43
-                  "_key": "item_1444480288996_7",
44 43
                   "items": []
45 44
                 },
46 45
                 {
47 46
                   "label": "Firefox Developer Edition Web Browser",
48 47
                   "icon": "firefox",
49
-                  "exec": "firefox %u",
50
-                  "_key": "item_1444761351301_2",
51
-                  "selected": true
48
+                  "exec": "firefox %u"
52 49
                 }
53
-              ],
54
-              "_key": "item_1444480285022_5"
50
+              ]
55 51
             }
56
-          ],
57
-          "_key": "item_1444480285022_4"
52
+          ]
58 53
         }
59
-      ],
60
-      "_key": "item_1444480285022_1"
54
+      ]
61 55
     }
62
-  ],
63
-  "_key": "item_1444480285021_0"
56
+  ]
64 57
 }

+ 5
- 0
partial-profile.json 查看文件

@@ -0,0 +1,5 @@
1
+{
2
+  "label": "Partial Level 1",
3
+  "icon": "chromium-browser",
4
+  "background": "./img/background2.jpg"
5
+}

+ 10
- 2
src/components/edit/item-form.js 查看文件

@@ -7,7 +7,9 @@ var ItemForm = React.createClass({
7 7
     return {
8 8
       label: '',
9 9
       icon: '',
10
-      exec: ''
10
+      exec: '',
11
+      import: '',
12
+      background: ''
11 13
     };
12 14
   },
13 15
 
@@ -19,7 +21,8 @@ var ItemForm = React.createClass({
19 21
         label: props.item.label,
20 22
         icon: props.item.icon,
21 23
         exec: props.item.exec,
22
-        background: props.item.background
24
+        background: props.item.background,
25
+        import: props.item.import
23 26
       });
24 27
     }
25 28
 
@@ -54,6 +57,11 @@ var ItemForm = React.createClass({
54 57
               placeholder="Fond d'écran" value={state.background}
55 58
               onChange={this.handleChange.bind(this, 'background')} />
56 59
           </div>
60
+          <div className="form-group">
61
+            <input type="text" className="form-control"
62
+              placeholder="Import" value={state.import}
63
+              onChange={this.handleChange.bind(this, 'import')} />
64
+          </div>
57 65
         </form>
58 66
       </div>
59 67
     );

+ 1
- 1
src/components/edit/profile-menu.js 查看文件

@@ -21,7 +21,7 @@ var ProfileMenu = React.createClass({
21 21
       var dispatch = this.props.dispatch;
22 22
       this.showOpenProfileDialog()
23 23
         .then(function(profilePath) {
24
-          if(profilePath) dispatch(actions.common.loadProfile(profilePath));
24
+          if(profilePath) dispatch(actions.common.loadProfile(profilePath, false));
25 25
         })
26 26
       ;
27 27
     },

+ 2
- 2
src/store/actions/common.js 查看文件

@@ -4,7 +4,7 @@ var LOAD_PROFILE = exports.LOAD_PROFILE = 'LOAD_PROFILE';
4 4
 var LOAD_PROFILE_SUCCESS = exports.LOAD_PROFILE_SUCCESS = 'LOAD_PROFILE_SUCCESS';
5 5
 var LOAD_PROFILE_FAILED = exports.LOAD_PROFILE_FAILED = 'LOAD_PROFILE_FAILED';
6 6
 
7
-exports.loadProfile = function(profilePath) {
7
+exports.loadProfile = function(profilePath, withImports) {
8 8
 
9 9
   return function(dispatch, getState) {
10 10
 
@@ -12,7 +12,7 @@ exports.loadProfile = function(profilePath) {
12 12
 
13 13
     Util.Logger.info('Loading profile "%s"', profilePath);
14 14
 
15
-    return Util.System.loadJSON(profilePath)
15
+    return Util.Profile.load(profilePath, withImports)
16 16
       .then(function(profile) {
17 17
         Util.Logger.info('Profile loaded.');
18 18
         dispatch({ type: LOAD_PROFILE_SUCCESS, profile: profile });

+ 1
- 0
src/util/index.js 查看文件

@@ -6,3 +6,4 @@ exports.Tree = require('./tree');
6 6
 exports.Const = require('./const');
7 7
 exports.Promises = require('./promises');
8 8
 exports.App = require('./app');
9
+exports.Profile = require('./profile');

+ 52
- 0
src/util/profile.js 查看文件

@@ -0,0 +1,52 @@
1
+var RecursiveIterator = require('recursive-iterator');
2
+var System = require('./system');
3
+var Logger = require('./logger');
4
+var _ = require('lodash');
5
+var path = require('path');
6
+
7
+// Load a profile file with imports
8
+exports.load = function(profileUrl, withImports) {
9
+
10
+  withImports = withImports || true;
11
+
12
+  // Load root profile
13
+  return System.loadJSON(profileUrl)
14
+    .then(function(profile) {
15
+
16
+      if(!withImports) {
17
+        return profile;
18
+      }
19
+
20
+      var promises = [];
21
+
22
+      // Search for imports
23
+      var iterator = new RecursiveIterator(profile);
24
+
25
+      for(var node, item = iterator.next(); !item.done; item = iterator.next()) {
26
+        node = item.value.node;
27
+        // For each import found, load the partial and "mount" it on the current item
28
+        if(node.import) {
29
+          Logger.info('Loading import "%s"', path.resolve(node.import));
30
+          p = exports.load(node.import)
31
+            .then(_mountImport.bind(node))
32
+          ;
33
+          promises.push(p);
34
+        }
35
+      }
36
+
37
+      // When all imports are loaded, return the complete profile
38
+      return Promise.all(promises)
39
+        .then(function() {
40
+          return profile;
41
+        })
42
+      ;
43
+
44
+    })
45
+  ;
46
+
47
+  // Internal method, extend "this" (the mount point) with the partial profile
48
+  function _mountImport(importProfile) {
49
+    _.extend(this, importProfile);
50
+  }
51
+
52
+};

+ 19
- 0
test/profile.js 查看文件

@@ -0,0 +1,19 @@
1
+var Profile = require('../src/util/profile');
2
+
3
+var ProfileSuite = module.exports = {};
4
+
5
+ProfileSuite.loadProfileWithImport = function(test) {
6
+
7
+  Profile.load(__dirname+'/../default-profile.json')
8
+    .then(function(profile) {
9
+      console.log('%j', profile);
10
+      test.ok(profile.items[0].label === "Partial Level 1", "It should have loaded the partial import !");
11
+      test.done();
12
+    })
13
+    .catch(function(err) {
14
+      test.ifError(err);
15
+      test.done();
16
+    })
17
+  ;
18
+
19
+};

Loading…
取消
儲存