Building A Custom Widget: Flickr Image Library - akumina/AkuminaTraining GitHub Wiki

Custom Widget - Flickr Image Library

Screenshot

image 20

How to Deploy

  1. Download digitalworkplace.custom.js from the “/Style Library/DigitalWorkplace/JS” folder within SharePoint
  2. Paste the Widget Definition within digitalworkplace.custom.js
  3. Upload the updated digitalworkplace.custom.js to the “/Style Library/DigitalWorkplace/JS” folder within SharePoint
  4. In the Management Apps tab of Interchange, click on the View Manager. Click “Add New”. In the left pane navigate to “/DigitalWorkplace/Content/Templates/” for the folder path. Click “Choose File”, navigate to your custom template (FlickrGallery.html). Click “Save”.
  5. In the Management Apps tab of Interchange, click on the Widget Manager app. Then click on Add Widget. Create your widget with the values in the Widget Manager – Widget Definition section. Click Save & Exit
  6. In the Manage Widgets window, find Flickr and click its ‘View Widget Definitions’ button. Then click on ‘Add New’. Create your widget instance with the values in the Widget Manager – Widget Instance section. Click Save & Exit
  7. Copy the Widget Snippet of your Widget Instance.
  8. Paste the snippet into a Content Editor Web Part on a page within the site. Publish the page.
  9. Flush your cache by clicking on the Akumina icon in the left rail, clicking the refresh icon, then clicking “Refresh All Cache”
  10. Refresh the page. You will see your Flickr Widget

Widget Definition

if ((typeof Akumina.AddIn.FlickrWidget) === 'undefined') {
	Akumina.AddIn.FlickrWidget = function () {
		var _cur = this; 
		
		this.GetPropertyValue = function (requestIn, key, defaultValue) {
            var propertyValue = "";

            for (var prop in requestIn) {
                if (key.toLowerCase() == prop.toLowerCase()) {
                    propertyValue = requestIn[prop];
                    break;
                }
            }

            return (propertyValue == undefined || propertyValue.toString().trim() == "") ? defaultValue : propertyValue;
        };
		
		this.SetDefaultsProperties = function (requestIn) {
            var requestOut = requestIn;

            requestOut.SenderId = _cur.GetPropertyValue(requestIn, "id", "");
            requestOut.DisplayTemplateUrl = _cur.GetPropertyValue(requestIn, "displaytemplateurl", "");
            requestOut.Tags = _cur.GetPropertyValue(requestIn, "tags", "");
            requestOut.callbackMethod = _cur.GetPropertyValue(requestIn, "callbackmethod", "");

            return requestOut;
        };
		
		this.Init = function (FlickrRequest) {
            _cur.appWebUrl = decodeURIComponent(Akumina.AddIn.Utilities.getQueryStringParameter("SPAppWebUrl", ""));
            _cur.hostUrl = decodeURIComponent(Akumina.AddIn.Utilities.getQueryStringParameter("SPHostUrl", ""));
            _cur.FlickrRequest = _cur.SetDefaultsProperties(FlickrRequest);
            _cur.FlickrRequest.EditMode = Akumina.AddIn.Utilities.getEditMode();
			var widgetName = "Flickr";
			Akumina.Digispace.WidgetPropertyViews.AddViewForProperty(widgetName, "DisplayTemplateUrl", "TemplatePicker");
			
            _cur.Prerender();
        };
		
		this.Prerender = function () {
            var targetDiv = this.FlickrRequest.SenderId;
            $("#" + targetDiv).html(Akumina.Digispace.ConfigurationContext.LoadingTemplateHtml);
            Akumina.Digispace.AppPart.Eventing.Subscribe('/loader/completed/', _cur.Render);
			Akumina.Digispace.AppPart.Eventing.Subscribe('/widget/updated/', _cur.RefreshWidget);
        };
		
		this.Render = function () {
            
			
            var tags = _cur.FlickrRequest.Tags;
			
			$.ajax({
				type: 'GET',
			    url: 'https://api.flickr.com/services/feeds/photos_public.gne',
				jsonpCallback: 'jsonFlickrFeed',
			    dataType: 'jsonp',
			    data: { "tags": tags, "format": "json" },
			    success: function(response) {_cur.Success(response)}
			});
					
		};
		
		this.Success = function (response)
		{
			console.log(response);
			var data = {};
			data.Items = [];
			data.SenderId = _cur.FlickrRequest.SenderId;
			data.Title = response.title;
			
			for(i = 0; i < response.items.length; i++) {
				var flickrInfo = response.items[i];
				var media = flickrInfo.media.m;
				
				var flickrItem = {
					"Image": media
				}
				
				data.Items.push(flickrItem);
			}
			
			if (!_cur.FlickrRequest.EditMode) {
				_cur.BindTemplate(_cur.FlickrRequest.DisplayTemplateUrl, data, _cur.FlickrRequest.SenderId);
			}	
		}
		
		this.RefreshWidget = function (newProps) {
		
			if (newProps["id"] == _cur.FlickrRequest.SenderId) {
				_cur.FlickrRequest = _cur.SetDefaultsProperties(newProps);
				_cur.Render();
			}
		
		};
            
		 this.BindTemplate = function (templateUri, data, targetDiv) {
            new Akumina.Digispace.AppPart.Data().Templates.ParseTemplate(templateUri, data).done(function (html) {
                $("#" + targetDiv).html(html);
            });
        };
    }
};

if ((typeof Akumina.AddIn.FlickrWidget) === ‘undefined’)

We define the Flickr widget with this line.

this.GetPropertyValue

Helper method to get properties

this.SetDefaultProperties

We set the default values to properties if nothing is passed in for them.

this.Init

this.Init calls the functions to render the widget. We also specify the widget name that we will use for the Widget Class step and we subscribe the widget to the Template Picker.

this.Prerender

this.Prerender defines what will show while the widget is rendering. We use subscriptions to call the this.Render function when loading is complete and this.RefreshWidget when the widget is updated.

this.Render

We make a GET call to Flickr. We pass the tags as paramaters. We’re using a Cross Domain AJAX call so we’re specifying the callback name, which is specific to site we are calling, in the jsonpCallback property. Upon successful query we call the Success function. Note: If you only want raw JSON without a function wrapper, add the parameter nojsoncallback with a value of 1 to your request. If you want to define your own callback function name, use the desired name as your jsonpcallback value.

this.Success

We assign the results from our GET to values to be used in our template

this.BindTemplate

We bind our data to our template and render

this.RefreshWidget

this.RefreshWidget resets the properties and calls this.Render

Template

Create a file called FlickrGallery.html and paste the markup below within

<div class="interAction">
    <div class="row">
        <div class="small-12 columns">
            <div class="ak-media-tabs">
                <h1 class="ak-media-tab">{{Title}}</h1>
            </div>
            <div class="ak-photo-list">
                <ul id="ak-photo-container">
					{{#if Items}}
                    {{#Items}}
                    <li>
                        <a href="#photo-popup" class="ak-photo-link">
                            <div class="ak-photo-image">
                                <img src="{{Image}}">
							</div>
                        </a>
                    </li>
                    {{/Items}}
					{{else}}
					<h4>No Photos Found</h4>
                </ul>
            </div>
            <div class="holder"></div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $('.ak-photo-link').magnificPopup({
        type: 'inline',
        preloader: false,
        closeBtnInside: true,
        showCloseBtn: true,
        fixedBgPos: true,
        mainClass: 'ak-photo-modal'
    });
    $('.ak-photo-link').on('click', function () {
        $('.imagepreview').attr('src', $(this).find('img').attr('src'));
    });
</script>

Widget Manager - Widget Definition

image 21

WidgetName

Flickr

WidgetClass

Akumina.AddIn.FlickrWidget

WidgetProperties

Property Name

tags

Friendly Name

tags

Property Value

cat

Property Type

Text

Widget Views

View 1

View Name

View 1

Relative Template Url

/Style Library/DigitalWorkplace/Content/Templates/FlickrGallery.html

Widget Manager - Widget Instance

image 22

Title

Flickr 1

WidgetOptions

View 1

Display View

Checked

Selected View

Selected

Widget Manager - Widget Snippet

image 23

Widget Manager - Change the Property Value

In your Akumina Foundation Site, click on the Akumina Icon in the Rail, click on the pencil, then click on your FlickrWidget to bring up the Widget Manager. Change the tags value to havanese. Click Update.

image 24 image 25

References

For more info on Custom Widgets see the following articles

⚠️ **GitHub.com Fallback** ⚠️