2016-08-17 11:18:57 +03:00
Description
============================================================
A header of an item (a dark bar with the title and edit buttons). It consists of a title (item-info-title), controls (item-info-controls), and a form for editing item info (item-info-edit-form).
Required context:
* item
* category
* item_color.light
item_color.dark
* link_to_item = e.g. /haskell/lenses-pt3tvnwt#item-e4t2tv2n
* possible_kinds = e.g.
[ {"name": "library", "caption": "Library", "selected": false}
, {"name": "tool", ...
... ]
* category_groups = e.g.
[ {"name": "POSIX", "selected": false}
, {"name": "PCRE", "selected": true}
... ]
* item_no_group = true if the item's group is Nothing
HTML
============================================================
<div class="item-info" style="background-color:{{item_color.dark}}">
<div class="section normal shown noscript-shown">
2016-11-26 22:23:01 +03:00
{{> item-info-anchor }}
2016-08-17 11:18:57 +03:00
{{> item-info-title }}
{{> item-info-controls }}
</div>
<div class="section editing">
{{> item-info-edit-form }}
</div>
</div>
2016-08-18 03:49:56 +03:00
CSS
------------------------------------------------------------
.item-info {
2016-11-26 22:23:01 +03:00
padding-bottom: 12px;
2016-08-18 03:49:56 +03:00
padding: 10px 15px;
}
2016-11-26 22:23:01 +03:00
.item-info .section.normal {
display: flex;
}
2016-08-18 03:49:56 +03:00
2016-11-26 22:23:01 +03:00
HTML: item-info-anchor
------------------------------------------------------------
<div style="font-size:23px; line-height:27px;">
<a class="anchor" href="{{link_to_item}}">#</a>
</div>
2016-08-17 11:18:57 +03:00
HTML: item-info-title
------------------------------------------------------------
2016-11-26 22:23:01 +03:00
<div style="font-size:23px; line-height:27px;">
2016-08-17 11:18:57 +03:00
{{> item-title}}
2016-11-26 22:23:01 +03:00
</div>
<div class="item-group" style="line-height:27px;">
2016-09-03 21:49:55 +03:00
{{#item.group_}}{{.}}{{/item.group_}}{{^item.group_}}other{{/item.group_}}
2016-11-26 22:23:01 +03:00
</div>
2016-08-17 11:18:57 +03:00
2016-11-26 22:23:01 +03:00
CSS
------------------------------------------------------------
.item-group {
padding-left: 2em;
}
2016-08-17 11:18:57 +03:00
HTML: item-info-controls
------------------------------------------------------------
2016-11-26 22:23:01 +03:00
<div class="controls">
<span>
{{> img-button
src = "/arrow-thick-top.svg"
title = "move item up"
class = "move-item-up"
action = [| moveItem("up", {{{%js item.uid}}}); |] }}
{{> img-button
src = "/arrow-thick-bottom.svg"
title = "move item down"
class = "move-item-down"
action = [| moveItem("down", {{{%js item.uid}}}); |] }}
</span>
<span>
{{> img-button
2017-04-20 11:07:59 +03:00
src = "/cog.svg"
2016-11-26 22:23:01 +03:00
title = "edit item info"
class = "edit-item-info"
action = [| editItemInfo({{{%js item.uid}}}); |] }}
{{> space em=0.4 }}
{{> img-button
src = "/x.svg"
title = "delete item"
class = "delete-item"
action = [| deleteItem({{{%js item.uid}}}); |] }}
</span>
</div>
2016-08-17 11:18:57 +03:00
2016-08-18 03:49:56 +03:00
CSS
------------------------------------------------------------
.item-info .controls {
2016-11-26 22:23:01 +03:00
margin-left: auto;
padding-left: 2em;
}
.item-info .controls > span {
white-space: nowrap;
}
/* on big screens we don't want to wrap the controls */
@media (min-width: 480px) {
.item-info .controls {
white-space: nowrap;
}
.item-info .controls > span:first-child {
padding-right: 1em;
}
2016-08-18 03:49:56 +03:00
}
.item-info .controls img {
opacity: 0.4;
2016-11-26 22:23:01 +03:00
height: 20px;
position: relative;
bottom: -3px;
2016-08-18 03:49:56 +03:00
}
2016-08-17 11:18:57 +03:00
JS
------------------------------------------------------------
function moveItem(dir, itemUid) {
var url = "/haskell/move/item/" + itemUid;
itemNode = '#item-' + itemUid;
$.post(url, {direction: dir})
.done(function () {
if (dir == "up") moveNodeUp(itemNode); else moveNodeDown(itemNode);
fadeIn(itemNode);
});
}
function deleteItem(itemUid) {
var itemNode = '#item-' + itemUid;
if (confirm("Confirm deletion?")) {
$.post("/haskell/delete/item/" + itemUid)
.done(function () {
fadeOutAndRemove(itemNode);
});
}
}
function editItemInfo(itemUid) {
switchSection("#item-" + itemUid + " .item-info", "editing");
}
2017-04-23 22:19:14 +03:00
CSS
------------------------------------------------------------
.formLabel {
display: block;
margin-bottom: 5px;
margin-top: 15px;
}
.form-group {
margin-top: 15px;
margin-bottom: 5px;
}
.form-btn-group {
margin-top: 20px;
margin-bottom: 5px;
}
.save {
margin-right: 20px;
}
2016-08-17 11:18:57 +03:00
HTML: item-info-edit-form
------------------------------------------------------------
{{! "autocomplete=off" everywhere: http://stackoverflow.com/q/8311455 }}
2017-04-23 22:19:14 +03:00
<form class="form-group" onsubmit="submitItemInfo('{{item.uid}}', this); return false;">
<label class="formLabel" for="name">
Name
2016-08-17 11:18:57 +03:00
</label>
2017-04-23 22:19:14 +03:00
<input id="name" name="name" value="{{item.name}}"
type="text" autocomplete="off">
2016-08-17 11:18:57 +03:00
2017-04-23 22:19:14 +03:00
<label class="formLabel" for="kind">
Kind
2016-08-17 11:18:57 +03:00
</label>
2017-04-23 22:19:14 +03:00
<select id="kind" name="kind" autocomplete="off">
{{! possible_kinds would have stuff like “library”, “tool”, “other” }}
{{#possible_kinds}}
<option value="{{name}}" {{%selectIf selected}}>{{caption}}</option>
{{/possible_kinds}}
</select>
<label class="formLabel" for="hackage-name">
Name on Hackage
</label>
<input id="hackage-name" name="hackage-name" value="{{#item.kind.hackageName}}{{.}}{{/item.kind.hackageName}}"
type="text" autocomplete="off">
2016-08-17 11:18:57 +03:00
2017-04-23 22:19:14 +03:00
<label class="formLabel" for="site">
Site (optional)
2016-08-17 11:18:57 +03:00
</label>
2017-04-23 22:19:14 +03:00
<input id="site" name="link" value="{{item.link}}"
type="text" autocomplete="off">
<div class="form-group">
<label class="formLabel" for="group">
Group
2016-08-17 11:18:57 +03:00
</label>
2017-04-23 22:19:14 +03:00
{{! When “new group” is selected in the list, we show a field for
entering new group's name }}
<select id="group" name="group" onchange="itemGroupSelectHandler(this);"
autocomplete="off">
<option value="-" {{%selectIf item_no_group}}>-</option>
{{# category_groups }}
<option value="{{name}}" {{%selectIf selected}}>{{name}}</option>
{{/ category_groups }}
<option value="">New group...</option>
</select>
2016-08-18 22:56:56 +03:00
<input hidden class="custom-group-input" name="custom-group"
2016-08-17 11:18:57 +03:00
type="text" autocomplete="off">
2017-04-23 22:19:14 +03:00
</div>
2016-08-17 11:18:57 +03:00
2017-04-23 22:19:14 +03:00
<div class="form-btn-group">
<input value="Save" class="save" type="submit">
<input value="Cancel" class="cancel" type="button"
onclick="itemInfoCancelEdit('{{item.uid}}');">
</div>
2016-08-17 11:18:57 +03:00
</form>
JS
------------------------------------------------------------
function itemInfoCancelEdit(itemUid) {
switchSection("#item-" + itemUid + " > .item-info", "normal");
}
2016-09-03 21:49:55 +03:00
function itemGroupSelectHandler(select) {
var customInput = $(select).closest("form").find(".custom-group-input");
if ($(select)[0].value == "") {
2016-08-17 11:18:57 +03:00
customInput.show();
customInput.focus();
} else {
customInput.hide();
}
}
function submitItemInfo(itemUid, form) {
custom = $(form)[0].elements["custom-group"].value;
// If the group was changed, we need to recolor the whole item,
// but we don't want to rerender the item on the server because
// it would lose the item's state (e.g. what if the traits were
// being edited? etc). So, instead we query colors from the server
// and change the color of the item's body manually.
var url = "/haskell/set/item/" + itemUid + "/info";
itemNode = '#item-' + itemUid;
$.post(url, $(form).serialize())
.done(function (data) {
$.get("/haskell/render/item/"+itemUid+"/colors")
.done(function (colors) {
$(itemNode + " .item-body").css("background-color", colors.light);
$(itemNode + " .item-info").replaceWith(data);
});
// And now, if a custom group was created, we should add it to other
// items' lists.
if (custom != "") {
$(".item").each(function (i, item) {
groups = $(item).find("select[name=group]")[0];
isOurOption = function (opt) {return opt.text == custom};
alreadyExists = $.grep(groups.options, isOurOption).length > 0;
if (!alreadyExists) {
groups.add(new Option(custom, custom), 1); }
});
}
});
}