This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
/
controller.common.js
155 lines (137 loc) · 4.85 KB
/
controller.common.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/**
Controller for Mojio
After an application has been configured to use a mojit, the mojit controller can either do all of the work or
delegate the work to models and/or views. In the typical case, the mojit controller requests the model to
retrieve data and then the controller serves that data to the views.
For more info, visit: http://developer.yahoo.com/cocktails/mojito/docs/intro/mojito_mvc.html#controllers
**/
/*jslint anon:true*/
/*global YUI*/
/**
Displays guide content in a horizontally flickable scrollview.
@class ReadController
**/
YUI.add('Read', function(Y, NAME) {
'use strict';
var LIB_MD;// Markdown library
/**
Is used for library initializations under test environment.
@method init_test
@private
@param {Object} config The configuration object.
**/
function init_test(config) {
// Mocking the library for test purposes.
// Unit tests are run using "mojito test app app_root/".
// Make sure the attributes are set up correctly, otherwise don't set at all
if (config && config.test && config.test.libs) {
LIB_MD = config.test.libs.lib_md;
}
}
/**
Composes the view with links to adjacent guides
return callback({
title: guide title
content: guide content
prev: link to previous guide
next: link to next guide
})
@method compose
@private
@param {Object} guidemeta Guide meta.
@param {Object} result data of guide content.
@param {Object} action context.
@param (Function) callback for the final view data.
**/
function compose(guidemeta, data, ac, callback) {
var vu = {},
model = ac.models.get('guide'),
afterGetAdjacentFiles = function (prevFilename, nextFilename) {
// Checks filenames are not null
if (prevFilename && nextFilename) {
vu.prev = ac.url.make('read', 'index', {
'filename': encodeURIComponent(prevFilename)
});
vu.next = ac.url.make('read', 'index', {
'filename': encodeURIComponent(nextFilename)
});
}
// Passes back view data
callback(vu);
};
// Sets up title and content of view data
vu.title = data.title;
// Render Markdown content into HTML
// Markdown requires node module installing. Please run "npm i" from project folder.
vu.content = LIB_MD ? LIB_MD(data.content) : require("node-markdown").Markdown(data.content);
// Gets filenames of all guides.
model.getAdjacentGuideFilenames(guidemeta.filename, afterGetAdjacentFiles);
}
/**
Something went wrong, RENDERS 'oh no' message.
@method fail
@private
@param {String} error The error message.
@param {ActionContext} ac The action context.
**/
function fail(error, ac) {
ac.done({
// Localize program generated text. See .js files under lang/
title: ac.intl.lang("ERROR_TITLE"),
content: error
});
}
/**
Loads guide's filename from URL, fetch the content from model and display it.
@method index
@param {ActionContext} ac The action context to operate on.
**/
function index(ac) {
var guidemeta = {},
model = ac.models.get('guide'),
error,
afterComposed = function(viewData) {
ac.done(viewData);
},
afterGetGuide = function (error, resultObj) {
// resultObj.title contains guide title
// resultObj.content contains HTML content
// Checks to see if there's an error
if (error) {
fail(error, ac);
} else {
// Normalize content
compose(guidemeta, resultObj, ac, afterComposed);
}
};
// Fills in feed metas
guidemeta = ac.params.merged();
// Asks model for content, or display error.
model.getGuide(guidemeta, afterGetGuide);
}
/**
Register index method under controller.
Keep all other private functions under "test" for unit testing.
**/
Y.namespace('mojito.controllers')[NAME] = {
index: index,
test: {
init_test: init_test,
compose: compose,
fail: fail
}
};
}, '0.0.1', {requires: [
'mojito',
'mojito-config-addon',
'mojito-models-addon',
'mojito-params-addon',
'mojito-url-addon',
'mojito-intl-addon',
'guide-model'
]});