Лично я фанат ExtJS (сейчас Sencha) , и это довольно просто и просто сделать с помощью этой платформы. Вот как я это сделаю:
<body>
<button id="content_define" class="zone_define">Content</button>
<button id="sidebar_define" class="zone_define">Sidebar</button>
<div id="template">
<!-- put your template here -->
</div>
</body>
<script type="text/javascript">
Ext.onReady(function() {
Ext.select('button.zone_define').on('click', function(ev, target) {
ev.stopEvent();
// this listener is bound to two buttons, determine which one was pressed
var zone = target.id.split('_')[0]; // 'content' or 'sidebar'
// add a click listener listener to the template so that when an element within is clicked,
// an Ajax request is initiated
var template = Ext.get('template');
template.addClass('targetable').on('click', function(ev, target) {
template.removeClass('targetable');
// this part is optional, in case the user clicks an <a> or <img> element,
// you may want to get the block element containing the clicked element
var target = ev.getTarget('div'); // or ul, table, p, whatever
// invoke zone_capture on the click target (or containing block element)
zone_capture(zone, target);
}, window, {single: true});
// the {single: true} part removes this listener after one click,
// so the user has to go back and press the button again if they want to redefine
});
// this is the code to do the ajax request
// and also add a class to the target element
function zone_capture(zone, target) {
// first, remove the class from elements which are already defined as this zone
Ext.select(String.format('#template .{0}_zone', zone)).removeClass(zone + '_zone');
// now add that class to the target
target.addClass(zone + '_zone');
// do the POST
Ext.Ajax.request({
method: 'POST',
url: '/region/define',
params: { // these are the params POSTed to your script
template: Ext.get('template').dom.innerHTML
}
});
}
});
</script>
Я бы также добавил следующие правила CSS для эффектов зависания и т. Д.
#template.targetable:hover div { /* add div, p, table, whatever */
border: 1px solid #f00;
}
#template.targetable {
cursor: crosshair;
}
Я бы назвал этот псевдо-код, но этой идеи должно быть достаточно, чтобы вы начали. Поскольку вы имеете дело с пользовательским вводом, у вас будет много угловых случаев, чтобы проверить, прежде чем вы сможете назвать это готовым к работе. Звучит как крутой способ определить шаблон, удачи!