-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from AdFabConnect/structure_editor
Structure editor
- Loading branch information
Showing
22 changed files
with
577 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as structure from './structure' | ||
|
||
export { | ||
structure | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import path from 'path' | ||
|
||
import { | ||
coreUtils, | ||
cmsStructure, | ||
cmsData, | ||
config | ||
} from '../../' | ||
|
||
export function editStructure(type, folderPath) { | ||
if(type === 'add') coreUtils.file.addFolder(folderPath) | ||
else coreUtils.file.removeFolder(folderPath) | ||
return folderPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/*global document */ | ||
|
||
import Nanoajax from 'nanoajax' | ||
import qs from 'qs' | ||
|
||
export default class EditorStructures { | ||
constructor() { | ||
var struc = document.querySelector('.structure-json') | ||
if(!(struc != null)) return; | ||
this._ajax = Nanoajax.ajax | ||
this.datas = JSON.parse(struc.value) | ||
this.structureWrapper = document.querySelector('.structure-wrapper') | ||
this.folderName = document.querySelector('input.folder-name') | ||
|
||
var lvl_0 = this.createFolder('structure/', 0, '', 'structure', '') | ||
|
||
this.createStructure(lvl_0, this.datas) | ||
this.structureWrapper.appendChild(lvl_0) | ||
this.rebind() | ||
} | ||
|
||
createFolder(path, level, daddy, folderName, hidden = 'hidden') { | ||
var folder = document.createElement('div') | ||
if(hidden && hidden !== '') folder.classList.add(hidden) | ||
folder.classList.add('structure-folder') | ||
folder.setAttribute('data-path', path.replace(/\/+$/, '') + '/') | ||
folder.setAttribute('data-level', level) | ||
folder.setAttribute('data-daddy', daddy) | ||
|
||
var span = document.createElement('span') | ||
var html = `<span class="glyphicon glyphicon-chevron-right arrow" aria-hidden="true"></span> | ||
${folderName} | ||
<div class="structure-tool"> | ||
<span class="glyphicon glyphicon-plus folder-action" data-init="0" data-action="add" aria-hidden="true"></span>` | ||
if(level !== 0) html += '<span class="glyphicon glyphicon-minus folder-action" data-init="0" data-action="remove" aria-hidden="true"></span>' | ||
html += '</div>' | ||
span.innerHTML = html | ||
folder.appendChild(span) | ||
|
||
this.bindArrow(span.querySelector('.arrow')) | ||
|
||
return folder | ||
} | ||
|
||
toggleFolder(daddy, forceState = ''){ | ||
var folders = daddy.querySelectorAll('[data-level="' + (parseInt(daddy.getAttribute('data-level')) + 1) + '"]') | ||
if(folders) { | ||
if(!daddy.classList.contains('open') || forceState === 'open'){ | ||
daddy.classList.add('open') | ||
Array.prototype.forEach.call(folders, (folder) => { | ||
folder.classList.remove('hidden') | ||
}) | ||
} | ||
else{ | ||
daddy.classList.remove('open') | ||
Array.prototype.forEach.call(folders, (folder) => { | ||
folder.classList.add('hidden') | ||
}) | ||
} | ||
} | ||
} | ||
|
||
bindArrow(arrow) { | ||
arrow.addEventListener('click', () => { | ||
this.toggleFolder(arrow.parentNode.parentNode) | ||
}) | ||
} | ||
|
||
createStructure(daddy, datas) { | ||
Array.prototype.forEach.call(datas, (data) => { | ||
var folderName = data['path'].split('/') | ||
var folder = this.createFolder( | ||
data['path'], | ||
(parseInt(daddy.getAttribute('data-level')) + 1), | ||
daddy.getAttribute('data-daddy'), | ||
folderName[folderName.length - 1] | ||
) | ||
daddy.appendChild(folder) | ||
|
||
if(data.folders && data.folders.length > 0) this.createStructure(folder, data.folders) | ||
}) | ||
} | ||
|
||
add(element){ | ||
this.toggleFolder(element, 'open') | ||
this.folderName.removeAttribute('disabled') | ||
this.folderName.focus() | ||
this.folderName.setAttribute('data-folder', `[data-path="${element.getAttribute('data-path')}"]`) | ||
|
||
var writeFolderName = (e) => { | ||
var value = this.folderName.value | ||
if(/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/.test(value)) this.folderName.classList.remove('error') | ||
else this.folderName.classList.add('error') | ||
if(e.keyCode === 13) { | ||
if(/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/.test(value)){ | ||
this.folderName.removeEventListener('keyup', writeFolderName) | ||
this.folderName.setAttribute('disabled', 1) | ||
var newFolderDaddy = document.querySelector(this.folderName.getAttribute('data-folder')) | ||
var path = newFolderDaddy.getAttribute('data-path').split('/') | ||
path.pop() | ||
path = path.concat(value).join('/') | ||
var folder = this.createFolder( | ||
newFolderDaddy.getAttribute('data-path') + value, | ||
(parseInt(newFolderDaddy.getAttribute('data-level')) + 1), | ||
newFolderDaddy.getAttribute('data-daddy'), | ||
value, | ||
'' | ||
) | ||
newFolderDaddy.appendChild(folder) | ||
this.rebind() | ||
this.save(qs.stringify({type: 'add', folderPath: (newFolderDaddy.getAttribute('data-path') + value)})) | ||
this.folderName.value = '' | ||
} | ||
} | ||
} | ||
|
||
this.folderName.removeEventListener('keyup', writeFolderName) | ||
this.folderName.addEventListener('keyup', writeFolderName) | ||
} | ||
|
||
remove(element){ | ||
this.save(qs.stringify({type: 'remove', folderPath: element.getAttribute('data-path')})) | ||
element.parentNode.removeChild(element) | ||
} | ||
|
||
save(body){ | ||
this._ajax({url: '/abe/structure/', body: body, cors: true, method: 'post'}, () => { | ||
|
||
}) | ||
} | ||
|
||
rebind(){ | ||
var folderActions = document.querySelectorAll('.folder-action') | ||
Array.prototype.forEach.call(folderActions, (folderAction) => { | ||
if(parseInt(folderAction.getAttribute('data-init')) === 0){ | ||
folderAction.setAttribute('data-init', 1) | ||
folderAction.addEventListener('click', (e) => { | ||
var target = e.target | ||
this[target.getAttribute('data-action')](target.parentNode.parentNode.parentNode) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,29 @@ | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
|
||
import { | ||
Manager | ||
Manager, | ||
coreUtils, | ||
config, | ||
Handlebars, | ||
User | ||
} from '../../cli' | ||
|
||
var route = function(req, res){ | ||
res.set('Content-Type', 'application/json') | ||
res.send(JSON.stringify({reference: Manager.instance.getReferences()})) | ||
var resHtml = '' | ||
|
||
var page = path.join(__dirname + '/../views/list-references.html') | ||
if (coreUtils.file.exist(page)) { | ||
resHtml = fs.readFileSync(page, 'utf8') | ||
} | ||
|
||
var template = Handlebars.compile(resHtml, {noEscape: true}) | ||
var tmp = template({ | ||
config: JSON.stringify(config), | ||
reference: Manager.instance.getReferences() | ||
}) | ||
|
||
return res.send(tmp) | ||
} | ||
|
||
export default route |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
|
||
import { | ||
Manager, | ||
coreUtils, | ||
config, | ||
Handlebars, | ||
User | ||
} from '../../cli' | ||
|
||
var route = function(req, res){ | ||
var resHtml = '' | ||
|
||
var page = path.join(__dirname + '/../views/list-structure.html') | ||
if (coreUtils.file.exist(page)) { | ||
resHtml = fs.readFileSync(page, 'utf8') | ||
} | ||
|
||
var structure = Manager.instance.getStructureAndTemplates().structure | ||
structure = JSON.stringify(structure).replace(new RegExp(config.root, 'g'), '') | ||
var template = Handlebars.compile(resHtml, {noEscape: true}) | ||
var tmp = template({ | ||
config: JSON.stringify(config), | ||
structure: structure | ||
}) | ||
return res.send(tmp) | ||
} | ||
|
||
export default route |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { | ||
cmsStructure | ||
} from '../../cli' | ||
|
||
var route = function(req, res){ | ||
if(typeof res._header !== 'undefined' && res._header !== null) return | ||
cmsStructure.structure.editStructure(req.body.type, req.body.folderPath) | ||
res.set('Content-Type', 'application/json') | ||
res.send(JSON.stringify({success: 1})) | ||
} | ||
|
||
export default route |
Oops, something went wrong.