Skip to content

Commit 452858f

Browse files
committed
Combine view and edit libs into one, refactor
1 parent 4976aca commit 452858f

12 files changed

+145
-41
lines changed

.env.example

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
#
2+
# API Configuration
3+
#
4+
API_BASE=http://admin.cbcortex.com/api/v1
5+
16
#
27
# Asset Configuration
38
#
49
CONTENT_SNIPPETS_EDIT_CSS_URL=https://s3.amazonaws.com/cortex-content-snippets/content-snippets-edit.css
5-
CKEDITOR_PLUGINS_URL=http://stg.admin.cbcortex.com/ckeditor-plugins/
10+
CKEDITOR_PLUGINS_URL=http://admin.cbcortex.com/ckeditor-plugins/

bower.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
{
22
"name": "content-snippets-edit",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"ignore": [
55
"**/.*",
66
"node_modules",
77
"components"
88
],
99
"dependencies": {
10+
"riot": "~2.3.12",
11+
"q": "~1.3.0",
12+
"qajax": "[email protected]:cb-talent-development/qajax#master",
13+
"underscore": "~1.8.3",
1014
"materialize": "~0.97",
1115
"normalize-scss": "~3.0"
16+
},
17+
"resolutions": {
18+
"q": "~1.3.0"
1219
}
1320
}

gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var applicationName = 'content-snippets-edit',
2020
appFiles = {
2121
styles: paths.styles.src + 'main.scss',
2222
scripts: paths.scripts.src + '**/*.js',
23-
riot: '**/tags/**'
23+
riot: basePaths.src + 'tags/**'
2424
},
2525
vendorFiles = {
2626
styles: 'src/vendor/**/*.css',

package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "content-snippets-edit",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "A tool to enable in-page editing of elements with Cortex as a backend",
55
"author": "CB Content Enablement Product Team <[email protected]>",
66
"license": "Apache-2.0",
@@ -14,19 +14,19 @@
1414
],
1515
"devDependencies": {
1616
"gulp": "~3.9.0",
17-
"gulp-load-plugins": "~1.0.0",
17+
"gulp-load-plugins": "~1.1.0",
1818
"gulp-autoprefixer": "~3.1.0",
1919
"gulp-concat": "~2.6.0",
20-
"gulp-ruby-sass": "~2.0.5",
21-
"gulp-riot": "~0.3.1",
20+
"gulp-ruby-sass": "~2.0.6",
21+
"gulp-riot": "~0.4.2",
2222
"gulp-size": "~2.0.0",
2323
"gulp-util": "~3.0.7",
24-
"gulp-uglify": "~1.4.2",
24+
"gulp-uglify": "~1.5.1",
2525
"gulp-combine-mq": "~0.4.0",
26-
"gulp-minify-css": "~1.2.1",
26+
"gulp-minify-css": "~1.2.2",
2727
"gulp-rename": "~1.2.2",
2828
"gulp-ignore": "~2.0.1",
29-
"main-bower-files": "~2.9.0",
29+
"main-bower-files": "~2.11.0",
3030
"streamqueue": "~1.1.1",
3131
"dotenv": "~1.2.0"
3232
},

src/app/config.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
(function(global) {
22
'use strict';
33

4-
_.extend(global.config, {
5-
CONTENT_SNIPPETS_EDIT_CSS_URL: process.env.CONTENT_SNIPPETS_EDIT_CSS_URL,
6-
CKEDITOR_PLUGINS_URL: process.env.CKEDITOR_PLUGINS_URL
7-
});
4+
/**
5+
* Temporarily not using Dotenv because it ain't workin!
6+
*
7+
* global.config = {
8+
* API_BASE: process.env.API_BASE,
9+
* API_ROUTES: {
10+
* WEBPAGES: '/webpages',
11+
* WEBPAGES_FEED: '/webpages/feed'
12+
* },
13+
* CONTENT_SNIPPETS_EDIT_CSS_URL: process.env.CONTENT_SNIPPETS_EDIT_CSS_URL,
14+
* CKEDITOR_PLUGINS_URL: process.env.CKEDITOR_PLUGINS_URL
15+
* };
16+
*/
17+
18+
global.config = {
19+
API_BASE: 'http://localhost:3000/api/v1',
20+
API_ROUTES: {
21+
WEBPAGES: '/webpages',
22+
WEBPAGES_FEED: '/webpages/feed'
23+
},
24+
CONTENT_SNIPPETS_EDIT_CSS_URL: 'https://s3.amazonaws.com/cortex-content-snippets/content-snippets-edit.css',
25+
CKEDITOR_PLUGINS_URL: 'http://admin.cbcortex.com/ckeditor-plugins/'
26+
};
827
}(this));

src/app/constants.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
(function(global) {
22
'use strict';
33

4-
_.extend(global.constants.ACTIONS, {
5-
MEDIAS_CHANGED: 'medias_changed',
6-
MEDIAS_NEXT: 'medias_next',
7-
MEDIAS_PREVIOUS: 'medias_previous',
8-
MEDIAS_SEARCH: 'medias_search',
9-
INSERT_MEDIA: 'insert_media'
10-
});
11-
12-
_.extend(global.constants.FRAME_ACTIONS, {
13-
GET_MEDIAS: 'get_medias',
14-
MEDIAS_DATA: 'medias_data'
15-
});
4+
global.constants = {
5+
ACTIONS: {
6+
WEBPAGE_CHANGED: 'webpage_changed',
7+
SNIPPET_CHANGED: 'snippet_changed',
8+
SAVE_WEBPAGE: 'save_webpage',
9+
MEDIAS_CHANGED: 'medias_changed',
10+
MEDIAS_NEXT: 'medias_next',
11+
MEDIAS_PREVIOUS: 'medias_previous',
12+
MEDIAS_SEARCH: 'medias_search',
13+
INSERT_MEDIA: 'insert_media'
14+
},
15+
FRAME_ACTIONS: {
16+
LOAD_EDITOR: 'load_editor',
17+
GET_MEDIAS: 'get_medias',
18+
MEDIAS_DATA: 'medias_data',
19+
CANCEL_EDITOR: 'cancel_editor',
20+
SAVE_WEBPAGE: 'save_webpage'
21+
}
22+
};
1623
}(this));

src/app/main.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'http://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100') );
77
$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'https://fonts.googleapis.com/icon?family=Material+Icons') );
88
$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', config.CONTENT_SNIPPETS_EDIT_CSS_URL) );
9-
$.getScript('https://cdn.ckeditor.com/4.5.4/standard/ckeditor.js', function() {
9+
$.getScript('https://cdn.ckeditor.com/4.5.6/standard/ckeditor.js', function() {
1010
global.CKEDITOR.config.autoParagraph = false;
1111
global.CKEDITOR.config.allowedContent = true;
1212
global.CKEDITOR.plugins.addExternal( 'media-selector', global.config.CKEDITOR_PLUGINS_URL + 'media-selector/', 'plugin.js' );
@@ -16,14 +16,15 @@
1616
global.CKEDITOR.config.removePlugins = 'image,sourcearea';
1717

1818
var stores = {
19+
WebpageStore: new WebpageStore(),
1920
MediasStore: new MediasStore()
2021
};
2122

2223
for (var k in stores) {
2324
RiotControl.addStore(stores[k]);
2425
}
2526

26-
_.extend(global.stores, stores);
27+
global.stores = stores;
2728

2829
riot.mount('snippet');
2930
riot.mount('media-library');

src/app/stores/WebpageStore.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
(function(global) {
2+
'use strict';
3+
4+
function WebpageStore(webpage) {
5+
riot.observable(this);
6+
7+
this.webpage = webpage || [];
8+
this._api = new Api(config.API_BASE);
9+
10+
this._fetch();
11+
this.on(constants.ACTIONS.SNIPPET_CHANGED, this._update_snippet);
12+
this.on(constants.ACTIONS.SAVE_WEBPAGE, this._save);
13+
}
14+
15+
WebpageStore.prototype._changed = function() {
16+
this.trigger(constants.ACTIONS.WEBPAGE_CHANGED, this.webpage);
17+
};
18+
19+
WebpageStore.prototype._fetch = function() {
20+
return new Promise(function(resolve, reject) {
21+
var url = window.location.href;
22+
this._api
23+
.get(config.API_ROUTES.WEBPAGES_FEED + '?url=' + encodeURIComponent(url))
24+
.then(function(webpage) {
25+
this.webpage = webpage;
26+
resolve(webpage);
27+
this._changed();
28+
}.bind(this), reject);
29+
}.bind(this));
30+
};
31+
32+
WebpageStore.prototype._save = function() {
33+
parent.postMessage({event: constants.FRAME_ACTIONS.SAVE_WEBPAGE, webpage: this.webpage}, '*');
34+
};
35+
36+
WebpageStore.prototype._update_snippet = function(snippet) {
37+
var that = this,
38+
foundSnippetIndex = _.findIndex(this.webpage.snippets, function(obj) {
39+
return obj.document.name === snippet.name;
40+
});
41+
42+
if (foundSnippetIndex >= 0) {
43+
this.webpage.snippets[foundSnippetIndex].document.body = snippet.body;
44+
}
45+
else {
46+
that.webpage.snippets.push({
47+
document: {
48+
name: snippet.name,
49+
body: snippet.body
50+
}
51+
});
52+
}
53+
};
54+
55+
global.WebpageStore = WebpageStore;
56+
}(this));

src/app/tags/media-library.tag

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@
6868
window.media_select_defer.resolve(e.item);
6969
}
7070

71-
mediasNext() {
71+
mediasNext(e) {
7272
RiotControl.trigger(constants.ACTIONS.MEDIAS_NEXT);
7373
}
7474

75-
mediasPrevious() {
75+
mediasPrevious(e) {
7676
RiotControl.trigger(constants.ACTIONS.MEDIAS_PREVIOUS);
7777
}
7878

79-
mediasSearch() {
79+
mediasSearch(e) {
8080
RiotControl.trigger(constants.ACTIONS.MEDIAS_SEARCH, self.search.value);
8181
}
8282
</script>

src/app/tags/snippet.tag

+12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
<snippet>
22
<div class="editor" onblur="{ contentChanged }" contenteditable="true"><yield /></div>
3+
34
<script>
45
fluxableTag(this, RiotControl);
56
var self = this;
67

8+
RiotControl.on(constants.ACTIONS.WEBPAGE_CHANGED, function(webpage) {
9+
var foundSnippet = _.find(webpage.snippets, function(obj) {
10+
return obj.document.name == opts.id;
11+
});
12+
if (typeof foundSnippet != "undefined") {
13+
self.getElementsByClassName('editor')[0].innerHTML = foundSnippet.document.body;
14+
self.update();
15+
}
16+
});
17+
718
this.contentChanged = function(event) {
819
RiotControl.trigger(constants.ACTIONS.SNIPPET_CHANGED, {name: opts.id, body: event.target.innerHTML});
920
}
1021
</script>
22+
1123
<style scoped>
1224
.editor {
1325
box-shadow: 0px 0px 2px #ccc;

src/app/tags/toolbar.tag

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77

88
<script>
99
fluxableTag(this, RiotControl);
10-
this.saveWebpage = function() {
10+
11+
this.saveWebpage = function(e) {
1112
RiotControl.trigger(constants.ACTIONS.SAVE_WEBPAGE);
1213
};
13-
this.cancel = function() {
14+
15+
this.cancel = function(e) {
1416
parent.postMessage({event: constants.FRAME_ACTIONS.CANCEL_EDITOR}, '*');
1517
};
16-
this.preview = function() {
18+
19+
this.preview = function(e) {
1720
$('.editor').removeClass('editor');
18-
}
21+
};
1922
</script>
2023

2124
<style scoped>

src/vendor/api.js

-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
(function(global) {
22
'use strict';
33

4-
var csrf = document.querySelector("meta[name='csrf-token']").attributes['content'].value;
5-
64
function Api(baseUrl) {
75
this.baseUrl = baseUrl || window.location.origin;
86
}
97

108
Api.prototype._request = function(config) {
119
config.headers = config.headers || {};
1210

13-
if (csrf) {
14-
config.headers['X-CSRF-Token'] = csrf;
15-
}
16-
1711
config.headers['Accept'] = 'application/json,text/html';
1812

1913
return Qajax(config)

0 commit comments

Comments
 (0)