github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/server/camlistored/ui/pics.js (about)

     1  /*
     2  Copyright 2013 The Camlistore Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  goog.provide('cam.GalleryPage');
    18  
    19  goog.require('goog.dom');
    20  goog.require('goog.events.EventHandler');
    21  goog.require('goog.events.EventType');
    22  goog.require('goog.ui.Component');
    23  
    24  goog.require('cam.ServerConnection');
    25  
    26  // @param {cam.ServerType.DiscoveryDocument} config Global config of the current server this page is being rendered for.
    27  // @param {goog.dom.DomHelper=} opt_domHelper DOM helper to use.
    28  cam.GalleryPage = function(config, opt_domHelper) {
    29  	goog.base(this, opt_domHelper);
    30  
    31  	this.config_ = config;
    32  	this.connection_ = new cam.ServerConnection(config);
    33  };
    34  goog.inherits(cam.GalleryPage, goog.ui.Component);
    35  
    36  cam.GalleryPage.prototype.decorateInternal = function(element) {
    37  	cam.GalleryPage.superClass_.decorateInternal.call(this, element);
    38  };
    39  
    40  cam.GalleryPage.prototype.disposeInternal = function() {
    41  	cam.GalleryPage.superClass_.disposeInternal.call(this);
    42  };
    43  
    44  cam.GalleryPage.prototype.enterDocument = function() {
    45  	cam.GalleryPage.superClass_.enterDocument.call(this);
    46  
    47  	var members = goog.dom.getElement('members');
    48  	if (!members) {
    49  		return;
    50  	}
    51  	var children = goog.dom.getChildren(members);
    52  	if (!children || children.length < 1) {
    53  		return;
    54  	}
    55  	goog.array.forEach(children, function(li) {
    56  		li.src = li.src + '&square=1';
    57  	})
    58  
    59  	if (camliViewIsOwner) {
    60  		var el = this.getElement();
    61  		goog.dom.classes.add(el, 'camliadmin');
    62  
    63  		goog.array.forEach(children, function(li) {
    64  			var lichild = goog.dom.getFirstElementChild(li);
    65  			var titleSpan = goog.dom.getLastElementChild(lichild);
    66  			var editLink = goog.dom.createElement('a', {'href': '#'});
    67  			goog.dom.classes.add(editLink, 'hidden');
    68  			goog.dom.setTextContent(editLink, 'edit title');
    69  
    70  			var titleInput = goog.dom.createElement('input');
    71  			goog.dom.classes.add(titleInput, 'hidden');
    72  
    73  			goog.events.listen(editLink,
    74  				goog.events.EventType.CLICK,
    75  				function(e) {
    76  					goog.dom.classes.remove(titleSpan, 'visible');
    77  					goog.dom.classes.add(titleSpan, 'hidden');
    78  					goog.dom.classes.remove(titleInput, 'hidden');
    79  					goog.dom.classes.add(titleInput, 'visible');
    80  					titleInput.focus();
    81  					titleInput.select();
    82  					e.stopPropagation();
    83  					e.preventDefault();
    84  				},
    85  				false, this
    86  			);
    87  			goog.events.listen(li,
    88  				goog.events.EventType.MOUSEOVER,
    89  					function(e) {
    90  						goog.dom.classes.remove(editLink, 'hidden');
    91  						goog.dom.classes.add(editLink, 'title-edit');
    92  					},
    93  					false, this
    94  			);
    95  			goog.events.listen(li,
    96  				goog.events.EventType.MOUSEOUT,
    97  					function(e) {
    98  						goog.dom.classes.remove(editLink, 'title-edit');
    99  						goog.dom.classes.add(editLink, 'hidden');
   100  						goog.dom.classes.remove(titleInput, 'visible');
   101  						goog.dom.classes.add(titleInput, 'hidden');
   102  						goog.dom.classes.remove(titleSpan, 'hidden');
   103  						goog.dom.classes.add(titleSpan, 'visible');
   104  					},
   105  					false, this
   106  			);
   107  			goog.events.listen(titleInput,
   108  				goog.events.EventType.KEYPRESS,
   109  				goog.bind(function(e) {
   110  					if (e.keyCode == 13) {
   111  						this.saveImgTitle_(titleInput, titleSpan);
   112  					}
   113  				}, this),
   114  				false, this
   115  			);
   116  			goog.dom.insertSiblingBefore(editLink, titleSpan);
   117  			goog.dom.insertChildAt(li, titleInput, 1);
   118  			}, this
   119  		)
   120  	}
   121  }
   122  
   123  // @param {string} titleInput text field element for title
   124  // @param {string} titleSpan span element containing the title
   125  cam.GalleryPage.prototype.saveImgTitle_ = function (titleInput, titleSpan) {
   126  	var spanText = goog.dom.getTextContent(titleSpan);
   127  	var newVal = titleInput.value;
   128  	if (newVal != "" && newVal != spanText) {
   129  		goog.dom.setTextContent(titleSpan, newVal);
   130  		var blobRef = goog.dom.getParentElement(titleInput).id.replace(/^camli-/, '');
   131  		this.connection_.newSetAttributeClaim(
   132  			blobRef,
   133  			"title",
   134  			newVal,
   135  			function() {
   136  			},
   137  			function(msg) {
   138  				alert(msg);
   139  			}
   140  		);
   141  	}
   142  	goog.dom.classes.remove(titleInput, 'visible');
   143  	goog.dom.classes.add(titleInput, 'hidden');
   144  	goog.dom.classes.remove(titleSpan, 'hidden');
   145  	goog.dom.classes.add(titleSpan, 'visible');
   146  }
   147  
   148  cam.GalleryPage.prototype.exitDocument = function() {
   149  	cam.GalleryPage.superClass_.exitDocument.call(this);
   150  };