Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/elin3t/django-hstore
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Apr 17, 2014
2 parents 4719f6d + cbe42d9 commit 7ae2720
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion django_hstore/static/admin/css/django-hstore-widget.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.grp-tools li a.hstore-remove-handler{ background-position: 0 -3003px }
.grp-tools li a.hstore-remove-handler:hover{ background-position: 0 -3047px }
.grp-tools li a.hstore-add-handler { background-position: 0 -2563px }
.grp-tools li a.hstore-add-handler:hover { background-position: 0 -2959px }
.grp-tools li a.hstore-add-handler:hover { background-position: 0 -2959px }
56 changes: 28 additions & 28 deletions django_hstore/static/admin/js/django_hstore/hstore-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var initDjangoHStoreWidget = function(hstore_field_name, inline_prefix) {
if(hstore_field_name.indexOf('__prefix__') > -1){
return;
}

$ = django.jQuery;

// reusable function that retrieves a template even if ID is not correct
// (written to support inlines)
var retrieveTemplate = function(template_name, field_name){
Expand All @@ -24,17 +24,17 @@ var initDjangoHStoreWidget = function(hstore_field_name, inline_prefix) {
return html;
}
}

// reusable function that compiles the UI
var compileUI = function(params){
var hstore_field_id = 'id_'+hstore_field_name,
original_textarea = $('#'+hstore_field_id),
original_value = original_textarea.val(),
original_container = original_textarea.parents('.form-row, .grp-row').eq(0),
json_data = {};

if(original_value !== ''){
// manage case in which textarea is blank
// manage case in which textarea is blank
try{
json_data = JSON.parse(original_value);
}
Expand All @@ -43,7 +43,7 @@ var initDjangoHStoreWidget = function(hstore_field_name, inline_prefix) {
return false;
}
}

var hstore_field_data = {
"id": hstore_field_id,
"label": original_container.find('label').text(),
Expand All @@ -55,76 +55,76 @@ var initDjangoHStoreWidget = function(hstore_field_name, inline_prefix) {
// compile template
ui_html = retrieveTemplate('hstore-ui-template', hstore_field_name),
compiled_ui_html = _.template(ui_html, hstore_field_data);

// this is just to DRY up a bit
if(params && params.replace_original === true){
// remove original textarea to avoid having two textareas with same ID
original_textarea.remove();
// inject compiled template and hide original
original_container.after(compiled_ui_html).hide();
}

return compiled_ui_html;
};

// generate UI
compileUI({ replace_original: true });

// cache other objects that we'll reuse
var row_html = retrieveTemplate('hstore-row-template', hstore_field_name),
empty_row = _.template(row_html, { 'key': '', 'value': '' }),
$hstore = $('#id_'+hstore_field_name).parents('.hstore');

// reusable function that updates the textarea value
var updateTextarea = function(container) {
// init empty json object
var new_value = {},
raw_textarea = container.find('textarea'),
rows = container.find('.form-row, .grp-row');

// loop over each object and populate json
rows.each(function() {
var inputs = $(this).find('input'),
key = inputs.eq(0).val(),
value = inputs.eq(1).val();
new_value[key] = value;
});

// update textarea value
$(raw_textarea).val(JSON.stringify(new_value, null, 4));
};

// remove row link
$hstore.delegate('a.remove-row', 'click', function(e) {
e.preventDefault();
// cache container jquery object before $(this) gets removed
$(this).parents('.form-row, .grp-row').eq(0).remove();
updateTextarea($hstore);
});

// add row link
$hstore.delegate('a.add-row, .add-row a', 'click', function(e) {
$hstore.delegate('a.hs-add-row, .hs-add-row a', 'click', function(e) {
e.preventDefault();
$hstore.find('.hstore-rows').append(empty_row);
});

// toggle textarea link
$hstore.delegate('.hstore-toggle-txtarea', 'click', function(e) {
e.preventDefault();

var raw_textarea = $hstore.find('.hstore-textarea'),
hstore_rows = $hstore.find('.hstore-rows'),
add_row = $hstore.find('.add-row');
add_row = $hstore.find('.hs-add-row');

if(raw_textarea.is(':visible')) {

var compiled_ui = compileUI();

// in case of JSON error
if(compiled_ui === false){
return;
}

// jquery < 1.8
try{
var $ui = $(compiled_ui);
Expand All @@ -133,10 +133,10 @@ var initDjangoHStoreWidget = function(hstore_field_name, inline_prefix) {
catch(e){
var $ui = $($.parseHTML(compiled_ui));
}

// update rows with only relevant content
hstore_rows.html($ui.find('.hstore-rows').html());

raw_textarea.hide();
hstore_rows.show();
add_row.show();
Expand All @@ -147,7 +147,7 @@ var initDjangoHStoreWidget = function(hstore_field_name, inline_prefix) {
add_row.hide();
}
});

// update textarea whenever a field changes
$hstore.delegate('input[type=text]', 'keyup', function() {
updateTextarea($hstore);
Expand All @@ -158,7 +158,7 @@ django.jQuery(window).load(function() {
// support inlines
// bind only once
if(window.hstoreWidgetBoundInlines === undefined){
$('.grp-group .grp-add-handler, .inline-group .add-row a').click(function(e){
$('.grp-group .grp-add-handler, .inline-group .hs-add-row a').click(function(e){
var hstore_original_textareas = $(this).parents('.grp-group, .inline-group').eq(0).find('.hstore-original-textarea');
// if module contains .hstore-original-textarea
if(hstore_original_textareas.length > 0){
Expand All @@ -182,4 +182,4 @@ django.jQuery(window).load(function() {
});
window.hstoreWidgetBoundInlines = true;
}
});
});
14 changes: 7 additions & 7 deletions django_hstore/templates/hstore_default_widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@
<script type="text/html" id="hstore-ui-template-{{ field_name }}"{% if '__prefix__' in field_name %} class="hstore-ui-template-inline"{% endif %}>
<div class="hstore" id="hstore-{{ field_name }}">
<h2><%= label %></h2>

<% if(help && help != '') { %>
<div class="form-row">
<p class="help" style="margin:0;padding:0"><%= help %></p>
</div>
<% } %>

<div class="hstore-rows">
<% for(key in data){ %>
<%= _.template($('.hstore-row-template-inline').eq(0).html(), { 'key': key, 'value': data[key] }) %>
<% } %>
</div>

<div class="form-row field-data hstore-textarea" style="display:none">
<div>
<label for="<%= id %>" class="required">{% trans 'Raw textarea' %}:</label>
<textarea class="vLargeTextField" cols="40" id="<%= id %>" name="<%= name %>" rows="10"><%= value %></textarea>
</div>
</div>

<div class="form-row">
<a href="#" class="add-row" title="{% trans 'Add another row' %}">
<a href="#" class="hs-add-row" title="{% trans 'Add another row' %}">
<img src="{{ STATIC_URL }}admin/img/icon_addlink.gif" width="10" height="10" alt="{% trans 'Add Another' %}">
{% trans "Add row" %}
</a>

<a href="#" class="hstore-toggle-txtarea" title="{% trans 'toggle textarea' %}" style="float:right">
<img src="{{ STATIC_URL }}admin/img/icon_changelink.gif" width="10" height="10" alt="{% trans 'toggle textarea' %}">
{% trans 'toggle textarea' %}
Expand All @@ -53,4 +53,4 @@ <h2><%= label %></h2>
</div>
</script>

<script>django.jQuery(function() { initDjangoHStoreWidget('{{ field_name }}') });</script>
<script>django.jQuery(function() { initDjangoHStoreWidget('{{ field_name }}') });</script>
18 changes: 9 additions & 9 deletions django_hstore/templates/hstore_grappelli_widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
value="<%= _.escape(value) %>"
type="text"
placeholder="{% trans 'value' %}">

<ul class="grp-tools" style="float:left;position:static;margin:0 0 0 15px">
<li><a href="#" class="icons-sprite hstore-remove-handler remove-row" title="{% trans 'remove row' %}"></a></li>
</ul>
Expand All @@ -20,16 +20,16 @@
<script type="text/html" id="hstore-ui-template-{{ field_name }}"{% if '__prefix__' in field_name %} class="hstore-ui-template-inline"{% endif %}>
<div class="grp-module hstore" id="hstore-{{ field_name }}">
<h2><%= label %></h2>

<ul class="grp-tools">
<li><a href="#" class="grp-text hstore-toggle-txtarea">{% trans 'toggle textarea' %}</a></li>
<li class="add-row"><a href="#" class="icons-sprite hstore-add-handler"></a></li>
<li class="hs-add-row"><a href="#" class="icons-sprite hstore-add-handler"></a></li>
</ul>

<% if(help && help != '') { %>
<div class="grp-row"><p class="grp-description"><%= help %></p></div>
<% } %>

<div class="l-2c-fluid l-d-4 grp-row grp-cells hstore-textarea" style="display:none">
<div class="c-1 grp-cell">
<label for="<%= id %>">{% trans 'Raw textarea' %}</label>
Expand All @@ -38,17 +38,17 @@ <h2><%= label %></h2>
<textarea class="vLargeTextField" cols="40" id="<%= id %>" name="<%= name %>" rows="10"><%= value %></textarea>
</div>
</div>

<div class="hstore-rows">
<% for(key in data){ %>
<%= _.template($('.hstore-row-template-inline').eq(0).html(), { 'key': key, 'value': data[key] }) %>
<% } %>
</div>
<div class="grp-row add-row">

<div class="grp-row hs-add-row">
<a href="#" class="grp-button grp-add-link">+ {% trans "Add row" %}</a>
</div>
</div>
</script>

<script>django.jQuery(function() { initDjangoHStoreWidget('{{ field_name }}') });</script>
<script>django.jQuery(function() { initDjangoHStoreWidget('{{ field_name }}') });</script>

0 comments on commit 7ae2720

Please sign in to comment.